We compare strings to check if they are the same or to find their order. This helps us decide things like if two words match or which word comes first in a list.
String comparison and equality in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
bool result = string1 == string2;
bool resultIgnoreCase = string.Equals(string1, string2, StringComparison.OrdinalIgnoreCase);
int compareResult = string.Compare(string1, string2, StringComparison.Ordinal);Use == or string.Equals to check if two strings are exactly equal.
Use string.Compare to find which string comes first alphabetically.
Examples
C Sharp (C#)
string a = "hello"; string b = "hello"; bool isEqual = a == b; // true
C Sharp (C#)
string a = "Hello"; string b = "hello"; bool isEqualIgnoreCase = string.Equals(a, b, StringComparison.OrdinalIgnoreCase); // true
a comes before b.C Sharp (C#)
string a = "apple"; string b = "banana"; int result = string.Compare(a, b, StringComparison.Ordinal); // negative number
Sample Program
This program compares two strings in three ways: exact match, ignoring case, and alphabetical order.
C Sharp (C#)
using System; class Program { static void Main() { string word1 = "Cat"; string word2 = "cat"; bool equalExact = word1 == word2; bool equalIgnoreCase = string.Equals(word1, word2, StringComparison.OrdinalIgnoreCase); int compareResult = string.Compare(word1, word2, StringComparison.Ordinal); Console.WriteLine($"Exact equality: {equalExact}"); Console.WriteLine($"Equality ignoring case: {equalIgnoreCase}"); Console.WriteLine($"Compare result: {compareResult}"); } }
Important Notes
String comparison is case-sensitive by default.
Use StringComparison.OrdinalIgnoreCase to ignore case differences.
Compare returns 0 if strings are equal, less than 0 if first is before second, and greater than 0 if first is after second.
Summary
Use == or string.Equals to check if strings are equal.
Use string.Compare to find alphabetical order.
Ignore case by using StringComparison.OrdinalIgnoreCase.
Practice
1. Which of the following is the correct way to check if two strings
str1 and str2 have the same value in C#?easy
Solution
Step 1: Understand string equality operator
In C#,==compares the values of two strings correctly.Step 2: Analyze other options
str1 = str2is assignment,str1.Equalsis incomplete, andCompareToreturns an int, not a bool.Final Answer:
if (str1 == str2) -> Option AQuick Check:
Use==for string equality [OK]
Hint: Use == to compare string values directly [OK]
Common Mistakes:
- Using single = instead of ==
- Calling Equals without parentheses or arguments
- Using CompareTo expecting a boolean
2. Which of the following is the correct syntax to compare two strings
a and b ignoring case in C#?easy
Solution
Step 1: Identify case-insensitive comparison method
string.EqualswithStringComparison.OrdinalIgnoreCasecompares strings ignoring case.Step 2: Check other options
a == b.ToLower()compares different types,a.Equals(b)is case-sensitive, andstring.Comparereturns int, not bool.Final Answer:
string.Equals(a, b, StringComparison.OrdinalIgnoreCase) -> Option CQuick Check:
Use string.Equals with OrdinalIgnoreCase for case-insensitive [OK]
Hint: Use string.Equals with OrdinalIgnoreCase to ignore case [OK]
Common Mistakes:
- Using == which is case-sensitive
- Calling Equals without StringComparison argument
- Using string.Compare expecting boolean
3. What is the output of the following C# code?
string s1 = "apple";
string s2 = "Banana";
int result = string.Compare(s1, s2, StringComparison.OrdinalIgnoreCase);
Console.WriteLine(result);
medium
Solution
Step 1: Understand string.Compare with OrdinalIgnoreCase
It compares strings ignoring case and returns negative if first is before second alphabetically.Step 2: Compare "apple" and "Banana" ignoring case
"apple" comes before "banana" alphabetically, so result is negative (-1).Final Answer:
-1 -> Option AQuick Check:
"apple" < "Banana" ignoring case = -1 [OK]
Hint: Compare returns negative if first string is alphabetically before second [OK]
Common Mistakes:
- Assuming Compare returns boolean
- Ignoring case sensitivity in comparison
- Expecting 0 when strings differ
4. 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");
}
medium
Solution
Step 1: Analyze '==' operator behavior
The '==' operator compares strings case-sensitively, so "Hello" != "hello".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.Final Answer:
Using '==' compares case-sensitively, so it fails here -> Option DQuick Check:
'==' is case-sensitive, so this check fails [OK]
Hint: Use string.Equals with ignore case instead of == [OK]
Common Mistakes:
- Assuming ToLower() changes original string
- Using == for case-insensitive comparison
- Not calling Equals with StringComparison argument
5. You want to sort a list of strings alphabetically ignoring case in C#. Which approach correctly compares two strings
x and y inside a custom comparer?hard
Solution
Step 1: Understand sorting comparer requirements
A comparer must return negative, zero, or positive int based on alphabetical order.Step 2: Check each option's return value and case sensitivity
return string.Compare(x, y, StringComparison.OrdinalIgnoreCase); uses string.Compare with OrdinalIgnoreCase, correctly returning int for sorting ignoring case. return x == y ? 0 : 1; returns only 0 or 1, not suitable. return x.Equals(y) ? 0 : -1; returns 0 or -1 but ignores order and case. return x.CompareTo(y); uses CompareTo which is case-sensitive.Final Answer:
return string.Compare(x, y, StringComparison.OrdinalIgnoreCase); -> Option BQuick Check:
Use string.Compare with OrdinalIgnoreCase for case-insensitive sorting [OK]
Hint: Use string.Compare with OrdinalIgnoreCase in sorting comparer [OK]
Common Mistakes:
- Returning only 0 or 1 instead of negative/zero/positive
- Using case-sensitive CompareTo for ignoring case
- Using Equals which returns bool, not int
