{"id":3511,"date":"2025-04-13T13:31:54","date_gmt":"2025-04-13T05:31:54","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3511"},"modified":"2025-04-13T13:31:54","modified_gmt":"2025-04-13T05:31:54","slug":"c-%e7%bb%93%e6%9e%84%e4%bd%93%ef%bc%88struct%ef%bc%89-2","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/13\/c-%e7%bb%93%e6%9e%84%e4%bd%93%ef%bc%88struct%ef%bc%89-2\/","title":{"rendered":"C# \u7ed3\u6784\u4f53\uff08Struct\uff09"},"content":{"rendered":"\n<p>C# \u7ed3\u6784\u4f53\uff08<code>struct<\/code>\uff09\u662f\u4e00\u79cd\u503c\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5728\u5185\u5b58\u4e2d\u5206\u914d\u7a7a\u95f4\u65f6\u4e0e\u7c7b\uff08<code>class<\/code>\uff09\u4e0d\u540c\u3002\u7ed3\u6784\u4f53\u901a\u5e38\u7528\u4e8e\u5c01\u88c5\u8f83\u5c0f\u7684\u6570\u636e\u5757\uff0c\u5b83\u6709\u52a9\u4e8e\u63d0\u5347\u6027\u80fd\uff0c\u7279\u522b\u662f\u5728\u5927\u91cf\u5bf9\u8c61\u521b\u5efa\u7684\u573a\u666f\u4e2d\u3002\u7ed3\u6784\u4f53\u7684\u884c\u4e3a\u4e0e\u7c7b\u6709\u5f88\u591a\u76f8\u4f3c\u4e4b\u5904\uff0c\u4f46\u5b83\u7684\u5185\u5b58\u7ba1\u7406\u548c\u4f7f\u7528\u573a\u666f\u6709\u6240\u4e0d\u540c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u7ed3\u6784\u4f53\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u503c\u7c7b\u578b<\/strong>\uff1a\u7ed3\u6784\u4f53\u662f\u503c\u7c7b\u578b\uff08\u4e0d\u50cf\u7c7b\u662f\u5f15\u7528\u7c7b\u578b\uff09\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5f53\u4f60\u5c06\u7ed3\u6784\u4f53\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u4f1a\u521b\u5efa\u8be5\u7ed3\u6784\u4f53\u7684\u526f\u672c\uff0c\u800c\u4e0d\u662f\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u80fd\u7ee7\u627f<\/strong>\uff1a\u7ed3\u6784\u4f53\u4e0d\u80fd\u7ee7\u627f\u5176\u4ed6\u7ed3\u6784\u4f53\u6216\u7c7b\uff0c\u4e5f\u4e0d\u80fd\u88ab\u7ee7\u627f\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u5305\u542b\u65e0\u53c2\u6784\u9020\u51fd\u6570<\/strong>\uff1a\u6bcf\u4e2a\u7ed3\u6784\u4f53\u90fd\u5305\u542b\u4e00\u4e2a\u9ed8\u8ba4\u7684\u65e0\u53c2\u6570\u6784\u9020\u51fd\u6570\uff0c\u4f46\u4f60\u4e0d\u80fd\u81ea\u5b9a\u4e49\u65e0\u53c2\u6784\u9020\u51fd\u6570\u3002\u53ef\u4ee5\u5b9a\u4e49\u5e26\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5b9a\u4e49\u7ed3\u6784\u4f53<\/h2>\n\n\n\n<p>\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\u7c7b\u4f3c\u4e8e\u7c7b\uff0c\u4f46\u7528 <code>struct<\/code> \u5173\u952e\u5b57\u6765\u58f0\u660e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Point\n{\n    public int X;\n    public int Y;\n\n    \/\/ \u5e26\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\n    public Point(int x, int y)\n    {\n        X = x;\n        Y = y;\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u521b\u5efa\u548c\u4f7f\u7528\u7ed3\u6784\u4f53<\/h2>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u50cf\u4f7f\u7528\u7c7b\u4e00\u6837\u521b\u5efa\u548c\u4f7f\u7528\u7ed3\u6784\u4f53\uff0c\u4f46\u7ed3\u6784\u4f53\u662f\u503c\u7c7b\u578b\uff0c\u56e0\u6b64\u5b83\u4eec\u7684\u884c\u4e3a\u6709\u6240\u4e0d\u540c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Point p1 = new Point(10, 20); \/\/ \u4f7f\u7528\u5e26\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\nConsole.WriteLine($\"X: {p1.X}, Y: {p1.Y}\"); \/\/ \u8f93\u51fa X: 10, Y: 20\n\nPoint p2 = p1; \/\/ \u521b\u5efa\u526f\u672c\np2.X = 30;     \/\/ \u4fee\u6539\u526f\u672c\u7684\u503c\nConsole.WriteLine($\"p1.X: {p1.X}, p2.X: {p2.X}\"); \/\/ \u8f93\u51fa p1.X: 10, p2.X: 30<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 \u7ed3\u6784\u4f53\u662f\u503c\u7c7b\u578b\uff0c\u6240\u4ee5\u4fee\u6539 <code>p2<\/code> \u4e0d\u4f1a\u5f71\u54cd <code>p1<\/code>\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u7ed3\u6784\u4f53\u7684\u5e38\u89c1\u64cd\u4f5c<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u64cd\u4f5c\u65b9\u6cd5<\/th><th>\u793a\u4f8b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>new<\/code><\/td><td><code>Point p = new Point(5, 10);<\/code><\/td><td>\u4f7f\u7528\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u7ed3\u6784\u4f53<\/td><\/tr><tr><td><code>ToString()<\/code><\/td><td><code>point.ToString()<\/code><\/td><td>\u8fd4\u56de\u7ed3\u6784\u4f53\u7684\u5b57\u7b26\u4e32\u8868\u793a<\/td><\/tr><tr><td><code>Equals()<\/code><\/td><td><code>point1.Equals(point2)<\/code><\/td><td>\u6bd4\u8f83\u4e24\u4e2a\u7ed3\u6784\u4f53\u662f\u5426\u76f8\u7b49<\/td><\/tr><tr><td><code>GetHashCode()<\/code><\/td><td><code>point.GetHashCode()<\/code><\/td><td>\u83b7\u53d6\u7ed3\u6784\u4f53\u7684\u54c8\u5e0c\u503c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u7ed3\u6784\u4f53\u7684\u6027\u80fd\u7279\u70b9<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u503c\u7c7b\u578b\u5206\u914d\u5728\u6808\u4e0a<\/strong>\uff1a\u7ed3\u6784\u4f53\u5b9e\u4f8b\u901a\u5e38\u5206\u914d\u5728\u6808\u4e0a\uff0c\u800c\u7c7b\u5b9e\u4f8b\u5206\u914d\u5728\u5806\u4e0a\u3002\u6808\u7684\u5185\u5b58\u5206\u914d\u548c\u56de\u6536\u901f\u5ea6\u6bd4\u5806\u5feb\uff0c\u56e0\u6b64\u7ed3\u6784\u4f53\u53ef\u4ee5\u907f\u514d\u5806\u5206\u914d\u7684\u5f00\u9500\u3002<\/li>\n\n\n\n<li><strong>\u907f\u514d\u5783\u573e\u56de\u6536<\/strong>\uff1a\u56e0\u4e3a\u7ed3\u6784\u4f53\u662f\u503c\u7c7b\u578b\uff0c\u5b83\u4eec\u4e0d\u9700\u8981\u5783\u573e\u56de\u6536\uff08GC\uff09\uff0c\u8fd9\u51cf\u5c11\u4e86\u8fd0\u884c\u65f6\u5f00\u9500\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 \u9002\u5408\u9891\u7e41\u521b\u5efa\u3001\u9500\u6bc1\u7684\u5c0f\u5bf9\u8c61\uff08\u5982\u5750\u6807\u3001\u989c\u8272\u7b49\uff09\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u7ed3\u6784\u4f53\u7684\u6ce8\u610f\u4e8b\u9879<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e0d\u53ef\u4e3a <code>null<\/code><\/strong>\uff1a\u7ed3\u6784\u4f53\u662f\u503c\u7c7b\u578b\uff0c\u56e0\u6b64\u4e0d\u80fd\u4e3a <code>null<\/code>\uff0c\u9664\u975e\u4f7f\u7528 <strong>\u53ef\u7a7a\u7c7b\u578b\uff08<code>Nullable<\/code>\uff09<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Point? p = null; \/\/ \u53ef\u7a7a\u7ed3\u6784\u4f53<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e0d\u80fd\u7ee7\u627f<\/strong>\uff1a\u7ed3\u6784\u4f53\u4e0d\u80fd\u7ee7\u627f\u5176\u4ed6\u7ed3\u6784\u4f53\u6216\u7c7b\uff0c\u4e5f\u4e0d\u80fd\u88ab\u7ee7\u627f\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u80fd\u5b9a\u4e49\u9ed8\u8ba4\u6784\u9020\u51fd\u6570<\/strong>\uff1a\u7ed3\u6784\u4f53\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u662f\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u63d0\u4f9b\u7684\uff0c\u4e0d\u80fd\u663e\u5f0f\u5b9a\u4e49\u65e0\u53c2\u6784\u9020\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u7ed3\u6784\u4f53\u793a\u4f8b\uff1a\u8868\u793a\u4e8c\u7ef4\u70b9<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Point\n{\n    public int X;\n    public int Y;\n\n    public Point(int x, int y)\n    {\n        X = x;\n        Y = y;\n    }\n\n    public double DistanceTo(Point other)\n    {\n        return Math.Sqrt(Math.Pow(other.X - X, 2) + Math.Pow(other.Y - Y, 2));\n    }\n\n    public override string ToString()\n    {\n        return $\"({X}, {Y})\";\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u7ed3\u6784\u4f53\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Point p1 = new Point(3, 4);\nPoint p2 = new Point(6, 8);\n\nConsole.WriteLine($\"Point 1: {p1}\");\nConsole.WriteLine($\"Point 2: {p2}\");\nConsole.WriteLine($\"Distance: {p1.DistanceTo(p2)}\");<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u7ed3\u6784\u4f53\u7684\u6bd4\u8f83\u4e0e\u76f8\u7b49\u6027<\/h2>\n\n\n\n<p>C# \u9ed8\u8ba4\u4f1a\u4f7f\u7528 <strong>\u9010\u5b57\u6bb5\u6bd4\u8f83<\/strong> \u6765\u5224\u65ad\u7ed3\u6784\u4f53\u7684\u76f8\u7b49\u6027\u3002\u4f60\u53ef\u4ee5\u91cd\u5199 <code>Equals<\/code> \u65b9\u6cd5\u6765\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7684\u76f8\u7b49\u6027\u6bd4\u8f83\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Point\n{\n    public int X;\n    public int Y;\n\n    public override bool Equals(object obj)\n    {\n        if (obj is Point)\n        {\n            Point p = (Point)obj;\n            return X == p.X &amp;&amp; Y == p.Y;\n        }\n        return false;\n    }\n\n    public override int GetHashCode()\n    {\n        return X.GetHashCode() ^ Y.GetHashCode();\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e5d\u3001\u7ed3\u6784\u4f53\u7684\u9002\u7528\u573a\u666f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784<\/h3>\n\n\n\n<p>\u9002\u7528\u4e8e\u8868\u793a\u4e00\u7ec4\u5c0f\u7684\u3001\u5c01\u88c5\u7684\u6570\u636e\uff08\u4f8b\u5982\uff1a\u5750\u6807\u3001\u989c\u8272\u3001\u77e9\u9635\u7b49\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u6027\u80fd\u4f18\u5316<\/h3>\n\n\n\n<p>\u7531\u4e8e\u7ed3\u6784\u4f53\u662f\u503c\u7c7b\u578b\uff0c\u56e0\u6b64\u53ef\u4ee5\u907f\u514d\u5783\u573e\u56de\u6536\u548c\u5806\u5185\u5b58\u5206\u914d\u7684\u5f00\u9500\uff0c\u7279\u522b\u662f\u5728\u9891\u7e41\u521b\u5efa\u5c0f\u5bf9\u8c61\u65f6\uff08\u4f8b\u5982\uff1a\u6e38\u620f\u5f00\u53d1\u4e2d\u7684\u4f4d\u7f6e\u3001\u901f\u5ea6\u7b49\u7269\u7406\u5c5e\u6027\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u7ebf\u7a0b\u5b89\u5168<\/h3>\n\n\n\n<p>\u503c\u7c7b\u578b\uff08\u7ed3\u6784\u4f53\uff09\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u5904\u7406\u7684\u662f\u72ec\u7acb\u7684\u526f\u672c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341\u3001\u53c2\u8003\u6587\u732e\u548c\u6587\u6863<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/struct\">C# \u7ed3\u6784\u4f53\uff08Struct\uff09\u6587\u6863 &#8211; Microsoft<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/differences-between-structs-and-classes\">C# \u7ed3\u6784\u4f53\u4e0e\u7c7b\u7684\u533a\u522b<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\uff0c\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C# \u7ed3\u6784\u4f53\uff08struct\uff09\u662f\u4e00\u79cd\u503c\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5728\u5185\u5b58\u4e2d\u5206\u914d\u7a7a\u95f4\u65f6\u4e0e\u7c7b\uff08class\uff09\u4e0d\u540c\u3002\u7ed3\u6784\u4f53\u901a\u5e38\u7528\u4e8e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3512,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3511","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3511"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3511\/revisions"}],"predecessor-version":[{"id":3513,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3511\/revisions\/3513"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3512"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}