locked
Do you know what's the different between "equals" and "==" RRS feed

  • Question

  • Hi,
    Do you know what's the different between "equals" and "=="?

    Friday, May 13, 2005 3:40 PM

Answers

  • Equals is a virtual method inherited from System.Object. All objects in .NET have an Equals method because all are derived from System.Object. Some classes override the == operator via public static bool operator==(Complex c1, Complex c2) where Complex is a Complex numbers class that I've implemented. (i.e. Complex is any class that you want to implement == for.) This allows you to write:

    Complex c1 = new Complex(1, 2);
    Complex c2 = new Complex(1, 2);
    if(c1 == c2) {
      Console.WriteLine("The numbers are equal!");
    } else {
      Console.WriteLine("Something went horribly wrong!");
    }

    Equals and == should return the same result. Users of your class will be terribly confused if they return different results. As such, the operators typically are implemented in terms of Equals. For instance:

    public static bool operator==(Complex c1, Complex c2) {
      return c1.Equals(c2);
    }

    public static bool operator!=(Complex c1, Complex c2) {
      return !c1.Equals(c2);
    }

    Friday, May 13, 2005 5:39 PM
  • Hello,

    Equals vs. C#'s operator

    Check it out on this page:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnguinet/html/drguinet4_update.asp

    Hope this helps,

    Chad

    Friday, May 13, 2005 6:00 PM

All replies

  • Equals is a virtual method inherited from System.Object. All objects in .NET have an Equals method because all are derived from System.Object. Some classes override the == operator via public static bool operator==(Complex c1, Complex c2) where Complex is a Complex numbers class that I've implemented. (i.e. Complex is any class that you want to implement == for.) This allows you to write:

    Complex c1 = new Complex(1, 2);
    Complex c2 = new Complex(1, 2);
    if(c1 == c2) {
      Console.WriteLine("The numbers are equal!");
    } else {
      Console.WriteLine("Something went horribly wrong!");
    }

    Equals and == should return the same result. Users of your class will be terribly confused if they return different results. As such, the operators typically are implemented in terms of Equals. For instance:

    public static bool operator==(Complex c1, Complex c2) {
      return c1.Equals(c2);
    }

    public static bool operator!=(Complex c1, Complex c2) {
      return !c1.Equals(c2);
    }

    Friday, May 13, 2005 5:39 PM
  • Hello,

    Equals vs. C#'s operator

    Check it out on this page:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnguinet/html/drguinet4_update.asp

    Hope this helps,

    Chad

    Friday, May 13, 2005 6:00 PM
  • I guess it's preety straight Forward . = is used to assign some value of calculation to some variable such as
    int Rate=7;
    and == is used when you want to sompare two differante variable with the same data type other then string. in string when you want to compare the two object the the synext will be somthing like
    string n1="Test";
    srting n2="Test";
    if(n1.Equals(n2))
    {
    }
    else
    {
    printf("Not Equals");


    Bye
    Saturday, May 14, 2005 5:51 AM
  • No, you *can* use == on string variables, as System.String overrides == operator (note that both sides must be string variables, otherwise System.Object's == operator is called, which does a reference comparison)


     string s1="Test";
    string s2="Tes";
    s2+="t"; /*prevent string interning*/

    Console.WriteLine((s1==s2)) //prints True

     


    Saturday, May 14, 2005 7:14 PM