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

Null-conditional operator 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 safely access the Length property of the string variable.

C Sharp (C#)
int? length = name[1]Length;
Drag options to blanks, or click blank then click option'
A?.
B.
C->
D::
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dot operator (.) causes a NullReferenceException if the object is null.
2fill in blank
medium

Complete the code to safely call the ToUpper method on the string variable.

C Sharp (C#)
string? upperName = name[1]ToUpper();
Drag options to blanks, or click blank then click option'
A.
B->
C?.
D::
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dot operator (.) causes a NullReferenceException if the object is null.
3fill in blank
hard

Fix the error in the code to safely get the length of the string or return 0 if null.

C Sharp (C#)
int length = name[1]Length ?? 0;
Drag options to blanks, or click blank then click option'
A?.
B.
C->
D::
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dot operator (.) causes a NullReferenceException if the object is null.
4fill in blank
hard

Fill both blanks to safely access the first character of the string or return '?' if null or empty.

C Sharp (C#)
char firstChar = name[1]Length > 0 ? name[2][0] : '?';
Drag options to blanks, or click blank then click option'
A?.
B.
C??
D->
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot operator (.) to check Length without null check causes exceptions.
Using null-coalescing operator (??) incorrectly in this context.
5fill in blank
hard

Fill all three blanks to safely get the length of the first string in the array or return 0 if null.

C Sharp (C#)
int length = names[1]Length > 0 ? names[0][2]Length [3] 0 : 0;
Drag options to blanks, or click blank then click option'
A?.
B??
C:
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Not checking if array is null before accessing Length.
Forgetting to provide default value with null-coalescing operator.