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 #