Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Nullable value types in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Nullable value types
Declare nullable value type
↓
Assign value or null
↓
Check HasValue property
↓
Use Value
↓
End
This flow shows how a nullable value type is declared, assigned a value or null, checked for a value, and then used or handled accordingly.
Execution Sample
C Sharp (C#)
int? number = null;
if (number.HasValue)
    Console.WriteLine(number.Value);
else
    Console.WriteLine("No value");
This code declares a nullable int, checks if it has a value, and prints the value or a message if null.
Execution Table
StepCode LineVariable 'number'Condition 'number.HasValue'ActionOutput
1int? number = null;nullN/AAssign null to number
2if (number.HasValue)nullFalseGo to else branch
3Console.WriteLine(number.Value);nullN/ASkipped
4Console.WriteLine("No value");nullN/APrint messageNo value
💡 Condition number.HasValue is false because number is null, so else branch runs and execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
numberundefinednullnullnull
Key Moments - 2 Insights
Why can't we directly use 'number' without checking HasValue?
Because 'number' might be null, accessing 'number.Value' without checking HasValue causes an error. See execution_table step 2 where HasValue is false, so we avoid using Value.
What does 'int?' mean in the declaration?
'int?' means the variable can hold either an int value or null. This is shown in execution_table step 1 where number is assigned null.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'number' after step 1?
A0
Bnull
Cundefined
DHasValue
💡 Hint
Check the 'Variable number' column in row for step 1.
At which step does the program decide to print 'No value'?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Look at the 'Action' and 'Output' columns in the execution table.
If 'number' was assigned 5 instead of null, what would 'number.HasValue' be at step 2?
ATrue
BFalse
Cnull
DUndefined
💡 Hint
Refer to variable_tracker and the meaning of HasValue property.
Concept Snapshot
Nullable value types allow value types to hold null.
Use syntax: int? number = null;
Check if value exists with HasValue.
Access actual value with Value property.
Always check HasValue before using Value to avoid errors.
Full Transcript
This example shows how nullable value types work in C#. We declare 'int? number' which can hold an int or null. Initially, number is null. We check 'number.HasValue' to see if it has a value. Since it is false, we skip printing the value and print 'No value' instead. This prevents errors from accessing a null value. Nullable types are useful when a value might be missing or undefined.

Practice

(1/5)
1. What does a nullable value type in C# allow you to do?
easy
A. Store only non-null values
B. Store values only in reference types
C. Store multiple values at once
D. Store a value or no value (null)

Solution

  1. Step 1: Understand nullable value types

    Nullable value types allow a value type to hold either a normal value or no value (null).
  2. Step 2: Compare options

    Only Store a value or no value (null) correctly describes this behavior; others are incorrect because value types normally cannot be null.
  3. Final Answer:

    Store a value or no value (null) -> Option D
  4. Quick Check:

    Nullable value type = value or null [OK]
Hint: Nullable means value or null, not multiple values [OK]
Common Mistakes:
  • Thinking nullable stores multiple values
  • Confusing nullable with reference types
  • Assuming nullable cannot hold null
2. Which of the following is the correct syntax to declare a nullable integer in C#?
easy
A. int number?;
B. int? number;
C. nullable int number;
D. Nullable number int;

Solution

  1. Step 1: Recall nullable syntax

    In C#, you add a question mark after the value type to make it nullable, like int?.
  2. Step 2: Check each option

    Only int? number; uses the correct syntax int? number;. Others are invalid syntax.
  3. Final Answer:

    int? number; -> Option B
  4. Quick Check:

    Nullable int syntax = int? [OK]
Hint: Use ? right after type for nullable, like int? [OK]
Common Mistakes:
  • Placing ? after variable name
  • Using 'nullable' keyword incorrectly
  • Mixing order of type and nullable
3. What is the output of this C# code?
int? x = null;
int y = x ?? 5;
Console.WriteLine(y);
medium
A. 0
B. null
C. 5
D. Compilation error

Solution

  1. Step 1: Understand the null-coalescing operator

    The operator ?? returns the left value if not null; otherwise, it returns the right value.
  2. Step 2: Apply to given code

    Since x is null, y gets the value 5, so Console.WriteLine(y); prints 5.
  3. Final Answer:

    5 -> Option C
  4. Quick Check:

    x is null, so y = 5 [OK]
Hint: ?? returns right value if left is null [OK]
Common Mistakes:
  • Assuming null prints as 0
  • Confusing ?? with || operator
  • Expecting compilation error for null assignment
4. Identify the error in this code snippet:
int? a = 10;
int b = a;
Console.WriteLine(b);
medium
A. Cannot assign nullable int to int without checking for null
B. Variable 'a' must be initialized
C. Console.WriteLine cannot print int values
D. No error, code runs fine

Solution

  1. Step 1: Check assignment from nullable to non-nullable

    Assigning a nullable int directly to an int variable without checking for null causes a compile error.
  2. Step 2: Explain why error occurs

    Because a might be null, C# requires explicit null check or use of .Value or null-coalescing operator.
  3. Final Answer:

    Cannot assign nullable int to int without checking for null -> Option A
  4. Quick Check:

    Nullable to int needs null check [OK]
Hint: Check nullable has value before assigning to non-nullable [OK]
Common Mistakes:
  • Ignoring null check before assignment
  • Assuming implicit conversion works
  • Thinking Console.WriteLine causes error
5. Given a nullable double double? d = null;, which code safely assigns its value to a non-nullable double result with default 0.0 if d is null?
hard
A. double result = d ?? 0.0;
B. double result = d.Value;
C. double result = (double)d;
D. double result = d != null ? d : null;

Solution

  1. Step 1: Understand safe assignment from nullable

    To assign a nullable to non-nullable safely, provide a default value if null using ??.
  2. Step 2: Evaluate each option

    double result = d.Value; throws exception if d is null. double result = d ?? 0.0; uses ?? to assign 0.0 if null, which is safe. double result = (double)d; casts but throws if null. double result = d != null ? d : null; tries to assign null to non-nullable, invalid.
  3. Final Answer:

    double result = d ?? 0.0; -> Option A
  4. Quick Check:

    Use ?? to provide default for nullable [OK]
Hint: Use ?? operator to assign default if nullable is null [OK]
Common Mistakes:
  • Using .Value without null check
  • Casting nullable directly without check
  • Assigning null to non-nullable variable
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard