Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a shortcut for StringComparison.InvariantCultureIgnoreCase?

Doing plenty of case-insensitive string comparisons, I end of with lots of lengthy statements like:

myList.FirstOrDefault(
    c => string.Equals(
             c.InternalName, 
             internalName, 
             StringComparison.InvariantCultureIgnoreCase));

What bothers me is the long name of StringComparison.InvariantCultureIgnoreCase.

I could think of having extension methods and the like to shorten the code I have to write but on the other hand I fear of "obfuscating" my code and thus making it harder to understand.

So my question is:

Is there a "best practice" of having to write less text and still do InvariantCultureIgnoreCase-style string comparisons?

like image 633
Uwe Keim Avatar asked Jan 10 '23 11:01

Uwe Keim


1 Answers

You could wrap it around in an Extension Method:

public static class StringExtensions
{
    public static bool EqualsCaseInsensitive(this string str, string other)
    {
        return string.Equals(str, other, StringComparison.InvariantCultureIgnoreCase);
    }
}

and then do:

myList.FirstOrDefault(
    c => c.InternalName.EqualsCaseInsensitive(internalName))
like image 180
Yuval Itzchakov Avatar answered Jan 28 '23 14:01

Yuval Itzchakov