Enumerable.FirstOrDefault() returns the first element of a sequence, or a default value if the sequence contains no elements. For eg: Running the following query returns 'Jack'
C#
void Main()
{
List<string> list = new List<string>() { "Jack", "And", "Jill", "Went", "Up", "The", "Hill" };
var first = list.FirstOrDefault();
Console.WriteLine(first);
}
VB.NET
Private Sub Main()
Dim list As New List(Of String)(New String() {"Jack", "And", "Jill", "Went", "Up", "The", "Hill"})
Dim first = list.FirstOrDefault()
Console.WriteLine(first)
End Sub
Similarly if FirstOrDefault is used on an empty list of integers, it returns 0
No comments:
Post a Comment