R
rossum
I was comparing strings, as one does:
stringA.CompareTo(stringB) == -1
Then I thought that this was not as easy to read as it could be,
wouldn't it be clearer to write:
stringA < stringB
Of course that doesn't compile since the < operator is not defined
between strings. Easy enough I thought, I can write the < operator:
static public bool operator <(string a, string b)
{ return a.CompareTo(b) == -1; }
of course I also need the > operator as well:
static public bool operator >(string a, string b)
{ return a.CompareTo(b) == +1; }
The problem is that neither of these will compile since neither
parameter is of the enclosing class and they have to be inside a
class.
My next idea was to derive a new string class which added the two
operators. No joy, String is sealed so I can't derive from it.
I could write a whole string class of my own from scratch but that
seems to be overkill for this problem. Is there any other solution
out there?
Thanks,
rossum
The ultimate truth is that there is no ultimate truth
stringA.CompareTo(stringB) == -1
Then I thought that this was not as easy to read as it could be,
wouldn't it be clearer to write:
stringA < stringB
Of course that doesn't compile since the < operator is not defined
between strings. Easy enough I thought, I can write the < operator:
static public bool operator <(string a, string b)
{ return a.CompareTo(b) == -1; }
of course I also need the > operator as well:
static public bool operator >(string a, string b)
{ return a.CompareTo(b) == +1; }
The problem is that neither of these will compile since neither
parameter is of the enclosing class and they have to be inside a
class.
My next idea was to derive a new string class which added the two
operators. No joy, String is sealed so I can't derive from it.
I could write a whole string class of my own from scratch but that
seems to be overkill for this problem. Is there any other solution
out there?
Thanks,
rossum
The ultimate truth is that there is no ultimate truth