Bird
0
0

The following code is intended to check if two strings are equal ignoring case, but it does not work as expected. What is the error?

medium📝 Debug Q14 of 15
C Sharp (C#) - Strings and StringBuilder
The following code is intended to check if two strings are equal ignoring case, but it does not work as expected. What is the error?
string a = "Hello";
string b = "hello";
if (a == b.ToLower())
{
Console.WriteLine("Equal");
} else {
Console.WriteLine("Not Equal");
}
AThe code should use 'string.Compare(a, b)' without ToLower()
Bb.ToLower() returns null, causing error
CThe code should use 'a.Equals(b)' instead
DUsing '==' compares case-sensitively, so it fails here
Step-by-Step Solution
Solution:
  1. Step 1: Analyze '==' operator behavior

    The '==' operator compares strings case-sensitively, so "Hello" != "hello".
  2. Step 2: Understand why ToLower() doesn't fix it

    Comparing 'a' to 'b.ToLower()' still compares case-sensitively; 'a' is "Hello" (mixed case), so comparison fails.
  3. Final Answer:

    Using '==' compares case-sensitively, so it fails here -> Option D
  4. Quick Check:

    '==' is case-sensitive, so this check fails [OK]
Quick Trick: Use string.Equals with ignore case instead of == [OK]
Common Mistakes:
MISTAKES
  • Assuming ToLower() changes original string
  • Using == for case-insensitive comparison
  • Not calling Equals with StringComparison argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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