02 November 2017
String.Compare is a better method to compare strings than String.Equal
String.Compare is a better method to compare strings than String.Equal
Below example illustrates how String.Compare handles nulls better than String.Equal
class Program
{
static void Main(string[] args)
{
string A = "Hello World";
string B = "Hello World";
string C = null;
if (String.Compare(B, A) == 0) { Console.WriteLine("A equals B"); }
if (String.Compare(C, A) == 0) { Console.WriteLine("A equals C"); }
if (B.Equals(A)) { Console.WriteLine("A equals B"); }
try
{
if (C.Equals(A)) { Console.WriteLine("A equals C"); }
}
catch (Exception e)
{
Console.Write(e.Message);
}
Console.ReadKey();
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment