How do u find out if 2 strings are the same in c#?
String manipulation
Collapse
X
-
Do you mean have the same contents, or the same reference?Originally posted by napstarHow do u find out if 2 strings are the same in c#?
Contents:
string hello1 = "Hello";
string hello2 = "Hello";
Console.WriteLi ne((hello1==hel lo2) + "");
should give you true (or 1, whatever, can't remember what .net gives us again)
but
Console.WriteLi ne(object.Refer enceEquals(hell o1,hello2) + "");
may give you false because it checks to see if hello1 and hello2 are referencing the same object in memory.
I think in this case both return true because of the way the compiler deals with constant strings... but it's easy to image a situation where it wouldn't
-mwalts
Comment