December 2007
Intermediate to advanced
896 pages
19h 57m
English
You need to compare two characters for equality, but you need the flexibility of performing a case-sensitive or case-insensitive comparison.
Create extension methods on the char type and use the Equals instance method on the char structure to compare the two characters:
static class CharStrExtMethods
{
public static bool IsCharEqual(this char firstChar, char secondChar)
{
return (IsCharEqual(firstChar, secondChar, false));
}
public static bool IsCharEqual(this char firstChar, char secondChar,
bool caseSensitiveCompare)
{
if (caseSensitiveCompare)
{
return (firstChar.Equals(secondChar));
}
else
{
return (char.ToUpperInvariant(firstChar).Equals(
char.ToUpperInvariant(secondChar)));
}
}
public static bool IsCharEqual(this char firstChar, CultureInfo firstCharCulture,
char secondChar, CultureInfo secondCharCulture)
{
return (IsCharEqual(firstChar, firstCharCulture,
secondChar, secondCharCulture, false));
}
public static bool IsCharEqual(this char firstChar, CultureInfo firstCharCulture,
char secondChar, CultureInfo secondCharCulture,
bool caseSensitiveCompare)
{
if (caseSensitiveCompare)
{
return (firstChar.Equals(secondChar));
}
else
{
return (char.ToUpper(firstChar, firstCharCulture).Equals
(char.ToUpper(secondChar, secondCharCulture)));
}
}
}The first overloaded IsCharEqual extension method takes only one parameter, which is the character to be compared against the value contained in the current char instance. ...
Read now
Unlock full access