views:

301

answers:

6

G'Day Mates -

What is the right way (excluding the argument of whether it is advisable) to overload the string operators <, >, <= and >= ?

I've tried it five ways to Sunday and I get various errors - my best shot was declaring a partial class and overloading from there, but it won't work for some reason.

namespace System
{
   public partial class String
   {
       public static Boolean operator <(String a, String b)
       {
           return a.CompareTo(b) < 0;
       }

       public static Boolean operator >(String a, String b)
       {
           return a.CompareTo(b) > 0;
       }
   }

}

+3  A: 

Do you mean the System.String class? That's impossible in C#. You can't add extension operators to an existing class. It's a much requested feature though.

Daniel Earwicker
+9  A: 

String is a sealed class. You cannot inherit from it, and without the original source for String, you cannot compile a partial class of it. Even if you got your hands on the source (it's possible via Reflector or via Visual Studio symbol download) you'd still have problems, since it's a first class citizen in the runtime.

Do you really need < and > as operators on string? If so... why not just use extension methods?

public static bool IsLessThan(this string a, string b) 
{ 
    return a.CompareTo(b) < 0; 
} 

public static bool IsGreaterThan(this string a, string b) 
{ 
    return a.CompareTo(b) > 0; 
}


// elsewhere...
foo.IsLessThan(bar); // equivalent to foo < bar
Randolpho
Even if he could inherit from it, only instances that were of his inherited type **and referenced as the inherited type** would be able to use the alternate operators. Because operators are not polymorphic (they're overloaded, not overridden) even referencing the subtype as a `string` would eliminate the alternate operator functionality.
Adam Robinson
@Adam Robinson: exactly. Bottom line: @ScottSEA simply cannot do what he wants to do.
Randolpho
+7  A: 

The simple answer is that you can't; there's no way to modify the operators for another class. Partial classes are only allowed for classes that are both declared partial in all files and defined in the same assembly.

Adam Robinson
+1  A: 
  • You can't have a partial class for string, because the string class itself is not partial and hence won't work with your partial class.

  • String is sealed, so you can inherit from it and then overload the operator.

In short, alas, you can't do what you with to do.

Not know what you're trying to do exactly, I can't suggest a good alternative. But, take a look at extension methods, which are often good for situations. Leaving aside whether you should :), you could add to the string class a method called IsGreaterThan and return true or false however you wish. This is good because you can give the extension method a name that makes it's meaning clear, keeps the existing operators in tact (which you have no choice about anyway), and allows for quick/simple code.

Patrick Karcher
A: 
Henri
Downvote because of bad practice? :p
Henri
+4  A: 

There is no way to replace any built-in behaviour of the compiler with your own. You cannot override existing built-in operators for comparisons, conversions, arithmetic, and so on. That's by design; it's so that someone can read your code and know that int x = M(); int y = x + 2; does integer arithmetic, as opposed to, say, formatting your hard disk.

Can you explain why you want to do this? Perhaps there is a better way to do what you want.

Eric Lippert