Rami Yusf
Rami Yusf

Reputation: 3032

How to make string comparisons to be case insensitive on process / thread level?

I'm reading about string comparison in C# and I wondered:

Can I predefine the compare info on a process / thread level to be case insensitive so I can use == directly when compare two strings?

Upvotes: 1

Views: 120

Answers (2)

Nahum
Nahum

Reputation: 7197

Not exactly BUT:

You could create an attribute using PostSharp and apply it to all methods in your assembly.

I didn't use PostSharp for a long time so I can't provide you an example, but it can be done.

I don't like this idea and it might lead to lots of trouble in the future. But it is possible and close to what you've asked.

Upvotes: 0

Patrick Hofman
Patrick Hofman

Reputation: 157038

No, you can't.

The == operator calls string.Equals, which on it's own, calls string.EqualsHelper.

As you can see, it doesn't use any culture or comparision settings (like the Equals(String value, StringComparison comparisonType) overload does). It just compares the string character by character.

You have to call an overload of Equals to get the result you want, which isn't the default behavior, and can't be changed, unless you have a way to override every string with an own implementation of string.Equals or the == operator.

Upvotes: 8

Related Questions