Bird
Raised Fist0

How can you safely check if a string variable is null or empty before using it in C#?

hard🚀 Application Q9 of Q15
C Sharp (C#) - Strings and StringBuilder
How can you safely check if a string variable is null or empty before using it in C#?
AUse == operator to compare with empty string only
BUse string.IsNullOrEmpty method
CCheck if string length is greater than 10
DCompare string to "null" text
Step-by-Step Solution
Solution:
  1. Step 1: Identify safe null or empty check

    string.IsNullOrEmpty returns true if string is null or empty.
  2. Step 2: Understand why other options fail

    Comparing to "null" text or only empty string misses null; length check is unrelated.
  3. Final Answer:

    Use string.IsNullOrEmpty method -> Option B
  4. Quick Check:

    Use string.IsNullOrEmpty to check null or empty safely [OK]
Quick Trick: Use string.IsNullOrEmpty to avoid null or empty errors [OK]
Common Mistakes:
MISTAKES
  • Comparing string to "null" text instead of null value
  • Checking length without null check
  • Using == with empty string only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes