Equal function and equal operator:
The differences between Equal function and == operator are:
== Operator is used to compare the value type object. It cannot compare the reference type objects. Because == compare two same values and if these both values are same then it will return true otherwise it will return false. But on the other hand Equal function compare both by value and by reference. It will return true if both values are same or the references pointing to same objects.
String comparison using equal function and equal operator:
To understand that how we can compare a string using equal function and equal operator let us consider this example. In this example i am going to compare two strings by value and reference. If we compare two value type strings which are same both will return true and if we compare these strings by reference then they will return different answer. The == returns false and Equal method will return true because == compare same values and Equals method compare both by value and by reference.
To demonstrate make a console application and write the following code.
staticvoid Main(string args)
stringmystring = "Friend";
string mystring1 = "Friend";
stringresult = mystring.Equals(mystring1);
stringresult = mystring == mystring1;
StringBuilder s1 = newStringBuilder("yes");
StringBuilder s2 = newStringBuilder("yes");
stringresult = s1.Equals(s2);
stringresult = s1 == s2;
The output of program will be
String comparison using equal function and equal operator in