You can’t override operators for pre-existing classes. The closest you can get is to make an extension method:
public static bool EqualsCaseInsensitive(this String a, String b) {
return String.Equals(a, b, StringComparison.OrdinalIgnoreCase);
}
You can use it like so:
var areSame = stringA.EqualsCaseInsensitive(stringB);
That being said, it’s considered bad practice to add extension methods to core types like String
. You’d be better off just having a utility method do the comparison instead. And in this particular case, the utility method you need already exists:
var areSame = String.Equals(stringA, stringB, StringComparison.OrdinalIgnoreCase);
11
solved how to overload the == operator for strings? [closed]