-1

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?

  • no. nothing changed, and probably never will. and i'm kinda glad it's not possible to do this, because people would use it to do math. – Franz Gleichmann Mar 17 '21 at 17:06
  • 2
    I'm glad I can load a DLL and being sure it will not change my string comparison. – Thomas Weller Mar 17 '21 at 17:08
  • 3
    I'd rather use (or implement for custom comparison) `IComparer`. We can compare strings in *many different ways*: case sensitive and insensitive, natural and lexicographically; some cultures have special requirements (e.g. in Finnish `W == V` when comparing) etc. It's impossible to implement one "standard" `<` `>` comparison for all cases – Dmitry Bychenko Mar 17 '21 at 17:13

1 Answers1

0

I'd still like to define the < and > operators

But why [.gifv]?

What does it mean to you that a string is "greater than" another? Its length? Or the first Unicode codepoint within it that's higher than the codepoint at the same location in the first string?

What about combined characters? Is ñ (U+00F1) greater or smaller than n and ~ combined (U+006E and U+0303), or perhaps equal? What about i + j and ij? Would the current culture matter?

Be happy that there's such a "clumsy" method to do comparisons, so you can call an overload on it to specify what kind of comparison you actually want. There is no one size fits all in string comparisons, and you definitely shouldn't be able to override it for all strings.

Besides, already compiled code doesn't care about extension methods (or extension operators, if that were a thing). Consider you're running library code in which you can definitely read that there's a if (foo < bar) and in your code it's true, but in the compiled code it's false, because it does a different comparison for the same operator.

So no, C# doesn't allow overriding operators for built-in types.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272