📖 Scenario: Imagine you are building a simple program that greets users. Sometimes, the user's name might not be provided, so you want to use a default greeting name instead.
🎯 Goal: You will create a program that uses the null-coalescing operator (??) to provide a default name when the user's name is missing.
📋 What You'll Learn
Create a string variable called userName with a null value.
Create a string variable called defaultName with the value "Guest".
Use the null-coalescing operator ?? to assign a variable nameToUse either userName if it is not null, or defaultName if userName is null.
Print the greeting message Hello, {nameToUse}!.
💡 Why This Matters
🌍 Real World
In real applications, user input or data from databases can be missing or null. The null-coalescing operator helps provide safe default values to avoid errors.
💼 Career
Understanding how to handle null values safely is important for writing robust C# programs, especially in web development and data processing jobs.
Progress0 / 4 steps
1
Create the userName variable with a null value
Create a string variable called userName and set it to null.
C Sharp (C#)
Hint
Use string? userName = null; to allow userName to be null.
2
Create the defaultName variable
Create a string variable called defaultName and set it to "Guest".
C Sharp (C#)
Hint
Use string defaultName = "Guest"; to set the default name.
3
Use the null-coalescing operator to assign nameToUse
Create a string variable called nameToUse and assign it the value of userName ?? defaultName using the null-coalescing operator.
C Sharp (C#)
Hint
Use ?? to choose userName if not null, otherwise defaultName.
4
Print the greeting message
Write a Console.WriteLine statement to print Hello, {nameToUse}! using string interpolation.
C Sharp (C#)
Hint
Use Console.WriteLine($"Hello, {nameToUse}!"); to print the greeting.
Practice
(1/5)
1.
What does the null-coalescing operator ?? do in C#?
easy
A. Returns the left value if it is not null; otherwise, returns the right value.
B. Checks if two values are equal.
C. Assigns a value only if the variable is null.
D. Throws an exception if the left value is null.
Solution
Step 1: Understand the operator's purpose
The null-coalescing operator ?? is used to provide a fallback value when the left-hand side is null.
Step 2: Analyze behavior
If the left value is not null, it returns that value; otherwise, it returns the right value.
Final Answer:
Returns the left value if it is not null; otherwise, returns the right value. -> Option A
Quick Check:
Null-coalescing operator = fallback value [OK]
Hint: Think: "Use right value only if left is null" [OK]
Common Mistakes:
Confusing it with equality check
Thinking it assigns values automatically
Assuming it throws exceptions on null
2.
Which of the following is the correct syntax to use the null-coalescing operator in C#?
string name = null;
string displayName = ??? "Guest";
easy
A. name ??
B. name ?? "Guest"
C. name ? "Guest"
D. name ?: "Guest"
Solution
Step 1: Identify correct operator usage
The null-coalescing operator syntax is left ?? right, where right is the fallback value.
Step 2: Match syntax to code
Given string displayName = name ?? "Guest";, it correctly assigns "Guest" if name is null.
Final Answer:
name ?? "Guest" -> Option B
Quick Check:
Correct syntax uses ?? between variables [OK]
Hint: Remember: null-coalescing uses two question marks ?? [OK]