string a="I am comparing 2 string";
string b="I am comparing 2 string";
if(a==b)
return true;
else
return false;
How does a .NET compiler compare two strings? Does a string work like a struct(int)?
string is class so a=b means we are comparing 2 object, but i want to compare 2 values.
The String class overloads the
==operator, so yes it compares the values of the strings, just like comparing value types likeint.(On a side note, the compiler also interns literal strings in the code, so the string variables
aandbwill actually be referencing the same string object. If you useObject.ReferenceEquals(a,b)it will also returntrue.)