jrliv
5/19/2017 - 12:09 PM

Example of how to compare string (reference) objects.

Example of how to compare string (reference) objects.

string str = "beer";
string secStr = str;
string thirdStr = "bee";
thirdStr = thirdStr + 'r';
string fourthStr = "more beer";

Console.WriteLine("str = {0}", str);	// str = beer
Console.WriteLine("secStr = {0}", secStr);	// secStr = beer
Console.WriteLine("thirdStr = {0}", thirdStr);	// thirdStr = beer
Console.WriteLine("fourthStr = {0}", fourthStr);	// fourthStr = more beer
Console.WriteLine(str == secStr);	// True - same object
Console.WriteLine(str == thirdStr);	// True - equal objects
Console.WriteLine((object)str == (object)secStr);	// True
Console.WriteLine((object)str == (object)thirdStr);	// False - different
Console.WriteLine((object)str == (object)fourthStr);	// False - different