Complete the code to get the first number from the list.
var numbers = new List<int> {1, 2, 3};
int first = numbers.[1]();The First() method returns the first element in the list.
Complete the code to get the single element from the list that equals 5.
var numbers = new List<int> {5};
int single = numbers.[1](n => n == 5);The Single() method returns the only element that matches the condition. It throws an error if there are zero or more than one matches.
Fix the error in the code to safely get the first element or default if none exists.
var emptyList = new List<int>();
int firstOrDefault = emptyList.[1]();FirstOrDefault() returns the first element or the default value (0 for int) if the list is empty, avoiding exceptions.
Fill both blanks to get the single element matching the condition or default if none exists.
var numbers = new List<int> {1, 2, 3};
int result = numbers.[1](n => n == 2).[2]();Use Where() to filter, then FirstOrDefault() to get the first matching element or default if none.
Fill all three blanks to get the single element matching the condition or default if none exists, using method chaining.
var words = new List<string> {"apple", "banana", "cherry"};
string word = words.[1](w => w.StartsWith("b")).[2]().[3]();Filter with Where(), convert to list with ToList(), then get single or default with SingleOrDefault().