Equals () and = = in C #

Source: Internet
Author: User

usingSystem;namespaceequalstest{classEqualstest {Static voidMain (string[] args) {            //value type            intx =1; inty =1; Console.WriteLine (x= = y);//TrueConsole.WriteLine (X.equals (y));//True//Reference typeA A =NewA (); b b=NewB (); //Console.WriteLine (a==b);//ErrorConsole.WriteLine (A.equals (b));//False//string (the reference type that most resembles the value type)            stringm ="1"; stringn ="1"; Console.WriteLine (M==N);//TrueConsole.WriteLine (M.equals (n));//True//struct (value type most like a reference type)T t =NewT (); V v=NewV (); //Console.WriteLine (t = = v);//ErrorConsole.WriteLine (T.equals (v));//TrueConsole.read (); }    }    classA Public intX =1; } classB Public intX =1; } structTintX;} structVintX;}}

Summarize:

① two "Same" value types either equals () or = = Returns true, and two "same" value types, whether equals () or = =, return false.

② the above sentence is not entirely correct, because there is a special case in the value type: struct; there is a special case in the reference type: a string.

③ except the string reference type, as well as the structure, can not use = = to compare, compile will be error.

Equals () and = = in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.