{"id":40895,"date":"2023-12-11T14:51:59","date_gmt":"2023-12-11T06:51:59","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=40895"},"modified":"2023-12-11T14:51:59","modified_gmt":"2023-12-11T06:51:59","slug":"c%e4%b8%adequal%e4%b8%8e%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/c%e4%b8%adequal%e4%b8%8e%e7%9a%84%e5%8c%ba%e5%88%ab\/","title":{"rendered":"C#\u4e2dequal\u4e0e==\u7684\u533a\u522b"},"content":{"rendered":"<p>&#8220;`&#8221;                    \u7b54\uff1a<\/p>\n<p>C#\u4e2d\uff0c\u5224\u65ad\u76f8\u7b49\u6709\u4e24\u79cd\u65b9\u5f0f\uff0c\u4e00\u79cd\u662f\u4f20\u7edf\u7684==\u64cd\u4f5c\uff0c\u4e00\u79cd\u662fobject\u63d0\u4f9b\u7684Equals\u65b9\u6cd5\u3002<\/p>\n<p>\u4e8c\u8005\u7684\u533a\u522b\u5728\u4e8e\uff1a<br \/>\n &lt;strong&gt;\u4e00\u3001==\u64cd\u4f5c\u7b26\u5224\u65ad\u7684\u662f\u5806\u6808\u4e2d\u7684\u503c\uff0cEqulas\u5224\u65ad\u7684\u662f\u5806\u4e2d\u7684\u503c&lt;\/strong&gt;<br \/>\n C#\u63d0\u4f9b\u503c\u7c7b\u578b\u548c\u5f15\u7528\u7c7b\u578b\uff0c\u503c\u7c7b\u578b\u5b58\u50a8\u5728\u6808\u4e0a\uff0c\u6545\u7528==\u5224\u65ad\u662f\u76f4\u63a5\u5224\u65ad\u5176\u503c\u662f\u5426\u76f8\u7b49\uff0c\u56e0\u4e3a\u503c\u7c7b\u578b\u4e0d\u5b58\u5728\u5806\u4e2d\u7684\u6570\u636e\uff0c\u56e0\u6b64\u503c\u7c7b\u578b\u7684Equals\u4e5f\u662f\u5224\u65ad\u6570\u636e\u3002\u5373\uff0c\u5bf9\u4e8e\u503c\u7c7b\u578b\u800c\u8a00\uff0c==\u4e0eEquals\u76f8\u540c\uff0c\u5747\u662f\u5224\u65ad\u5176\u503c\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<p>\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\u800c\u8a00\uff0c\u5176\u6808\u4e2d\u5b58\u50a8\u7684\u662f\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u90a3\u4e48==\u5c31\u662f\u6bd4\u8f83\u4e24\u4e2a\u5730\u5740\u662f\u5426\u76f8\u7b49\uff0c\u5373\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff1bEquals\u51fd\u6570\u5219\u662f\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u5728\u5806\u4e2d\u7684\u6570\u636e\u662f\u5426\u4e00\u6837\uff0c\u5373\u4e24\u4e2a\u5f15\u7528\u7c7b\u578b\u662f\u5426\u662f\u5bf9\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u5f15\u7528\u3002<br \/>\n &lt;strong&gt;\u4e8c\u3001String\u7c7b\u578b\u7279\u6b8a&lt;\/strong&gt;<br \/>\n String\u7c7b\u578b\u867d\u7136\u662f\u5f15\u7528\u7c7b\u578b\uff0c\u4f46\u662f\u5bf9String\u5bf9\u8c61\u7684\u8d4b\u503c\u5374\u6309\u7167\u503c\u7c7b\u578b\u64cd\u4f5c\u3002<br \/>\n \u4f8b\u5982\uff1a<br \/>\n String s1=&quot;&quot;hello&quot;&quot;;<br \/>\n String s2=&quot;&quot;hello&quot;&quot;;<br \/>\n \u5bf9s2\u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u5e76\u6ca1\u6709\u91cd\u65b0\u5f00\u8f9f\u5185\u5b58\uff0c\u800c\u662f\u76f4\u63a5\u5c06\u5176\u5730\u5740\u6307\u5411s1\u7684\u5185\u5bb9\u201chello\u201d\u3002\u8fd9\u6837\u4e00\u6765\uff0cstring\u7c7b\u578b\u867d\u7136\u662f\u5f15\u7528\u7c7b\u578b\uff0c\u4f46\u662f\u5176==\u64cd\u4f5c\u548cEquals\u64cd\u4f5c\u90fd\u662f\u4e00\u6837\u7684\uff0c\u5747\u6bd4\u8f83\u503c\u662f\u5426\u76f8\u7b49\u3002<br \/>\n &lt;strong&gt;\u4e09\u3001\u4e0eGetHashCode()\u7684\u5173\u7cfb&lt;\/strong&gt;<br \/>\n \u82e5\u4e24\u5bf9\u8c61Equals\u76f8\u7b49\uff0c\u90a3\u4e48\u5176GetHashCode()\u5fc5\u5b9a\u76f8\u7b49\uff1b\u4f46\u662f\u53cd\u8fc7\u6765\uff0c\u82e5GetHashCode()\u76f8\u7b49\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5bf9\u8c61Equals\u65b9\u6cd5\u6bd4\u8f83\u7ed3\u679c\u4e0d\u4e00\u5b9a\u76f8\u540c\u3002\uff08\u4e3a\u4e86\u83b7\u53d6\u6700\u4f73\u6027\u80fd\uff0chash\u51fd\u6570\u4e3a\u5bf9\u8c61\u5185\u5bb9\u751f\u6210\u7684\u6570\u5b57\u662f\u968f\u673a\u5206\u5e03\u7684\uff0c\u8fd9\u5c31\u610f\u5473\u7740\uff0c\u5185\u5bb9\u4e0d\u540c\u7684\u5bf9\u8c61\uff0c\u6709\u53ef\u80fd\u751f\u6210\u7684\u6570\u5b57\u662f\u4e00\u6837\u7684\uff0c\u4f46\u53ef\u4ee5\u8ba4\u4e3a\u8fd9\u79cd\u6982\u7387\u975e\u5e38\u5c0f\uff09<br \/>\n \u4e0b\u9762\u793a\u4f8b\u8bf4\u660e\uff1a<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-csharp&quot;&quot; lang=&quot;&quot;csharp&quot;&quot;&gt;using System;<br \/>\nusing System.Collections.Generic;<br \/>\nusing System.Linq;<br \/>\nusing System.Text;<\/p>\n<p>namespace ConsoleApplication1<br \/>\n{<br \/>\n    class People<br \/>\n    {<br \/>\n        private string name;<\/p>\n<p>        public string Name<br \/>\n        {<br \/>\n            get { return name; }<br \/>\n            set { name = value; }<br \/>\n        }<\/p>\n<p>        public People(string name)<br \/>\n        {<br \/>\n            this.name = name;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    class Program<br \/>\n    {<br \/>\n        static void Main(string[] args)<br \/>\n        {<br \/>\n           string a = &quot;&quot;hello&quot;&quot;;<br \/>\n            string b = new string(new char[] { &#039;h&#039;, &#039;e&#039;, &#039;l&#039;, &#039;l&#039;, &#039;o&#039; });<br \/>\n            Console.WriteLine(a == b); \/\/True<br \/>\n            Console.WriteLine(a.Equals(b));\/\/True<br \/>\n            Console.WriteLine(&quot;&quot;*********&quot;&quot;);<br \/>\n            int m = 3;<br \/>\n            int n = 3;<br \/>\n            Console.WriteLine(n == m);\/\/True<br \/>\n            Console.WriteLine(n.Equals(m));\/\/True<br \/>\n            Console.WriteLine(&quot;&quot;*********&quot;&quot;);<br \/>\n            object g = a;<br \/>\n            object h = b;<br \/>\n            Console.WriteLine(g == h);\/\/False<br \/>\n            Console.WriteLine(g.Equals(h));\/\/True<br \/>\n            Console.WriteLine(g.GetHashCode() + &quot;&quot;   &quot;&quot; + h.GetHashCode()); \/\/1025947313   1025947313<br \/>\n            Console.WriteLine(&quot;&quot;*********&quot;&quot;);<br \/>\n            People p1 = new People(&quot;&quot;Jimmy&quot;&quot;);<br \/>\n            People p2 = new People(&quot;&quot;Jimmy&quot;&quot;);<br \/>\n            Console.WriteLine(p1 == p2); \/\/False<br \/>\n            Console.WriteLine(p1.Equals(p2)); \/\/False<br \/>\n            Console.WriteLine(p1.GetHashCode() + &quot;&quot;  &quot;&quot; + p2.GetHashCode());\/\/43495525  55915408<br \/>\n            Console.WriteLine(&quot;&quot;*********&quot;&quot;);<br \/>\n            People p3 = new People(&quot;&quot;Jimmy&quot;&quot;);<br \/>\n            People p4 = p3;<br \/>\n            Console.WriteLine(p3 == p4);\/\/True<br \/>\n            Console.WriteLine(p3.Equals(p4));\/\/True<\/p>\n<p>        }<br \/>\n    }<br \/>\n}<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u5728C#\u4e2d\uff0cstring \u7c7b\u578b\u7684\u7279\u70b9\u6709\uff1a<br \/>\n \uff081\uff09\u5c5e\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff1b<br \/>\n \uff082\uff09\u662f\u5f15\u7528\u7c7b\u578b\uff1b<br \/>\n \uff083\uff09\u53ea\u8bfb\uff1b<br \/>\n \uff084\uff09 string a=&quot;&quot;1123&quot;&quot;;   string b=&quot;&quot;1123&quot;&quot;;<br \/>\n \u90a3\u4e48a\u548cb\u6307\u5411\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff1b<br \/>\n \u4f46\u662f\u5e76\u975e2\u4e2a\u76f8\u540c\u503c\u76f8\u7b49\u7684\u5b57\u7b26\u4e32\u5c31\u5bf9\u5e94\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff1b<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; \u7b54\uff1a C#\u4e2d\uff0c\u5224\u65ad\u76f8\u7b49\u6709\u4e24\u79cd\u65b9\u5f0f\uff0c\u4e00\u79cd\u662f\u4f20\u7edf\u7684==\u64cd\u4f5c\uff0c\u4e00\u79cd\u662fobject\u63d0\u4f9b [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-40895","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/40895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=40895"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/40895\/revisions"}],"predecessor-version":[{"id":40896,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/40895\/revisions\/40896"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=40895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=40895"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=40895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}