C# string surprise

My jaw still aches remembering the pain of dropping to the floor when Visual Studio refused to compile

    if (someString >= anotherString)

It complains in no uncertain terms. In fact, it drives the message home by repeating the message, albeit with a different word order.

Operator ‘>=’ cannot be applied to operands of type ‘string’ and ‘string’
Cannot apply operator ‘>=’ to operands of type ‘string’ and ‘string’

What the …?

Coming from Delphi, typing comparisons like that is in my muscles. It will simply do a character by character equality comparison ending at the shortest length or the first mismatch. Only if you want anything more intelligent, like case insensitivity or locale-awareness, do you resort to specialized comparison functions.

So why does C# complain?

Why does it make me use string.Compare when that doesn’t force me to specify ignoreCase or a ComparisonType? After all, C# does overload the == operator. So why not the other comparison operators?

I just don’t get it.

Anybody care to shed light on this?

Leave a Reply

Your email address will not be published. Required fields are marked *


Show Buttons
Hide Buttons