Bird
0
0

You want to check if a user input string matches a stored password ignoring case and culture differences. Which is the best approach?

hard🚀 Application Q8 of 15
C Sharp (C#) - Strings and StringBuilder
You want to check if a user input string matches a stored password ignoring case and culture differences. Which is the best approach?
Astring.Compare(input, password, true) == 0
Bstring.Equals(input, password, StringComparison.OrdinalIgnoreCase)
Cinput.ToLower() == password.ToLower()
Dinput.Equals(password)
Step-by-Step Solution
Solution:
  1. Step 1: Identify best practice for secure string comparison

    Using string.Equals with OrdinalIgnoreCase is efficient and culture-invariant.
  2. Step 2: Evaluate other options

    input.ToLower() == password.ToLower() creates new strings and depends on culture. string.Compare(input, password, true) == 0 uses Compare with ignoreCase true but less explicit. input.Equals(password) is case-sensitive.
  3. Final Answer:

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

    Use OrdinalIgnoreCase for culture-invariant case-insensitive compare [OK]
Quick Trick: Use string.Equals with OrdinalIgnoreCase for password checks [OK]
Common Mistakes:
MISTAKES
  • Using ToLower which depends on culture
  • Using case-sensitive Equals
  • Assuming Compare is better than Equals

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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