How to Compare Strings in C#: Best Practices
Comparing Strings in C#: Techniques, Performance, and Best Practices
By ByteNomads – May 2025
String comparison is one of those deceptively simple tasks in C# that can quickly become complex when you consider culture, case sensitivity, and performance. In this article, we'll explore different methods to compare strings in C#, highlight when to use each one, and look at real-world scenarios — including comparisons for text, numbers as strings, and even enums.
1. Using ==
and .Equals()
These are the most common methods for comparing strings.
string a = "hello";
string b = "HELLO";
Console.WriteLine(a == b); // False (case-sensitive)
Console.WriteLine(a.Equals(b)); // False (also case-sensitive)
When to use:
- When you want a simple, case-sensitive, culture-invariant comparison
- Comparing hard-coded values (e.g., user roles or status)
2. Using String.Equals()
with options
string a = "straรe";
string b = "STRASSE";
bool result = String.Equals(a, b, StringComparison.OrdinalIgnoreCase);
Console.WriteLine(result); // False
Why is this important? Because StringComparison.OrdinalIgnoreCase
is faster and safer for most technical comparisons (e.g., file names, identifiers).
Common options:
StringComparison.Ordinal
– Fastest, binary comparisonStringComparison.InvariantCultureIgnoreCase
– Case-insensitive, culture-invariantStringComparison.CurrentCulture
– For UI elements, user input, etc.
3. Using string.Compare()
More flexible than Equals
, especially when sorting or ordering is needed.
int result = string.Compare("apple", "Banana", StringComparison.OrdinalIgnoreCase);
Console.WriteLine(result < 0 ? "apple comes first" : "banana comes first");
When to use:
- Sorting strings in a list or UI
- When you need full control over comparison rules
4. Using CultureInfo
for Language-Specific Comparison
var turkish = new CultureInfo("tr-TR");
bool same = string.Equals("I", "ฤฑ", StringComparison.CurrentCulture);
Console.WriteLine(same); // False in Turkish locale
Culture can dramatically affect string comparison, especially in non-English locales. Be explicit when building globalized apps.
5. Comparing Numbers Stored as Strings
string x = "10";
string y = "2";
Console.WriteLine(x.CompareTo(y)); // Returns 1 (wrong for numeric sort)
This returns incorrect order because it's a lexicographical comparison. Convert to numeric types:
int xNum = int.Parse(x);
int yNum = int.Parse(y);
Console.WriteLine(xNum.CompareTo(yNum)); // Correct
6. Comparing Enums as Strings
enum Status { Pending, Approved, Rejected }
string input = "approved";
bool match = Enum.TryParse(typeof(Status), input, true, out var result);
Console.WriteLine(match); // True
Why it matters:
Enums are often serialized or passed as strings (e.g., API payloads). Use Enum.TryParse
with ignoreCase: true
for safe matching.
Conclusion
String comparison in C# isn't one-size-fits-all. Choosing the right method — and knowing the cultural and performance implications — helps build more robust and user-friendly applications.
Was this helpful? Follow ByteNomads for more hands-on programming tips and deep dives into real-world .NET scenarios.
Comments
Post a Comment