Element Operators

Let's meet other operators.

var singleBird = birds.Single(b => b.Name == "Beo");
Console.WriteLine(singleBird.Color);
// return Red

var singleBird = birds.SingleOrDefault(b => b.Name == "Beo");
Console.WriteLine(singleBird.Color);
// return null

To check the first and last item in collection.

Console.WriteLine(birds.First().Name);
Console.WriteLine(birds.Last().Name);

Console.WriteLine(birds.First(b => b.Color =="Red"));

You can improve it with OrDefault method.

Another method is ElementAt(index) method.

var recBirds = birds.Where(b => b.Color == "Red");
recBirds(0);
// error

recBirds.ElementAt(0);

results matching ""

    No results matching ""