In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you the difference between Equals, = = and RefrenceEquals. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
We will briefly compare the differences between Equals, =, and RefrenceEquals here, and we will analyze them from the comparison of reference types and types.
Today, I came across a post about the difference between Equals and =, which is simply described as a comparison of quotes and comparison values, which is very problematic.
I took a look at MSDN and summarized it as follows.
Talk about the difference between the three from two aspects.
First give a class and a structure:
Public class RefrenceClass {public int X {get;set;} public string Y {get;set;}} public struct ValueStruct {public int x; public string y;}
1. Comparison of reference types
See what the following code will output.
RefrenceClass C1 = new RefrenceClass (); c1.X = 1; c1.Y = "OK"; RefrenceClass c2 = new RefrenceClass (); c2.X = 1; c2.Y = "OK"; RefrenceClass c3 = new RefrenceClass (); c3.X = 1; c3.Y = "OK!" RefrenceClass c4 = new RefrenceClass (); c4.X = 2; c4.Y = "OK"; RefrenceClass c5 = new RefrenceClass (); c5cm c1; c5.X = 2; RefrenceClass c6 = C1; Console.WriteLine (ReferenceEquals (C1 Magneto c2). ToString () Console.WriteLine (ReferenceEquals (C1 ~ c3). ToString (); Console.WriteLine (ReferenceEquals (C1 ~ c4). ToString ()); Console.WriteLine (ReferenceEquals (C1 ~ c5). ToString ()); Console.WriteLine (ReferenceEquals (C1 ~ (c6)). ToString (); Console.WriteLine (); Console.WriteLine (Equals (C1 ~ (c2)). ToString () Console.WriteLine (Equals (C1 ~ c3). ToString (); Console.WriteLine (Equals (C1 ~ c4). ToString ()); Console.WriteLine (Equals (C1 ~ c5). ToString ()); Console.WriteLine (Equals (C1 ~ (c6)). ToString (); Console.WriteLine (); Console.WriteLine (c1.Equals (c2). ToString ()) Console.WriteLine (c1.Equals (c3). ToString (); Console.WriteLine (c1.Equals (c4). ToString ()); Console.WriteLine (c1.Equals (c5). ToString ()); Console.WriteLine (c1.Equals (c6). ToString ()); Console.WriteLine (); Console.WriteLine (c1==c2) Console.WriteLine (c1==c3); Console.WriteLine (c1==c4); Console.WriteLine (c1==c5); Console.WriteLine (c1==c6); Console.WriteLine ()
The returned results are false for the first three comparisons and true for the last two.
As a result, static RefrenceEquals, static Equals, virtual Equals and = = are all reference addresses
When we compare Struct, the result of static RefrenceEquals is false, while the result of static Equals and virtual Equals is that true,false,false,false,true,== is not available. From this result, static Equals and virtual Equals compare values.
This is different from the results we got above, so what exactly is the comparison?
We analyze it by type:
1. Static RefrenceEquals, you can also see from the method name that the reference address is compared, and when comparing value types, it is always false, even if it is RefrenceEauals (1 null,null 1), because here it boxed the values first and then compared them, so the reference addresses of the two are different, but the value of RefrenceEquals (null,null) is true.
two。 Static Equals method, which actually ends up calling different overloads of the virtual Equals method
3. Virtual Equals method, which can be overloaded in different classes, here we can understand why the comparison of two different types of variables above will have different results. For example, the String.Equals method is used to determine whether the contents of two strings are equal.
In general, for the value type, the type is the same, and the value is the same (it must be the same for every member of struct), then Equals returns true, otherwise it returns false. For reference types, the default behavior is the same as that of ReferenceEquals, returning true only if two objects point to the same Reference. One advantage of static Equals over the virtual Equals method is that it does not have to consider whether the object to be compared is null.
4. Value = operator, for built-in value types, directly determine whether the values of two objects are equal, and convert objects as needed. User-defined value types, such as struct, are not available; du Yu reference type, the default behavior is the same as that of ReferenceEquals, but many classes overload =, such as String.
What is the difference between Equals, = = and RefrenceEquals? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.