Personally, I find the string.CompareTo() annoying to use and read.
I would love to read and write like: if (string1 > string2)
Currently, I can do this, using ExtensionMethods:
string a = "ABC";
string b = "BBC";
if ( a.IsGreaterThan(b) )
{
Console.WriteLine("a is greater than b");
}
else if ( a.IsLessThan(b) )
{
Console.WriteLine("a is less than b");
}
public static class StringExtensions
{
public static bool IsGreaterThan(this string i, string value)
{
return (i.CompareTo(value) > 0);
}
public static bool IsLessThan(this string i, string value)
{
return (i.CompareTo(value) < 0);
}
}
This is much better, but I'd still like to define the < and > operators. I found an OLD post explaining why this is NOT possible:
Currently this is not supported because Extension methods are defined in separate static class and static classes cannot have operator overloading definitions.
Has anything changed in C# to now allow defining the < and > operators for string class?