{"id":3264,"date":"2023-05-26T01:29:17","date_gmt":"2023-05-25T17:29:17","guid":{"rendered":"https:\/\/lonelinerd.com\/?p=3264"},"modified":"2023-06-14T01:22:53","modified_gmt":"2023-06-13T17:22:53","slug":"equality","status":"publish","type":"post","link":"https:\/\/lonelinerd.com\/index.php\/2023\/05\/26\/equality\/","title":{"rendered":"C#\u7b46\u8a18 \u2013\u00a0\u5c0d\u8c61\u76f8\u7b49\u6027\u548c\u540c\u4e00\u6027"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"3264\" class=\"elementor elementor-3264\">\n\t\t\t\t\t\t<div class=\"elementor-inner\">\n\t\t\t\t<div class=\"elementor-section-wrap\">\n\t\t\t\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-d3d2720 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"d3d2720\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t\t\t<div class=\"elementor-row\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-e62deba\" data-id=\"e62deba\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-column-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t<div class=\"elementor-widget-wrap\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-125875f elementor-widget elementor-widget-text-editor\" data-id=\"125875f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<h5 style=\"font-style: normal;\"><span style=\"font-weight: bold;\">\u5c0d\u8c61\u76f8\u7b49\u6027\u548c\u540c\u4e00\u6027<\/span><\/h5><ul style=\"font-size: 15px; font-style: normal; font-weight: 400; margin-bottom: 1.5em; margin-left: 3em;\" data-mark=\"-\"><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">System.Object\u63d0\u4f9b\u4e86Equals\u865b\u65b9\u6cd5\uff0c\u5176\u5be6\u73fe\u70ba\uff1a<\/p><ul style=\"font-size: 15px;\" data-mark=\"-\"><li style=\"font-size: 15px;\"><pre lang=\"C#\" spellcheck=\"false\"><span role=\"presentation\">public class Object<\/span><br \/><span role=\"presentation\">{<\/span><br \/><span role=\"presentation\"> \u00a0 \u00a0public virtual bool Equals(Object obj)<\/span><br \/><span role=\"presentation\"> \u00a0  {<\/span><br \/><span role=\"presentation\"> \u00a0 \u00a0 \u00a0 \u00a0if(this == obj) { return true; }<\/span><br \/><span role=\"presentation\"> \u00a0 \u00a0 \u00a0 \u00a0return false;<\/span><br \/><span role=\"presentation\"> \u00a0  }<\/span><br \/><span role=\"presentation\">}<\/span><\/pre><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u8a72\u5be6\u73fe\u53ea\u662f\u5224\u65b7\u300c\u5c0d\u8c61\u300d\u662f\u5426\u8207\u81ea\u8eab\u5305\u542b\u4e00\u6a23\u7684\u503c\uff0c\u662f\u300c\u540c\u4e00\u6027\u300d\uff0c\u800c\u4e0d\u662f\u300c\u76f8\u7b49\u6027\u300d<\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\"><span spellcheck=\"false\"><span style=\"color: red;\">\u53e6\u5916\uff0c\u7531\u65bc\u985e\u578b\u80fd\u91cd\u5bebObject\u7684Equals\u65b9\u6cd5\uff0c\u56e0\u6b64\u4e0d\u80fd\u518d\u901a\u904eEquals\u4f86\u6e2c\u8a66\u540c\u4e00\u6027\uff1b\u540c\u7406 == \u64cd\u4f5c\u7b26\u4e5f\u80fd\u88ab\u91cd\u8f09\u3002\u56e0\u6b64\u60f3\u8981\u5b89\u5168\u5730\u6e2c\u8a66\u540c\u4e00\u6027\uff0c\u61c9\u4f7f\u7528ReferenceEquals<\/span><\/span><\/p><\/li><\/ul><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u6b63\u78ba\u7684\u76f8\u7b49\u6027\u5be6\u73fe\u61c9\u8a72\u5305\u542b\uff1a<\/p><ul style=\"font-size: 15px;\" data-mark=\"-\"><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u53c3\u6578\u5c0d\u8c61<span spellcheck=\"false\"><span style=\"color: red;\">\u7a7a\u5224\u65b7<\/span><\/span><\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u81ea\u8eab\u5c0d\u8c61\u8207\u53c3\u6578\u5c0d\u8c61\u7684<span spellcheck=\"false\"><span style=\"color: red;\">\u5f15\u7528\u540c\u4e00\u6027<\/span><\/span><\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\"><span spellcheck=\"false\"><span style=\"color: red;\">\u81ea\u8eab\u5c0d\u8c61\u8207\u53c3\u6578\u5c0d\u8c61\u4e2d\u7684\u6bcf\u500b\u5be6\u4f8b\u5b57\u6bb5\u7684\u503c\u6bd4\u8f03<\/span><\/span><\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\"><span spellcheck=\"false\"><span style=\"color: red;\">\u8abf\u7528\u57fa\u985e\u7684Equals\u65b9\u6cd5<\/span><\/span><\/p><\/li><li style=\"font-size: 15px;\"><pre lang=\"C#\" spellcheck=\"false\"><span role=\"presentation\">public class Object<\/span><br \/><span role=\"presentation\">{<\/span><br \/><span role=\"presentation\"> \u00a0 \u00a0public virtual bool Equals(Object obj)<\/span><br \/><span role=\"presentation\"> \u00a0  {<\/span><br \/><span role=\"presentation\"> \u00a0 \u00a0 \u00a0 \u00a0if(obj == null) { return false; }<\/span><br \/><span role=\"presentation\"> \u00a0 \u00a0 \u00a0 \u00a0if(this.GetType() != obj.GetType()) { return false; }<\/span><br \/><span role=\"presentation\"> \u00a0 \u00a0 \u00a0 \u00a0return true;<\/span><br \/><span role=\"presentation\"> \u00a0  }<\/span><br \/><span role=\"presentation\">}<\/span><\/pre><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u5be6\u969b\u4e0a\uff0cSystem.ValueType\u5c31\u9032\u884c\u4e86\u6b63\u78ba\u7684\u76f8\u7b49\u6027\u6e2c\u8a66\u5be6\u73fe<\/p><ul style=\"font-size: 15px;\" data-mark=\"-\"><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u5224\u65b7\u5c0d\u8c61\u662f\u5426\u70ba\u7a7a<\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u5224\u65b7\u5f15\u7528\u5c0d\u8c61\u985e\u578b\u662f\u5426\u4e00\u81f4<\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\"><span spellcheck=\"false\"><span style=\"color: red;\">\u5229\u7528\u53cd\u5c04\uff0c\u5c0dthis\u5c0d\u8c61\u548cobj\u5c0d\u8c61\u4e2d\u7684\u6240\u6709\u503c\u9032\u884c\u6bd4\u8f03<\/span><\/span><\/p><ul style=\"font-size: 15px;\" data-mark=\"-\"><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">CLR\u7684\u53cd\u5c04\u6a5f\u5236\u6162\uff0c\u56e0\u6b64\u81ea\u5b9a\u7fa9\u503c\u985e\u578b\u6642\u61c9<span spellcheck=\"false\"><span style=\"color: red;\">\u91cd\u5bebEquals\u65b9\u6cd5\uff0c\u800cEquals\u65b9\u6cd5\u61c9\u7b26\u5408\u76f8\u7b49\u6027\u76844\u500b\u7279\u5fb5\uff1a<\/span><\/span><\/p><ul style=\"font-size: 15px;\" data-mark=\"-\"><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\"><span spellcheck=\"false\"><span style=\"color: red;\">\u81ea\u53cd\uff1ax.Equals(x)\u5fc5\u7136\u70batrue<\/span><\/span><\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\"><span spellcheck=\"false\"><span style=\"color: red;\">\u5c0d\u7a31\uff1ax.Equals(y) = y.Equals{x}<\/span><\/span><\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\"><span spellcheck=\"false\"><span style=\"color: red;\">\u53ef\u50b3\u905e\uff1ax.Equals(y) = true; y.Equals(z) = true; then x.Equals(z) = true<\/span><\/span><\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\"><span spellcheck=\"false\"><span style=\"color: red;\">\u4e00\u81f4\uff1a\u6bd4\u8f03\u7684\u5169\u500b\u503c\u4e0d\u8b8a\uff0c\u5176\u8fd4\u56de\u503c\u4e5f\u4e0d\u8b8a<\/span><\/span><\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\"><span spellcheck=\"false\"><span style=\"color: red;\">\u53e6\u5916\uff0c\u53ef\u4ee5\u901a\u904e\u5be6\u73feIEquatable<\/span><\/span><span spellcheck=\"false\"><span style=\"color: red;\">\u63a5\u53e3\u6216\u91cd\u8f09==\u548c!=\u64cd\u4f5c\u7b26\u4f86\u78ba\u4fdd\u5167\u90e8\u8abf\u7528\u985e\u578b\u5b89\u5168\u7684Equals\u65b9\u6cd5<\/span><\/span><\/p><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><\/ul><h5 style=\"font-style: normal;\"><span style=\"font-weight: bold;\">\u5c0d\u8c61\u54c8\u5e0c\u78bc<\/span><\/h5><ul style=\"font-size: 15px; font-style: normal; font-weight: 400; margin-bottom: 1.5em; margin-left: 3em;\" data-mark=\"-\"><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">System.GetHashCode\u7372\u53d6\u4efb\u610f\u5c0d\u8c61\u7684int32\u54c8\u5e0c\u78bc<\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\"><span spellcheck=\"false\"><span style=\"color: red;\">\u5982\u679c\u91cd\u5beb\u4e86Equals\uff0c\u90a3\u5c31\u5fc5\u9808\u91cd\u5bebGetHashCode<\/span><\/span><\/p><ul style=\"font-size: 15px;\" data-mark=\"-\"><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u56e0\u70ba\u5728\u4e00\u4e9b\u96c6\u5408\uff08\u5982Dictionary\uff09\u7684\u5be6\u73fe\u4e2d\uff0c<span spellcheck=\"false\"><span style=\"color: red;\">\u8981\u6c42\u5169\u500b\u5c0d\u8c61\u5fc5\u9808\u5177\u6709\u76f8\u540c\u7684HashCode\u624d\u88ab\u8996\u70ba\u76f8\u7b49\uff0c\u6240\u4ee5\u91cd\u5bebEquals\u5c31\u5fc5\u9808\u91cd\u5bebGetHashCode\u3002<\/span><\/span><\/p><ul style=\"font-size: 15px;\" data-mark=\"-\"><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u5411\u96c6\u5408\u6dfb\u52a0KeyValuePair\u6642\uff0c\u9996\u5148\u8981\u7372\u53d6\u9375\u5c0d\u8c61\u7684HashCode\u3002\u8a72HashCode\u6307\u51fa\u9375\u503c\u5c0d\u8981\u5b58\u5132\u5230\u54ea\u500bbucket\u4e2d\u3002<\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u67e5\u627eKey\u6642\uff0c\u6703\u7372\u53d6\u6307\u5b9aKey\u5c0d\u8c61\u7684HashCode\u3002\u8a72HashCode\u6a19\u8b58\u8981\u641c\u7d22\u7684bucket\uff0c\u7136\u5f8c\u5728bucket\u4e2d\u67e5\u627e\u8207\u6307\u5b9aKey\u5c0d\u8c61\u76f8\u7b49\u7684Key\u5c0d\u8c61<\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u56e0\u6b64\uff0c\u4e00\u65e6\u4fee\u6539\u4e86\u96c6\u5408\u4e2d\u7684\u4e00\u500bKey\u5c0d\u8c61\uff0c\u96c6\u5408\u5c31\u518d\u4e5f\u627e\u4e0d\u5230\u8a72\u5c0d\u8c61<\/p><ul style=\"font-size: 15px;\" data-mark=\"-\"><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u6240\u4ee5\uff0c\u9700\u8981\u4fee\u6539HashTable\u4e2d\u7684Key\u5c0d\u8c61\u6642\uff0c\u61c9\u5148\u79fb\u9664\u539f\u4f86\u7684KeyValuePair\uff0c\u4fee\u6539\uff0c\u518d\u628a\u6539\u5b8c\u7684\u52a0\u56de\u53bb<\/p><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u54c8\u5e0c\u78bc\u7b97\u6cd5\u898f\u5247\uff1a<\/p><ul style=\"font-size: 15px;\" data-mark=\"-\"><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u63d0\u4f9b\u826f\u597d\u96a8\u6a5f\u5206\u5e03<\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u4e0d\u8981\u8abf\u7528Object\/ValueType\u7684GetHashCode<\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\"><span spellcheck=\"false\"><span style=\"color: red;\">\u81f3\u5c11\u4f7f\u7528\u4e00\u500b\u5be6\u4f8b\u5b57\u6bb5<\/span><\/span><\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\"><span spellcheck=\"false\"><span style=\"color: red;\">\u7b97\u6cd5\u4f7f\u7528\u7684\u5b57\u6bb5\u61c9\u8a72\u662f\u5728\u751f\u5b58\u671f\u4e0d\u53ef\u8b8a\u7684<\/span><\/span><\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\">\u5920\u5feb<\/p><\/li><li style=\"font-size: 15px;\"><p style=\"font-size: 15px;\"><span spellcheck=\"false\"><span style=\"color: red;\">\u5305\u542b\u76f8\u540c\u503c\u7684\u4e0d\u540c\u5c0d\u8c61\u61c9\u8fd4\u56de\u76f8\u540c\u7684HashCode<\/span><\/span><\/p><\/li><\/ul><\/li><\/ul>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-99ff984 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"99ff984\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t\t\t<div class=\"elementor-row\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-cabd04f\" data-id=\"cabd04f\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-column-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t<div class=\"elementor-widget-wrap\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-ff74079 elementor-widget-divider--view-line elementor-widget elementor-widget-divider\" data-id=\"ff74079\" data-element_type=\"widget\" data-widget_type=\"divider.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<div class=\"elementor-divider\">\n\t\t\t<span class=\"elementor-divider-separator\">\n\t\t\t\t\t\t<\/span>\n\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-bc2696c elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"bc2696c\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t\t\t<div class=\"elementor-row\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-b27ac8f\" data-id=\"b27ac8f\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-column-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t<div class=\"elementor-widget-wrap\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-2ac7bd1 elementor-widget elementor-widget-text-editor\" data-id=\"2ac7bd1\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<h4 class=\"md-end-block md-heading\"><span style=\"text-decoration: underline;\"><strong><span class=\"md-plain md-expand\">\u53c3\u8003\u66f8\u76ee<\/span><\/strong><\/span><\/h4><ul class=\"ul-list\" data-mark=\"-\"><li class=\"md-list-item md-focus-container\">\u300aCLR via C#\u300b\uff08\u7b2c4\u7248\uff09 Jeffrey Richter<\/li><li>\u300a\u6df1\u5165\u7406\u89e3C#\u300b\uff08\u7b2c3\u7248\uff09 Jon Skeet<\/li><\/ul>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Equals\u865b\u65b9\u6cd5\u53ea\u662f\u5224\u65b7\u300c\u5c0d\u8c61\u300d\u662f\u5426\u8207\u81ea\u8eab\u5305\u542b\u4e00\u6a23\u7684\u503c\uff0c\u662f\u300c\u540c\u4e00\u6027\u300d\uff0c\u800c\u4e0d\u662f\u300c\u76f8\u7b49\u6027\u300d<\/p>\n","protected":false},"author":1,"featured_media":947,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,27,18],"tags":[],"class_list":["post-3264","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-reading-notes","category-c-sharp","category-developing"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/lonelinerd.com\/wp-content\/uploads\/2021\/03\/FeatureCover_ReadingNotes.png","_links":{"self":[{"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/posts\/3264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/comments?post=3264"}],"version-history":[{"count":5,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/posts\/3264\/revisions"}],"predecessor-version":[{"id":3789,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/posts\/3264\/revisions\/3789"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/media\/947"}],"wp:attachment":[{"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/media?parent=3264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/categories?post=3264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/tags?post=3264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}