0
0
C Sharp (C#)programming~10 mins

First, Single, and their OrDefault variants in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the first number from the list.

C Sharp (C#)
var numbers = new List<int> {1, 2, 3};
int first = numbers.[1]();
Drag options to blanks, or click blank then click option'
AFirst
BSingle
CLast
DCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using Single() when there are multiple elements causes an error.
2fill in blank
medium

Complete the code to get the single element from the list that equals 5.

C Sharp (C#)
var numbers = new List<int> {5};
int single = numbers.[1](n => n == 5);
Drag options to blanks, or click blank then click option'
AFirst
BLast
CSingle
DWhere
Attempts:
3 left
💡 Hint
Common Mistakes
Using First() may return the first match even if there are multiple.
3fill in blank
hard

Fix the error in the code to safely get the first element or default if none exists.

C Sharp (C#)
var emptyList = new List<int>();
int firstOrDefault = emptyList.[1]();
Drag options to blanks, or click blank then click option'
ASingleOrDefault
BFirstOrDefault
CFirst
DSingle
Attempts:
3 left
💡 Hint
Common Mistakes
Using First() causes an exception if the list is empty.
4fill in blank
hard

Fill both blanks to get the single element matching the condition or default if none exists.

C Sharp (C#)
var numbers = new List<int> {1, 2, 3};
int result = numbers.[1](n => n == 2).[2]();
Drag options to blanks, or click blank then click option'
AWhere
BFirstOrDefault
CSingleOrDefault
DFirst
Attempts:
3 left
💡 Hint
Common Mistakes
Using SingleOrDefault() directly may throw if multiple matches.
5fill in blank
hard

Fill all three blanks to get the single element matching the condition or default if none exists, using method chaining.

C Sharp (C#)
var words = new List<string> {"apple", "banana", "cherry"};
string word = words.[1](w => w.StartsWith("b")).[2]().[3]();
Drag options to blanks, or click blank then click option'
AWhere
BSingleOrDefault
CToList
DFirstOrDefault
Attempts:
3 left
💡 Hint
Common Mistakes
Skipping ToList() may cause errors if multiple matches exist.