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?
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With