Hello Basant.
As you are a beginner to C# I would like to point out that Arrays are fixed length and Lists are dynamic length. More often you would want to use Lists unless you are dealing with constant variables predetermined. However here is a little demonstration, showing some Array syntax and examples, and then finally how to search by condition without the hassle of using for loops.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
// 5 element int array (pre-populated)
int[] number_array1 = new int[] { 1, 2, 3, 4, 5 };
// 5 element int array (empty)
int[] number_array2 = new int[5];
// 2 element string array (pre-populated)
string[] string_array1 = new string[] { "foo", "bar" };
// 2 element MyObject array (pre-populated)
MyObject[] myobject_array1 = new MyObject[]
{
new MyObject
{
Name = "foo",
Age = 20
},
new MyObject
{
Name = "bar",
Age = 25
}
};
// search for a MyObject where his age is 25
MyObject result = myobject_array1.FirstOrDefault<MyObject>(x => x.Age == 25);
if (result != null)
Console.WriteLine("{0} is 25 years old", result.Name);
else
Console.WriteLine("no one in the array is 25");
Console.Read();
}
}
class MyObject
{
public string Name { get; set; }
public int Age { get; set; }
}
}
Hope this helps.