C# – Why strings does not compare references

c++operatorsreferencestring

I know it is special case but why == between strings returns if their value equals and not when their reference equals. Does it have something to do with overlloading operators?

Best Solution

The == operator is overloaded in String to perform value equality instead of reference equality, indeed. The idea is to make strings more friendly to the programmer and to avoid errors that arise when using reference equality to compare them (not too uncommon in Java, especially for beginners).

So far I have never needed to compare strings by reference, to be honest. If you need to do it you can use object.ReferenceEquals().