CopyPastehas never been so tasty!

String comparison using equal function and equal operator in

by anonymous

  • 0
  • 0
  • 0
61 views

Orignal Source:http://codingvilla.com/string-comparison-csharp-single-article-609.aspx 

 

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.

 

 

Code:

 

 

classcomparison_equal_function_and_operator

    {

staticvoid Main(string[] args)

        {

try

            {

stringmystring = "Friend";

string mystring1 = "Friend";

boolstringresult;

stringresult = mystring.Equals(mystring1);

Console.WriteLine(stringresult);

stringresult = mystring == mystring1;

Console.WriteLine(stringresult);

StringBuilder s1 = newStringBuilder("yes");

StringBuilder s2 = newStringBuilder("yes");

stringresult = s1.Equals(s2);

Console.WriteLine(stringresult);

stringresult = s1 == s2;

Console.WriteLine(stringresult);

Console.ReadLine();

            }

catch(Exceptionexce)

            {

Console.WriteLine(exce);

            }

        }

    }

 

 

Output:

                 

       The output of program will be

 

True

 

True

 

True

 

False

 

Add A Comment: