
You have a collection of objects and want to find the first matching object. These objects instances can be of any type, such as Animal or Employee. Use the powerful control structures and extension methods in the C# language to achieve this goal. Here we look at how you can use the First extension method.
Initially here we look at a complete program that uses a List of object instances and then searches this List with two different methods. The two methods shown efficiently test each element in the List for a match. The following code demonstrates the two methods being used and their results being printed.
Program that uses First on class [C#]
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
class Animal
{
public string Name { get; set; }
public int Age { get; set; }
public override string ToString()
{
return string.Format("Name={0},Age={1}",
Name,
Age);
}
};
static List<Animal> _animals = new List<Animal>()
{
new Animal()
{
Name = "Camel",
Age = 5
},
new Animal()
{
Name = "Ape",
Age = 3
},
new Animal()
{
Name = "Dog",
Age = 6
}
};
static void Main()
{
// A
// Get Ape from collection
Animal a1 = GetAnimal1("Ape");
Console.WriteLine(a1);
// B
// Get Camel from collection
Animal a2 = GetAnimal2("Camel");
Console.WriteLine(a2);
}
static Animal GetAnimal1(string n)
{
foreach (Animal a in _animals)
{
if (a.Name == n)
{
return a;
}
}
throw new Exception(n);
}
static Animal GetAnimal2(string n)
{
return _animals.First(a => a.Name == n);
}
}
Output
Name=Ape,Age=3
Name=Camel,Age=5Class declaration. The Animal class is declared at the top, and it includes two automatically implemented properties. These have actual backing stores, but they are implicit. They means they are shorter to type. Next, an example List containing three Animal objects is instantiated. For the example, it is a static method, meaning it cannot be instantiated in two places at once.
Getting first elements. Method 1 above is called, and returns the first matching Animal with a Name of "Ape". The correct object will printed out. Method 2 above is then called, and it finds the first Animal with the Name of "Camel". The correct Animal is returned. As you can see, Method 2 is implemented with a LINQ expression.

Lambda expressions. The second method uses an extension method called First, which has an overload that accepts a lambda expression specifying which objects will match. The lambda expression, which uses =>, can be read as "goes to". The "a" in the expression is a variable name, and you could easily change it. On the right side of the expression, the Name is tested against the parameter n.
The C# programming language provides another extension method called FirstOrDefault in the System.Linq namespace that provides the same functionality as First but will return the default value for a type if there are no elements in the sequence. This method is excellent for determining the "best match" from a sorted or filtered query.
FirstOrDefault Method
Here we saw that you can successfully rewrite foreach loops into First method calls. We saw some lambda calculus and then proved the correctness of both methods. Additionally, we same some interesting C# syntax, such as the collection initializer syntax, and also several static methods and fields. We saw how you can use extension methods from the System.Linq namespace.
LINQ Examples