Bird
Raised Fist0
C Sharp (C#)programming~10 mins

String comparison and equality in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - String comparison and equality
Start
Get string1
Get string2
Compare strings
Are strings equal?
YesOutput: Equal
Output: Not Equal
End
This flow shows how two strings are taken, compared for equality, and then outputs if they are equal or not.
Execution Sample
C Sharp (C#)
string s1 = "Hello";
string s2 = "hello";
bool areEqual = s1 == s2;
Console.WriteLine(areEqual);
This code compares two strings for equality and prints True or False.
Execution Table
StepVariable/ExpressionValueCondition/CheckAction/Output
1s1"Hello"N/AAssign string "Hello" to s1
2s2"hello"N/AAssign string "hello" to s2
3areEqual = s1 == s2FalseCompare "Hello" == "hello"Result is False because of case difference
4Console.WriteLine(areEqual)FalsePrint value of areEqualOutput: False
💡 Comparison done, output printed, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
s1null"Hello""Hello""Hello""Hello"
s2nullnull"hello""hello""hello"
areEqualundefinedundefinedundefinedFalseFalse
Key Moments - 2 Insights
Why does s1 == s2 return False even though the words look similar?
Because string comparison in C# is case-sensitive by default, "Hello" and "hello" differ in letter case, so the comparison returns False as shown in step 3 of the execution_table.
Can we use == to compare strings safely in C#?
Yes, == compares string contents, not references, so it works for equality checks as shown in step 3. But it is case-sensitive.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of areEqual at step 3?
AFalse
BTrue
Cnull
DUndefined
💡 Hint
Check the 'Value' column at step 3 in execution_table.
At which step is the output printed to the console?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Look for Console.WriteLine action in the execution_table.
If s2 was "Hello" instead of "hello", what would be the value of areEqual at step 3?
AFalse
BTrue
Cnull
DUndefined
💡 Hint
Consider how == compares strings case-sensitively as shown in step 3.
Concept Snapshot
String comparison in C# uses == to check if contents are equal.
Comparison is case-sensitive by default.
"Hello" == "hello" is False.
Use String.Equals with StringComparison for case-insensitive checks.
Console.WriteLine prints the boolean result.
Full Transcript
This example shows how two strings s1 and s2 are assigned values "Hello" and "hello" respectively. Then, the equality operator == compares them. Because C# string comparison is case-sensitive, the result is False. Finally, the program prints False to the console. Beginners often wonder why similar words are not equal; it's due to case sensitivity. The == operator compares string contents, not references, so it is safe for equality checks. If both strings had the same case, the result would be True.

Practice

(1/5)
1. Which of the following is the correct way to check if two strings str1 and str2 have the same value in C#?
easy
A. if (str1 == str2)
B. if (str1 = str2)
C. if (str1.Equals)
D. if (str1.CompareTo(str2))

Solution

  1. Step 1: Understand string equality operator

    In C#, == compares the values of two strings correctly.
  2. Step 2: Analyze other options

    str1 = str2 is assignment, str1.Equals is incomplete, and CompareTo returns an int, not a bool.
  3. Final Answer:

    if (str1 == str2) -> Option A
  4. Quick 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
A. a.Equals(b)
B. a == b.ToLower()
C. string.Equals(a, b, StringComparison.OrdinalIgnoreCase)
D. string.Compare(a, b)

Solution

  1. Step 1: Identify case-insensitive comparison method

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

    a == b.ToLower() compares different types, a.Equals(b) is case-sensitive, and string.Compare returns int, not bool.
  3. Final Answer:

    string.Equals(a, b, StringComparison.OrdinalIgnoreCase) -> Option C
  4. Quick 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
A. -1
B. 0
C. 1
D. Compilation error

Solution

  1. Step 1: Understand string.Compare with OrdinalIgnoreCase

    It compares strings ignoring case and returns negative if first is before second alphabetically.
  2. Step 2: Compare "apple" and "Banana" ignoring case

    "apple" comes before "banana" alphabetically, so result is negative (-1).
  3. Final Answer:

    -1 -> Option A
  4. Quick 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
A. The code should use 'string.Compare(a, b)' without ToLower()
B. b.ToLower() returns null, causing error
C. The code should use 'a.Equals(b)' instead
D. Using '==' compares case-sensitively, so it fails here

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]
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
A. return x == y ? 0 : 1;
B. return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
C. return x.Equals(y) ? 0 : -1;
D. return x.CompareTo(y);

Solution

  1. Step 1: Understand sorting comparer requirements

    A comparer must return negative, zero, or positive int based on alphabetical order.
  2. 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.
  3. Final Answer:

    return string.Compare(x, y, StringComparison.OrdinalIgnoreCase); -> Option B
  4. Quick 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