C # String operator overload

G'Day Mates -

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

I tried five ways on Sunday and I get various errors - my best shot is to declare a partial class and overload from there, but for some reason this will not work.

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; } } 

}

+7
c # operator-overloading
source share
6 answers

A string is a private class. You cannot inherit it, and without the source for String, you cannot compile its partial class. Even if you got access to the source (this is possible using Reflector or by loading the Visual Studio symbol), you will still have problems, as he is a first-class citizen at runtime.

Do you really need <and> as operators on a line? 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 
+16
source share

It is not possible to replace the compiler's native behavior with your own. You cannot override existing built-in operators for comparison, conversion, arithmetic, etc. It is by design; so that someone can read your code and know that int x = M(); int y = x + 2; int x = M(); int y = x + 2; Performs integer arithmetic, unlike, for example, formatting your hard drive.

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

+15
source share

The simple answer is: you cannot; There is no way to modify statements for another class. Partial classes are allowed only for classes declared as partial in all files and defined in the same assembly.

+10
source share

Do you mean the System.String class? This is not possible in C #. You cannot add extension operators to an existing class. This is a very popular feature.

+5
source share
  • You cannot have a partial class for a string, because the string class itself is not partial and therefore will not work with your partial class.

  • The string is sealed, so you can inherit it and then overload the statement.

In short, alas, you cannot do what you need to do.

I don’t know what you are trying to do for sure, I can’t offer a good alternative. But take a look at extension methods that are often suitable for situations. Leaving aside if you need :), you can add the IsGreaterThan method to the string class and return true or false, as you wish. This is good because you can give the extension method a name that makes it understandable, keeps existing operators in tact (which you have no choice anyway), and allows fast / simple code.

+3
source share

You cannot directly overload the operator> = and <=, but you can achieve the same result by overloading> and == separately.

Your code seems right to me, except that you skip overloading for ==. C>

I seem to be wrong, but you can always get back to thinking. I think that if you do some digging and hack, you can make it work with reflection to expand the class, since reflection allows you to add functions or exchange function bodies at runtime.

Whether this is appropriate and good practice, I doubt it. There is a reason the class is sealed. Doing what I mentioned can lead to undefined behavior in some cases due to some assumptions that the .net structure makes in strings. The chances are great that the row class will collapse internally.

+1
source share

All Articles