Bird
Raised Fist0

Which of the following is the correct syntax to compare two strings a and b for equality ignoring case?

easy📝 Syntax Q3 of Q15
C Sharp (C#) - Strings and StringBuilder
Which of the following is the correct syntax to compare two strings a and b for equality ignoring case?
Aa == b.ToLower()
Bstring.Equals(a, b, StringComparison.OrdinalIgnoreCase)
Ca.CompareTo(b) == 0
Da.Equals(b)
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct method for case-insensitive compare

    string.Equals with StringComparison.OrdinalIgnoreCase compares ignoring case.
  2. Step 2: Analyze other options

    a == b.ToLower() compares a to lowercased b only, not both. CompareTo is case-sensitive. a.Equals(b) is case-sensitive.
  3. Final Answer:

    string.Equals(a, b, StringComparison.OrdinalIgnoreCase) -> Option B
  4. Quick Check:

    Ignore case equality = string.Equals with OrdinalIgnoreCase [OK]
Quick Trick: Use string.Equals with OrdinalIgnoreCase for case-insensitive check [OK]
Common Mistakes:
MISTAKES
  • Lowercasing only one string before compare
  • Using CompareTo for equality
  • Assuming Equals ignores case by default

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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