Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# String Equals: Choosing the Right Comparison

Understand C# string equality with ==, Equals, and String.Compare, including ordinal vs. culture-sensitive behavior, null handling, and performance trade-offs.

~5 min readC#
#String Comparison#C# Equality#StringComparison#Ordinal vs Culture
Two string variables being compared with an equals sign, illustrating C# string equality checks.

To compare two strings for equality in C#, choose between the == operator, string.Equals, and String.Compare. == and string.Equals are the usual tools for equality checks; String.Compare is for sorting and can also be used for equality if you check that its result is zero. The main decisions are ordinal versus culture-sensitive comparison, case sensitivity, and null handling.

How the == Operator Works for Strings

The == operator on strings is overloaded to compare the character sequence instead of object references. It compiles to the static String.op_Equality method and performs an ordinal, case-sensitive comparison. This is deterministic and does not apply culture-specific rules, which is usually what you want for internal identifiers and status codes.

csharp
string left = "café"; string right = "café"; bool equal = left == right; // true

The operator also handles null operands: null == null is true, and null == "x" is false.

Instance Equals vs Static String.Equals

The instance method string.Equals(string other) performs the same ordinal comparison as == for strings. The static method string.Equals(string a, string b) also compares string values by ordinal, but it handles null operands without throwing. If both are null, it returns true; if one is null and the other is not, it returns false. Calling the instance method on a null reference throws a NullReferenceException.

csharp
string a = null; string b = null; bool staticResult = string.Equals(a, b); // true // bool instanceResult = a.Equals(b); // NullReferenceException

Because of this null safety, string.Equals is often the safer choice when either operand could be null. The == operator also handles null correctly.

Ordinal vs Culture-Sensitive Comparisons

An ordinal comparison looks at the raw Unicode code points. It is fast, deterministic, and independent of the current culture. A culture-sensitive comparison applies rules from a specific culture, such as case folding, accent sensitivity, and sorting order. For example, Turkish has distinct mappings for dotless I and dotted İ, so ignore-case behaviors can differ from the invariant culture. StringComparison.InvariantCultureIgnoreCase uses the invariant culture, so it is not affected by the current machine's locale.

The default for == and Equals(string) is ordinal. String.Compare, however, defaults to culture-sensitive comparison, which is a common source of bugs when developers use it to test equality.

csharp
string s1 = "Straße"; string s2 = "STRASSE"; bool ordinalIgnoreCase = string.Equals(s1, s2, StringComparison.OrdinalIgnoreCase); // false bool invariantIgnoreCase = string.Equals(s1, s2, StringComparison.InvariantCultureIgnoreCase); // true

If you are comparing strings that come from user input and you need to follow the user's language rules, use a culture-sensitive comparison. If you are comparing internal identifiers, tokens, or machine-generated values, use ordinal comparison to avoid surprises.

Case Sensitivity and Common Pitfalls

A frequent mistake is assuming that == is case-insensitive. It is not. To compare strings without regard to case, use StringComparison.OrdinalIgnoreCase or a culture-aware option. The Equals method has an overload that accepts the StringComparison enum.

csharp
string input = "Admin"; bool isAdmin = input.Equals("admin", StringComparison.OrdinalIgnoreCase); // true

Using ToLower() or ToUpper() before comparison is another approach, but it allocates a new string and can introduce culture-specific issues. Prefer StringComparison overloads when possible. If input might be null, use the static overload instead of the instance call.

Performance Considerations

Ordinal comparisons are generally faster than culture-sensitive ones because they do not need to consult culture-specific tables or apply linguistic rules. For hot paths, such as comparing keys in a dictionary or filtering large collections, use StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase. The runtime can also optimize ordinal comparisons with direct memory comparisons.

Culture-sensitive comparisons are more expensive and can vary by culture. They are necessary when you are sorting strings for display in a user interface or when you need to respect linguistic conventions. For equality checks, ordinal is almost always sufficient and safer.

If you use a Dictionary<string, T> or HashSet<string>, the default equality comparer uses ordinal string equality. That is a good default for most internal uses. If you need case-insensitive keys, use StringComparer.OrdinalIgnoreCase.

Choosing the Right Comparison for Your Scenario

The decision depends on the origin and purpose of the strings. Use ordinal comparison when:

  • Comparing internal identifiers, tokens, or machine-generated values.
  • Implementing security-sensitive checks where culture rules could be exploited.
  • You need deterministic results that do not vary by locale.

Use culture-sensitive comparison when:

  • Sorting strings for display to users in a specific language.
  • Comparing user-entered names or addresses where linguistic rules matter.
  • You need to match the behavior of a specific locale.

For most equality checks, StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase is the right choice.

Handling Null and Empty Strings

When comparing strings, null and empty are different. An empty string is a valid instance with zero characters; null means no instance at all. The == operator and string.Equals treat them differently: null == "" is false. The string.IsNullOrEmpty method is useful when you want to treat null and empty as equivalent for validation, but it does not perform equality comparison.

csharp
string value = null; if (string.IsNullOrEmpty(value)) { /* handles both null and empty */ }

If you need to compare a string that might be null against a known value, use the static string.Equals with a StringComparison overload, or use the == operator, which handles null correctly. Avoid calling the instance method on a potentially null reference.

Common Mistakes and Edge Cases

One common mistake is using String.Compare to test equality. String.Compare returns an integer indicating the relative order, not a boolean. It is also culture-sensitive by default. If you use it for equality, check that the result is zero and specify the desired comparison type.

csharp
int result = string.Compare("apple", "Apple", StringComparison.OrdinalIgnoreCase); bool equal = result == 0; // true

Another edge case is comparing strings that contain combining characters. Ordinal comparison treats a base character and a combining mark as separate code points, while a culture-sensitive comparison may treat the composed and decomposed forms as equal. If you are dealing with user-visible text, you might need to normalize the strings first using string.Normalize().

Finally, remember that == on strings is not the same as == on object. If the operands are typed as object, the compiler uses reference equality. Because identical string literals can be interned, the reference comparison in the example below may be true; do not rely on it for value equality.

csharp
string a = "hello"; string b = "hello"; object objA = a; object objB = b; bool refEq = objA == objB; // reference comparison; may be true because literals are interned bool valEq = a == b; // true, because string overload

In most production code, you will not cast strings to object, but it is worth knowing when debugging or writing generic algorithms.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.