{"id":123,"date":"2026-05-21T15:31:05","date_gmt":"2026-05-21T07:31:05","guid":{"rendered":"https:\/\/tachyoncat.top\/?p=123"},"modified":"2026-05-21T15:31:05","modified_gmt":"2026-05-21T07:31:05","slug":"c%e5%9f%ba%e7%a1%80%e5%85%a5%e9%97%a82","status":"publish","type":"post","link":"https:\/\/tachyoncat.top\/index.php\/2026\/05\/21\/c%e5%9f%ba%e7%a1%80%e5%85%a5%e9%97%a82\/","title":{"rendered":"C#\u57fa\u7840\u5165\u95e82 <\u679a\u4e3e><\u7ed3\u6784\u4f53><\u59d4\u6258\u548c\u4e8b\u4ef6><\u6cdb\u578b>"},"content":{"rendered":"<h1>C# \u8fdb\u9636\u77e5\u8bc6\u70b9\u603b\u7ed3<\/h1>\n<blockquote><p>\n  \u9002\u5408\u521d\u5b66\u8005\u7684C#\u8fdb\u9636\u6307\u5357\n<\/p><\/blockquote>\n<hr \/>\n<h2>\u76ee\u5f55<\/h2>\n<ol>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#1-\u679a\u4e3e-enum\" target=\"_blank\"  rel=\"nofollow\" >\u679a\u4e3e (enum)<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#2-\u7ed3\u6784\u4f53-struct\" target=\"_blank\"  rel=\"nofollow\" >\u7ed3\u6784\u4f53 (struct)<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#3-\u8fd0\u7b97\u7b26\u91cd\u8f7d\" target=\"_blank\"  rel=\"nofollow\" >\u8fd0\u7b97\u7b26\u91cd\u8f7d<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#4-\u88c5\u7bb1\u4e0e\u62c6\u7bb1\" target=\"_blank\"  rel=\"nofollow\" >\u88c5\u7bb1\u4e0e\u62c6\u7bb1<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#5-arraylist-\u4e3a\u4ec0\u4e48\u4e0d\u63a8\u8350\" target=\"_blank\"  rel=\"nofollow\" >ArrayList \u4e3a\u4ec0\u4e48\u4e0d\u63a8\u8350<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#6-\u59d4\u6258-delegate\" target=\"_blank\"  rel=\"nofollow\" >\u59d4\u6258 (delegate)<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#7-\u4e8b\u4ef6-event\" target=\"_blank\"  rel=\"nofollow\" >\u4e8b\u4ef6 (event)<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#8-lambda-\u8868\u8fbe\u5f0f\" target=\"_blank\"  rel=\"nofollow\" >Lambda \u8868\u8fbe\u5f0f<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#9-in--out--ref-\u53c2\u6570\u4fee\u9970\u7b26\" target=\"_blank\"  rel=\"nofollow\" >in \/ out \/ ref \u53c2\u6570\u4fee\u9970\u7b26<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#10-\u6cdb\u578b-generic\" target=\"_blank\"  rel=\"nofollow\" >\u6cdb\u578b (Generic)<\/a><\/li>\n<\/ol>\n<hr \/>\n<h2>1. \u679a\u4e3e (enum)<\/h2>\n<p>\u679a\u4e3e\u7528\u4e8e\u5b9a\u4e49\u4e00\u7ec4<strong>\u547d\u540d\u7684\u5e38\u91cf<\/strong>\uff0c\u8ba9\u4ee3\u7801\u66f4\u6613\u8bfb\u3002<\/p>\n<h3>\u57fa\u672c\u8bed\u6cd5<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u5b9a\u4e49\u679a\u4e3e\npublic enum Weekday\n{\n    Monday,    \/\/ \u9ed8\u8ba4\u4ece0\u5f00\u59cb\n    Tuesday,   \/\/ 1\n    Wednesday, \/\/ 2\n    Thursday,  \/\/ 3\n    Friday,    \/\/ 4\n    Saturday,  \/\/ 5\n    Sunday     \/\/ 6\n}\n\n\/\/ \u4e5f\u53ef\u4ee5\u624b\u52a8\u6307\u5b9a\u503c\npublic enum StatusCode\n{\n    Success = 200,\n    NotFound = 404,\n    Error = 500\n}\n<\/code><\/pre>\n<h3>\u4f7f\u7528\u679a\u4e3e<\/h3>\n<pre><code class=\"language-csharp line-numbers\">Weekday today = Weekday.Friday;\nConsole.WriteLine(today);          \/\/ \u8f93\u51fa: Friday\nConsole.WriteLine((int)today);     \/\/ \u8f93\u51fa: 4\n\nStatusCode code = StatusCode.NotFound;\nConsole.WriteLine((int)code);      \/\/ \u8f93\u51fa: 404\n<\/code><\/pre>\n<h3>\u679a\u4e3e\u7684\u5e95\u5c42<\/h3>\n<p>\u679a\u4e3e\u672c\u8d28\u4e0a\u662f<strong>\u6574\u6570<\/strong>\uff0c\u6240\u4ee5\u53ef\u4ee5\u4e0e int \u76f8\u4e92\u8f6c\u6362\uff1a<\/p>\n<pre><code class=\"language-csharp line-numbers\">int dayNum = 3;\nWeekday day = (Weekday)dayNum;     \/\/ int \u2192 enum\nConsole.WriteLine(day);            \/\/ Thursday\n\nint num = (int)Weekday.Sunday;     \/\/ enum \u2192 int\nConsole.WriteLine(num);            \/\/ 6\n<\/code><\/pre>\n<h3>switch \u914d\u5408\u679a\u4e3e<\/h3>\n<pre><code class=\"language-csharp line-numbers\">public string GetDayMessage(Weekday day)\n{\n    switch (day)\n    {\n        case Weekday.Monday:\n            return \"\u5f00\u59cb\u65b0\u7684\u4e00\u5468\uff01\";\n        case Weekday.Friday:\n            return \"\u660e\u5929\u5c31\u662f\u5468\u672b\u5566\uff01\";\n        case Weekday.Saturday:\n        case Weekday.Sunday:\n            return \"\u4f11\u606f\u65e5\";\n        default:\n            return \"\u5de5\u4f5c\u65e5\";\n    }\n}\n<\/code><\/pre>\n<hr \/>\n<h2>2. \u7ed3\u6784\u4f53 (struct)<\/h2>\n<p>\u7ed3\u6784\u4f53\u662f<strong>\u503c\u7c7b\u578b<\/strong>\uff0c\u4e0e<strong>\u7c7b\uff08\u5f15\u7528\u7c7b\u578b\uff09<\/strong>\u6709\u91cd\u8981\u533a\u522b\u3002<\/p>\n<h3>\u503c\u7c7b\u578b vs \u5f15\u7528\u7c7b\u578b<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u7279\u6027<\/th>\n<th>\u7ed3\u6784\u4f53 (struct)<\/th>\n<th>\u7c7b (class)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u7c7b\u578b<\/td>\n<td>\u503c\u7c7b\u578b<\/td>\n<td>\u5f15\u7528\u7c7b\u578b<\/td>\n<\/tr>\n<tr>\n<td>\u5b58\u50a8\u4f4d\u7f6e<\/td>\n<td>\u6808 (Stack)<\/td>\n<td>\u5806 (Heap)<\/td>\n<\/tr>\n<tr>\n<td>\u8d4b\u503c\u884c\u4e3a<\/td>\n<td>\u590d\u5236\u6574\u4e2a\u5bf9\u8c61<\/td>\n<td>\u53ea\u590d\u5236\u5f15\u7528<\/td>\n<\/tr>\n<tr>\n<td>\u9ed8\u8ba4\u503c<\/td>\n<td>\u81ea\u52a8\u521d\u59cb\u5316\u4e3a\u96f6<\/td>\n<td>\u9ed8\u8ba4 null<\/td>\n<\/tr>\n<tr>\n<td>\u6027\u80fd<\/td>\n<td>\u66f4\u8f7b\u91cf\u3001\u66f4\u5feb<\/td>\n<td>\u6709GC\u5f00\u9500<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u5c0f\u578b\u6570\u636e\u7ed3\u6784<\/td>\n<td>\u590d\u6742\u5bf9\u8c61\u3001\u9700\u8981\u7ee7\u627f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u56fe\u89e3<\/h3>\n<pre><code class=\"line-numbers\">\u3010\u503c\u7c7b\u578b (struct)\u3011\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502  \u6808              \u2502\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2502\n\u2502  \u2502 a: Point    \u2502 \u2502 \u2190 \u5b8c\u6574\u6570\u636e\n\u2502  \u2502 x=10, y=20   \u2502 \u2502\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 \u2502\n\u2502                  \u2502\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2502\n\u2502  \u2502 b: Point    \u2502 \u2502 \u2190 \u590d\u5236\u4e86\u4e00\u4efd\u65b0\u6570\u636e\n\u2502  \u2502 x=10, y=20   \u2502 \u2502\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n\n\u3010\u5f15\u7528\u7c7b\u578b (class)\u3011\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502  \u6808              \u2502    \u2502  \u5806              \u2502\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2502    \u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2502\n\u2502  \u2502  a: Person   \u2502\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2502 Person\u5bf9\u8c61    \u2502 \u2502\n\u2502  \u2502  (\u5f15\u7528\u5730\u5740)   \u2502 \u2502    \u2502  \u2502 name=\"\u5f20\u4e09\"  \u2502 \u2502\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 \u2502    \u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 \u2502\n\u2502                  \u2502    \u2502                  \u2502\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2502    \u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2502\n\u2502  \u2502  b: Person   \u2502\u2500\u253c\u2500\u2510  \u2502  \u2502 Person\u5bf9\u8c61    \u2502 \u2502\n\u2502  \u2502  (\u5f15\u7528\u5730\u5740)   \u2502 \u2502 \u2514\u2500\u2500\u2500\u2500\u2500\u2502 name=\"\u674e\u56db\"  \u2502 \u2502\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 \u2502    \u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n<\/code><\/pre>\n<h3>\u7ed3\u6784\u4f53\u793a\u4f8b<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u5b9a\u4e49\u7ed3\u6784\u4f53\npublic struct Point\n{\n    public int X { get; set; }\n    public int Y { get; set; }\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        int dx = X - other.X;\n        int dy = Y - other.Y;\n        return Math.Sqrt(dx * dx + dy * dy);\n    }\n}\n\n\/\/ \u4f7f\u7528\nPoint a = new Point(0, 0);\nPoint b = new Point(3, 4);\nConsole.WriteLine(a.DistanceTo(b));  \/\/ 5\n<\/code><\/pre>\n<h3>\u8d4b\u503c\u884c\u4e3a\u5bf9\u6bd4<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u7ed3\u6784\u4f53 - \u8d4b\u503c\u65f6\u590d\u5236\u6574\u4e2a\u5bf9\u8c61\nPoint p1 = new Point(1, 2);\nPoint p2 = p1;        \/\/ \u590d\u5236\u4e86\u4e00\u4efd\uff01\np2.X = 10;\nConsole.WriteLine(p1.X);  \/\/ 1 \u2190 p1\u6ca1\u53d8\n\n\/\/ \u7c7b - \u8d4b\u503c\u65f6\u590d\u5236\u5f15\u7528\uff08\u5171\u4eab\u540c\u4e00\u4e2a\u5bf9\u8c61\uff09\nPerson c1 = new Person { Name = \"\u5f20\u4e09\" };\nPerson c2 = c1;       \/\/ \u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\nc2.Name = \"\u674e\u56db\";\nConsole.WriteLine(c1.Name);  \/\/ \u674e\u56db \u2190 c1\u4e5f\u53d8\u4e86\uff01\n<\/code><\/pre>\n<h3>\u4ec0\u4e48\u65f6\u5019\u7528\u7ed3\u6784\u4f53\uff1f<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u2705 \u9002\u5408\u7528\u7ed3\u6784\u4f53\u7684\u573a\u666f\uff1a\npublic struct RGBColor { public byte R, G, B; }           \/\/ \u5c0f\u578b\u6570\u636e\npublic struct Vector3 { public float X, Y, Z; }            \/\/ \u51e0\u4f55\u6570\u636e\npublic struct DateTimeOffset { ... }                      \/\/ .NET\u5185\u7f6e\u503c\u7c7b\u578b\n\n\/\/ \u274c \u4e0d\u9002\u5408\u7528\u7ed3\u6784\u4f53\u7684\u573a\u666f\uff1a\npublic struct Employee { public string Name; ... }          \/\/ \u5305\u542b\u5927\u5bf9\u8c61\npublic struct Order { public List&lt;Item&gt; Items; }           \/\/ \u9700\u8981\u7ee7\u627f\n<\/code><\/pre>\n<hr \/>\n<h2>3. \u8fd0\u7b97\u7b26\u91cd\u8f7d<\/h2>\n<p>\u5141\u8bb8\u81ea\u5b9a\u4e49\u7c7b\u5728\u4f7f\u7528\u8fd0\u7b97\u7b26\u65f6\u7684\u884c\u4e3a\u3002<\/p>\n<h3>\u57fa\u672c\u8bed\u6cd5<\/h3>\n<pre><code class=\"language-csharp line-numbers\">public class Vector2\n{\n    public int X { get; set; }\n    public int Y { get; set; }\n\n    public Vector2(int x, int y)\n    {\n        X = x;\n        Y = y;\n    }\n\n    \/\/ \u91cd\u8f7d + \u8fd0\u7b97\u7b26\n    public static Vector2 operator +(Vector2 a, Vector2 b)\n    {\n        return new Vector2(a.X + b.X, a.Y + b.Y);\n    }\n\n    \/\/ \u91cd\u8f7d == \u8fd0\u7b97\u7b26\uff08\u5fc5\u987b\u540c\u65f6\u91cd\u8f7d !=\uff09\n    public static bool operator ==(Vector2 a, Vector2 b)\n    {\n        return a.X == b.X &amp;&amp; a.Y == b.Y;\n    }\n\n    public static bool operator !=(Vector2 a, Vector2 b)\n    {\n        return !(a == b);\n    }\n}\n<\/code><\/pre>\n<h3>\u4f7f\u7528<\/h3>\n<pre><code class=\"language-csharp line-numbers\">Vector2 v1 = new Vector2(1, 2);\nVector2 v2 = new Vector2(3, 4);\nVector2 v3 = v1 + v2;          \/\/ \u81ea\u52a8\u8c03\u7528\u6211\u4eec\u7684 + \u91cd\u8f7d\nConsole.WriteLine(v3.X);       \/\/ 4\nConsole.WriteLine(v3.Y);       \/\/ 6\n<\/code><\/pre>\n<h3>\u53ef\u4ee5\u91cd\u8f7d\u7684\u8fd0\u7b97\u7b26<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u8fd0\u7b97\u7b26<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>+<\/code>, <code>-<\/code>, <code>*<\/code>, <code>\/<\/code>, <code>%<\/code><\/td>\n<td>\u7b97\u672f\u8fd0\u7b97\u7b26<\/td>\n<\/tr>\n<tr>\n<td><code>==<\/code>, <code>!=<\/code>, <code>&lt;<\/code>, <code>&gt;<\/code>, <code>&lt;=<\/code>, <code>&gt;=<\/code><\/td>\n<td>\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/td>\n<\/tr>\n<tr>\n<td><code>&amp;&amp;<\/code>, <code>\\|\\|<\/code><\/td>\n<td>\u4e0d\u80fd\u91cd\u8f7d\uff01<\/td>\n<\/tr>\n<tr>\n<td><code>=<\/code><\/td>\n<td>\u4e0d\u80fd\u91cd\u8f7d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2>4. \u88c5\u7bb1\u4e0e\u62c6\u7bb1<\/h2>\n<h3>\u4ec0\u4e48\u662f\u88c5\u7bb1\uff1f<\/h3>\n<p><strong>\u503c\u7c7b\u578b \u2192 \u5f15\u7528\u7c7b\u578b<\/strong> \u7684\u8f6c\u6362\uff0c\u9700\u8981\u5728\u5806\u4e0a\u5206\u914d\u5185\u5b58\u3002<\/p>\n<pre><code class=\"language-csharp line-numbers\">int i = 123;        \/\/ \u503c\u7c7b\u578b\uff0c\u5728\u6808\u4e0a\nobject o = i;       \/\/ \u88c5\u7bb1\uff01\u628a\u503c\u7c7b\u578b\u5305\u88c5\u6210\u5f15\u7528\u7c7b\u578b\n\n\/\/ \u5185\u90e8\u53d1\u751f\u7684\u4e8b\uff1a\n\/\/ 1. \u5728\u5806\u4e0a\u5206\u914d\u5185\u5b58\n\/\/ 2. \u590d\u5236\u6808\u4e0a\u7684\u503c\u5230\u5806\n\/\/ 3. \u8fd4\u56de\u5806\u4e0a\u7684\u5f15\u7528\u5730\u5740\n<\/code><\/pre>\n<h3>\u4ec0\u4e48\u662f\u62c6\u7bb1\uff1f<\/h3>\n<p><strong>\u5f15\u7528\u7c7b\u578b \u2192 \u503c\u7c7b\u578b<\/strong> \u7684\u8f6c\u6362\uff0c\u9700\u8981\u7c7b\u578b\u68c0\u67e5\u3002<\/p>\n<pre><code class=\"language-csharp line-numbers\">object o = 123;     \/\/ \u88c5\u7bb1\nint i = (int)o;      \/\/ \u62c6\u7bb1\uff01\u4ece\u5806\u4e0a\u53d6\u56de\u503c\u7c7b\u578b\n\n\/\/ \u5982\u679c\u7c7b\u578b\u4e0d\u5339\u914d\uff0c\u4f1a\u629b\u5f02\u5e38\uff1a\nobject o2 = \"hello\";\n\/\/ int i2 = (int)o2;  \/\/ \u274c InvalidCastException\uff01\n<\/code><\/pre>\n<h3>\u56fe\u89e3\u88c5\u7bb1\u62c6\u7bb1<\/h3>\n<pre><code class=\"line-numbers\">\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502  \u88c5\u7bb1 (Boxing)                               \u2502\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510         \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u2502\n\u2502  \u2502 int i \u2502  \u2500\u2500\u2500\u2500\u25ba  \u2502 object o          \u2502    \u2502\n\u2502  \u2502 = 123 \u2502         \u2502 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2502    \u2502\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518         \u2502 \u2502 123 (\u5806\u4e0a)     \u2502 \u2502    \u2502\n\u2502   \u6808               \u2502 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 \u2502    \u2502\n\u2502                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2502\n\u2502                     \u5806                      \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502  \u62c6\u7bb1 (Unboxing)                             \u2502\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510         \u2502\n\u2502  \u2502 object o          \u2502\u2500\u2500\u2500\u25ba\u2502 int i \u2502         \u2502\n\u2502  \u2502 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2502    \u2502 = 123 \u2502         \u2502\n\u2502  \u2502 \u2502 123 (\u5806\u4e0a)     \u2502 \u2502    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518         \u2502\n\u2502  \u2502 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 \u2502     \u6808               \u2502\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518                      \u2502\n\u2502   \u5806                   \u2713 \u9700\u8981\u7c7b\u578b\u68c0\u67e5        \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n<\/code><\/pre>\n<h3>\u6027\u80fd\u95ee\u9898<\/h3>\n<p>\u88c5\u7bb1\u62c6\u7bb1\u6709<strong>\u6027\u80fd\u5f00\u9500<\/strong>\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\uff1a<\/p>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u274c \u6027\u80fd\u5dee\uff1a\u5927\u91cf\u88c5\u7bb1\nArrayList list = new ArrayList();\nfor (int i = 0; i &lt; 100000; i++)\n{\n    list.Add(i);  \/\/ \u6bcf\u6b21\u90fd\u88c5\u7bb1\uff01int \u2192 object\n}\n\n\/\/ \u2705 \u6027\u80fd\u597d\uff1a\u4f7f\u7528\u6cdb\u578b\nList&lt;int&gt; list2 = new List&lt;int&gt;();\nfor (int i = 0; i &lt; 100000; i++)\n{\n    list2.Add(i);  \/\/ \u6ca1\u6709\u88c5\u7bb1\uff01\n}\n<\/code><\/pre>\n<h3>\u4ec0\u4e48\u65f6\u5019\u4f1a\u53d1\u751f\u88c5\u7bb1\uff1f<\/h3>\n<pre><code class=\"language-csharp line-numbers\">int num = 10;\n\n\/\/ \u8fd9\u4e9b\u90fd\u4f1a\u88c5\u7bb1\uff01\nobject o = num;                    \/\/ \u76f4\u63a5\u8d4b\u503c\nConsole.WriteLine(num);            \/\/ Console.WriteLine \u63a5\u6536 object\nArrayList list = new ArrayList();\nlist.Add(num);                     \/\/ ArrayList.Add \u63a5\u6536 object\nMethodThatTakesObject(num);        \/\/ \u65b9\u6cd5\u53c2\u6570\u662f object\n\n\/\/ \u89e3\u51b3\u529e\u6cd5\uff1a\u7528\u6cdb\u578b\nList&lt;int&gt; list = new List&lt;int&gt;();\nlist.Add(num);                     \/\/ \u4e0d\u88c5\u7bb1\uff01\n<\/code><\/pre>\n<hr \/>\n<h2>5. ArrayList \u4e3a\u4ec0\u4e48\u4e0d\u63a8\u8350<\/h2>\n<h3>ArrayList \u7684\u95ee\u9898<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ ArrayList \u53ef\u4ee5\u5b58\u50a8\u4efb\u610f\u7c7b\u578b\nArrayList list = new ArrayList();\nlist.Add(10);          \/\/ int \u2192 object (\u88c5\u7bb1)\nlist.Add(\"hello\");     \/\/ string\nlist.Add(new Person()); \/\/ \u5bf9\u8c61\n\n\/\/ \u8bfb\u53d6\u65f6\u8fd4\u56de object\uff0c\u9700\u8981\u5f3a\u5236\u8f6c\u6362\nint num = (int)list[0];    \/\/ \u62c6\u7bb1\nstring str = (string)list[1];\n<\/code><\/pre>\n<h3>\u4e09\u5927\u95ee\u9898<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u95ee\u9898<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>\u88c5\u7bb1\u62c6\u7bb1<\/strong><\/td>\n<td>\u5b58\u50a8\u503c\u7c7b\u578b\u65f6\u4f1a\u88c5\u7bb1\uff0c\u8bfb\u53d6\u65f6\u62c6\u7bb1\uff0c\u6027\u80fd\u5dee<\/td>\n<\/tr>\n<tr>\n<td><strong>\u7c7b\u578b\u4e0d\u5b89\u5168<\/strong><\/td>\n<td>\u53ef\u4ee5\u653e\u5165\u4efb\u4f55\u7c7b\u578b\uff0c\u7f16\u8bd1\u65f6\u4e0d\u68c0\u67e5<\/td>\n<\/tr>\n<tr>\n<td><strong>\u9700\u8981\u5f3a\u5236\u8f6c\u6362<\/strong><\/td>\n<td>\u8bfb\u53d6\u65f6\u5fc5\u987b\u624b\u52a8\u8f6c\u6362\uff0c\u5bb9\u6613\u51fa\u9519<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u63a8\u8350\uff1a\u4f7f\u7528\u6cdb\u578b List<T><\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u2705 \u6cdb\u578bList - \u7c7b\u578b\u5b89\u5168\uff0c\u6ca1\u6709\u88c5\u7bb1\nList&lt;int&gt; numbers = new List&lt;int&gt;();\nnumbers.Add(10);              \/\/ \u76f4\u63a5\u6dfb\u52a0\uff0c\u4e0d\u4f1a\u88c5\u7bb1\nint num = numbers[0];          \/\/ \u76f4\u63a5\u8bfb\u53d6\uff0c\u4e0d\u9700\u8981\u8f6c\u6362\n\nList&lt;string&gt; words = new List&lt;string&gt;();\nwords.Add(\"hello\");\nstring word = words[0];        \/\/ \u7c7b\u578b\u5b89\u5168\uff0c\u7f16\u8bd1\u65f6\u5c31\u68c0\u67e5\n\n\/\/ \u274c \u7f16\u8bd1\u9519\u8bef\uff01\u7c7b\u578b\u5b89\u5168\nnumbers.Add(\"hello\");  \/\/ \u9519\u8bef\uff1a\u4e0d\u80fd\u628astring\u653e\u5165List&lt;int&gt;\n<\/code><\/pre>\n<h3>\u4e00\u53e5\u8bdd\u603b\u7ed3<\/h3>\n<blockquote><p>\n  <strong>ArrayList = \u88c5\u7bb1\u62c6\u7bb1 + \u7c7b\u578b\u4e0d\u5b89\u5168 + \u5f3a\u5236\u8f6c\u6362 = \u4e0d\u63a8\u8350<\/strong><\/p>\n<p>  <strong>List<T> = \u65e0\u88c5\u7bb1\u62c6\u7bb1 + \u7c7b\u578b\u5b89\u5168 + \u65e0\u9700\u8f6c\u6362 = \u63a8\u8350<\/strong>\n<\/p><\/blockquote>\n<hr \/>\n<h2>6. \u59d4\u6258 (delegate)<\/h2>\n<p>\u59d4\u6258\u662f\u4e00\u79cd<strong>\u7c7b\u578b\u5b89\u5168\u7684\u51fd\u6570\u6307\u9488<\/strong>\uff0c\u53ef\u4ee5\u5f15\u7528\u65b9\u6cd5\u3002<\/p>\n<h3>\u57fa\u672c\u6982\u5ff5<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ 1. \u58f0\u660e\u59d4\u6258\u7c7b\u578b\npublic delegate int Calculate(int a, int b);\n\n\/\/ 2. \u5b9a\u4e49\u7b26\u5408\u59d4\u6258\u7684\u65b9\u6cd5\npublic int Add(int a, int b) { return a + b; }\npublic int Multiply(int a, int b) { return a * b; }\n\n\/\/ 3. \u521b\u5efa\u59d4\u6258\u5b9e\u4f8b\nCalculate calc = Add;           \/\/ \u5f15\u7528 Add \u65b9\u6cd5\nCalculate calc2 = Multiply;     \/\/ \u5f15\u7528 Multiply \u65b9\u6cd5\n\n\/\/ 4. \u8c03\u7528\nint result = calc(5, 3);        \/\/ \u8c03\u7528 Add(5, 3)\uff0c\u7ed3\u679c 8\nint result2 = calc2(5, 3);     \/\/ \u8c03\u7528 Multiply(5, 3)\uff0c\u7ed3\u679c 15\n<\/code><\/pre>\n<h3>\u59d4\u6258\u7684\u7c7b\u578b\u5b89\u5168<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u274c \u7f16\u8bd1\u9519\u8bef\uff01\u65b9\u6cd5\u7b7e\u540d\u4e0d\u5339\u914d\nCalculate calc = Add;        \/\/ Add \u7b7e\u540d\u662f (int, int) \u2192 int \u2713\nCalculate calc2 = \"hello\";   \/\/ \u9519\u8bef\uff01\u4e0d\u80fd\u8d4b\u5b57\u7b26\u4e32\n\n\/\/ \u65b9\u6cd5\u53c2\u6570\u6216\u8fd4\u56de\u503c\u4e0d\u5339\u914d\u4e5f\u4f1a\u62a5\u9519\npublic int Subtract(int a, int b, int c) { return a - b - c; }\nCalculate calc3 = Subtract;   \/\/ \u274c \u7f16\u8bd1\u9519\u8bef\uff01\u53c2\u6570\u4e2a\u6570\u4e0d\u5339\u914d\n<\/code><\/pre>\n<h3>\u591a\u64ad\u59d4\u6258<\/h3>\n<p>\u4e00\u4e2a\u59d4\u6258\u53ef\u4ee5\u5f15\u7528<strong>\u591a\u4e2a\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<pre><code class=\"language-csharp line-numbers\">public delegate void Action(string message);\n\npublic void SendEmail(string msg) { Console.WriteLine(<span class=\"katex math inline\">\"\u90ae\u4ef6: {msg}\"); }\npublic void SendSMS(string msg) { Console.WriteLine(<\/span>\"\u77ed\u4fe1: {msg}\"); }\npublic void Log(string msg) { Console.WriteLine($\"\u65e5\u5fd7: {msg}\"); }\n\n\/\/ \u521b\u5efa\u591a\u64ad\u59d4\u6258\nAction actions = SendEmail;\nactions += SendSMS;    \/\/ \u6dfb\u52a0\nactions += Log;        \/\/ \u518d\u6dfb\u52a0\n\n\/\/ \u8c03\u7528\u4e00\u6b21\uff0c\u6240\u6709\u65b9\u6cd5\u90fd\u4f1a\u6267\u884c\nactions(\"\u4f60\u597d\uff01\");\n\/\/ \u8f93\u51fa:\n\/\/ \u90ae\u4ef6: \u4f60\u597d\uff01\n\/\/ \u77ed\u4fe1: \u4f60\u597d\uff01\n\/\/ \u65e5\u5fd7: \u4f60\u597d\uff01\n\n\/\/ \u79fb\u9664\u65b9\u6cd5\nactions -= SendSMS;\nactions(\"\u518d\u89c1\uff01\");\n\/\/ \u8f93\u51fa:\n\/\/ \u90ae\u4ef6: \u518d\u89c1\uff01\n\/\/ \u65e5\u5fd7: \u518d\u89c1\uff01\n<\/code><\/pre>\n<h3>\u5185\u7f6e\u59d4\u6258\u7c7b\u578b<\/h3>\n<p>C# \u63d0\u4f9b\u4e86\u5e38\u7528\u7684\u5185\u7f6e\u59d4\u6258\uff0c\u4e0d\u9700\u8981\u81ea\u5df1\u58f0\u660e\uff1a<\/p>\n<pre><code class=\"language-csharp line-numbers\">\/\/ Action - \u65e0\u8fd4\u56de\u503c\u7684\u65b9\u6cd5\nAction&lt;string&gt; print = Console.WriteLine;\nAction add = () =&gt; Console.WriteLine(\"\u6dfb\u52a0\u6210\u529f\");\n\n\/\/ Func - \u6709\u8fd4\u56de\u503c\u7684\u65b9\u6cd5\nFunc&lt;int, int, int&gt; calculate = (a, b) =&gt; a + b;\nFunc&lt;int&gt; getRandom = () =&gt; new Random().Next();\n\n\/\/ Predicate - \u8fd4\u56de bool \u7684\u65b9\u6cd5\nPredicate&lt;int&gt; isEven = n =&gt; n % 2 == 0;\n<\/code><\/pre>\n<hr \/>\n<h2>7. \u4e8b\u4ef6 (event)<\/h2>\n<p>\u4e8b\u4ef6\u662f\u57fa\u4e8e\u59d4\u6258\u7684\u5c01\u88c5\uff0c<strong>\u9650\u5236\u5916\u90e8\u53ea\u80fd\u8ba2\u9605\/\u53d6\u6d88\u8ba2\u9605\uff0c\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528<\/strong>\u3002<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u9700\u8981\u4e8b\u4ef6\uff1f<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u274c \u95ee\u9898\uff1a\u7528\u59d4\u6258\uff0c\u5916\u90e8\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\uff08\u4e0d\u5b89\u5168\uff09\npublic class Button\n{\n    public Action OnClick;  \/\/ \u5916\u90e8\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\uff1abutton.OnClick()\n}\n\nButton btn = new Button();\nbtn.OnClick();  \/\/ \u8c01\u90fd\u53ef\u4ee5\u968f\u4fbf\u8c03\u7528\uff01\n<\/code><\/pre>\n<h3>\u7528\u4e8b\u4ef6\u4fdd\u62a4\u59d4\u6258<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u2705 \u4e8b\u4ef6 - \u5916\u90e8\u53ea\u80fd += \u548c -=\uff0c\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\npublic class Button\n{\n    \/\/ \u4f7f\u7528 event \u5173\u952e\u5b57\n    public event Action OnClick;\n\n    public void Click()\n    {\n        Console.WriteLine(\"\u6309\u94ae\u88ab\u70b9\u51fb\");\n        \/\/ \u53ea\u6709\u5185\u90e8\u53ef\u4ee5\u89e6\u53d1\u4e8b\u4ef6\n        OnClick?.Invoke();  \/\/ \u5b89\u5168\u8c03\u7528\n    }\n}\n\nButton btn = new Button();\nbtn.OnClick += () =&gt; Console.WriteLine(\"\u5904\u7406\u70b9\u51fb1\");  \/\/ \u53ef\u4ee5\u8ba2\u9605\nbtn.OnClick += () =&gt; Console.WriteLine(\"\u5904\u7406\u70b9\u51fb2\");  \/\/ \u518d\u6b21\u8ba2\u9605\n\nbtn.Click();  \/\/ \u8f93\u51fa:\n\/\/ \u6309\u94ae\u88ab\u70b9\u51fb\n\/\/ \u5904\u7406\u70b9\u51fb1\n\/\/ \u5904\u7406\u70b9\u51fb2\n\n\/\/ \u274c \u5916\u90e8\u4e0d\u80fd\u8fd9\u6837\u8c03\u7528\uff1a\n\/\/ btn.OnClick();  \/\/ \u7f16\u8bd1\u9519\u8bef\uff01\n<\/code><\/pre>\n<h3>\u4e8b\u4ef6 vs \u59d4\u6258<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u7279\u6027<\/th>\n<th>\u59d4\u6258<\/th>\n<th>\u4e8b\u4ef6<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u5916\u90e8\u53ef\u4ee5\u8c03\u7528<\/td>\n<td>\u2705 \u53ef\u4ee5<\/td>\n<td>\u274c \u4e0d\u53ef\u4ee5<\/td>\n<\/tr>\n<tr>\n<td>\u5916\u90e8\u53ef\u4ee5\u8ba2\u9605<\/td>\n<td>\u2705 \u53ef\u4ee5<\/td>\n<td>\u2705 \u53ef\u4ee5<\/td>\n<\/tr>\n<tr>\n<td>\u5916\u90e8\u53ef\u4ee5\u53d6\u6d88\u8ba2\u9605<\/td>\n<td>\u2705 \u53ef\u4ee5<\/td>\n<td>\u2705 \u53ef\u4ee5<\/td>\n<\/tr>\n<tr>\n<td>\u5c01\u88c5\u6027<\/td>\n<td>\u4f4e<\/td>\n<td>\u9ad8<\/td>\n<\/tr>\n<tr>\n<td>\u4f7f\u7528\u573a\u666f<\/td>\n<td>\u56de\u8c03\u3001\u51fd\u6570\u53c2\u6570<\/td>\n<td>\u901a\u77e5\u673a\u5236<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5178\u578b\u5e94\u7528\uff1a\u89c2\u5bdf\u8005\u6a21\u5f0f<\/h3>\n<pre><code class=\"language-csharp line-numbers\">public class Stock\n{\n    \/\/ \u4e8b\u4ef6\n    public event Action&lt;decimal&gt; PriceChanged;\n\n    private decimal _price;\n    public decimal Price\n    {\n        get =&gt; _price;\n        set\n        {\n            if (_price != value)\n            {\n                _price = value;\n                \/\/ \u4ef7\u683c\u53d8\u5316\u65f6\u901a\u77e5\u6240\u6709\u8ba2\u9605\u8005\n                PriceChanged?.Invoke(_price);\n            }\n        }\n    }\n}\n\n\/\/ \u4f7f\u7528\nStock apple = new Stock { Price = 100 };\n\napple.PriceChanged += (newPrice) =&gt; \n    Console.WriteLine($\"\u4ef7\u683c\u53d8\u4e3a: {newPrice}\");\n\napple.Price = 105;  \/\/ \u89e6\u53d1\u4e8b\u4ef6\napple.Price = 105;  \/\/ \u4ef7\u683c\u6ca1\u53d8\uff0c\u4e0d\u89e6\u53d1\n\n\/\/ \u8f93\u51fa: \u4ef7\u683c\u53d8\u4e3a: 105\n<\/code><\/pre>\n<hr \/>\n<h2>8. Lambda \u8868\u8fbe\u5f0f<\/h2>\n<p>Lambda \u662f<strong>\u533f\u540d\u51fd\u6570<\/strong>\u7684\u7b80\u5199\u8bed\u6cd5\uff0c\u5e38\u7528\u4e8e\u59d4\u6258\u3002<\/p>\n<h3>\u57fa\u672c\u8bed\u6cd5<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u5b8c\u6574\u5199\u6cd5\nFunc&lt;int, int, int&gt; add = (int a, int b) =&gt; { return a + b; };\n\n\/\/ \u7b80\u5316\uff1a\u7c7b\u578b\u63a8\u65ad\uff08\u6700\u5e38\u7528\uff09\nFunc&lt;int, int, int&gt; add2 = (a, b) =&gt; { return a + b; };\n\n\/\/ \u7b80\u5316\uff1a\u5355\u8868\u8fbe\u5f0f\u53ef\u7701\u7565 return \u548c {}\nFunc&lt;int, int, int&gt; add3 = (a, b) =&gt; a + b;\n\n\/\/ \u5355\u53c2\u6570\u53ef\u7701\u7565\u62ec\u53f7\nPredicate&lt;int&gt; isPositive = n =&gt; n &gt; 0;\n\n\/\/ \u65e0\u53c2\u6570\nAction greet = () =&gt; Console.WriteLine(\"\u4f60\u597d\uff01\");\n<\/code><\/pre>\n<h3>Lambda \u914d\u5408\u59d4\u6258\u4f7f\u7528<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u4f20\u7edf\u65b9\u6cd5\npublic bool IsEven(int n) { return n % 2 == 0; }\nList&lt;int&gt; nums = new List&lt;int&gt; { 1, 2, 3, 4, 5 };\n\n\/\/ \u7528 Lambda \u8fc7\u6ee4\nList&lt;int&gt; evens = nums.FindAll(n =&gt; n % 2 == 0);\nforeach (var n in evens) Console.WriteLine(n);\n\/\/ \u8f93\u51fa: 2, 4\n\n\/\/ \u7528 Lambda \u6392\u5e8f\nnums.Sort((a, b) =&gt; b.CompareTo(a));\n\/\/ \u4ece\u5927\u5230\u5c0f\u6392\u5e8f: 5, 4, 3, 2, 1\n<\/code><\/pre>\n<h3>Lambda \u5728 LINQ \u4e2d\u7684\u5e94\u7528<\/h3>\n<pre><code class=\"language-csharp line-numbers\">List&lt;int&gt; numbers = new List&lt;int&gt; { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };\n\nvar result = numbers\n    .Where(n =&gt; n % 2 == 0)           \/\/ \u8fc7\u6ee4\u5076\u6570\n    .Select(n =&gt; n * n)               \/\/ \u5e73\u65b9\n    .OrderByDescending(n =&gt; n)       \/\/ \u964d\u5e8f\u6392\u5217\n    .Take(3);                         \/\/ \u53d6\u524d3\u4e2a\n\nforeach (var n in result)\n    Console.WriteLine(n);\n\/\/ \u8f93\u51fa: 100, 64, 36\n<\/code><\/pre>\n<hr \/>\n<h2>9. in \/ out \/ ref \u53c2\u6570\u4fee\u9970\u7b26<\/h2>\n<h3>ref - \u4f20\u5165\u5f15\u7528\uff0c\u53ef\u8bfb\u53ef\u5199<\/h3>\n<pre><code class=\"language-csharp line-numbers\">public void Swap(ref int a, ref int b)\n{\n    int temp = a;\n    a = b;\n    b = temp;\n}\n\nint x = 5, y = 10;\nSwap(ref x, ref y);\nConsole.WriteLine($\"x={x}, y={y}\");  \/\/ x=10, y=5\n<\/code><\/pre>\n<h3>out - \u4f20\u51fa\u53c2\u6570\uff0c\u5fc5\u987b\u8fd4\u56de<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u89e3\u6790\u5b57\u7b26\u4e32\u8fd4\u56de\u591a\u4e2a\u503c\npublic bool TryParse(string input, out int result)\n{\n    try\n    {\n        result = int.Parse(input);\n        return true;\n    }\n    catch\n    {\n        result = 0;\n        return false;\n    }\n}\n\nif (int.TryParse(\"123\", out int number))\n{\n    Console.WriteLine($\"\u89e3\u6790\u6210\u529f: {number}\");\n}\nelse\n{\n    Console.WriteLine(\"\u89e3\u6790\u5931\u8d25\");\n}\n<\/code><\/pre>\n<h3>in - \u53ea\u8bfb\u4f20\u5165\uff08\u6027\u80fd\u4f18\u5316\uff09<\/h3>\n<pre><code class=\"language-csharp line-numbers\">public double CalculateDistance(in Point a, in Point b)\n{\n    \/\/ a \u548c b \u5728\u65b9\u6cd5\u5185\u53ea\u80fd\u8bfb\u53d6\uff0c\u4e0d\u80fd\u4fee\u6539\n    int dx = a.X - b.X;  \/\/ \u2713 \u53ef\u4ee5\u8bfb\u53d6\n    \/\/ a.X = 100;         \/\/ \u274c \u7f16\u8bd1\u9519\u8bef\uff01\u4e0d\u80fd\u4fee\u6539\n    return Math.Sqrt(dx * dx + (a.Y - b.Y) * (a.Y - b.Y));\n}\n\n\/\/ \u4f18\u70b9\uff1a\u544a\u8bc9\u7f16\u8bd1\u5668\u53c2\u6570\u4e0d\u4f1a\u4fee\u6539\uff0c\u53ef\u4ee5\u5b89\u5168\u5730\u8fdb\u884c\u4f18\u5316\n<\/code><\/pre>\n<h3>\u4e09\u8005\u5bf9\u6bd4<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u4fee\u9970\u7b26<\/th>\n<th>\u65b9\u5411<\/th>\n<th>\u65b9\u6cd5\u5185\u80fd\u4fee\u6539<\/th>\n<th>\u8c03\u7528\u65f6\u5fc5\u987b\u521d\u59cb\u5316<\/th>\n<th>\u8c03\u7528\u65f6\u5fc5\u987b\u52a0\u4fee\u9970\u7b26<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u65e0<\/td>\n<td>\u503c\u4f20\u9012<\/td>\n<td>\u274c<\/td>\n<td>\u53c2\u6570\u9700\u8981<\/td>\n<td>\u4e0d\u9700\u8981<\/td>\n<\/tr>\n<tr>\n<td><code>ref<\/code><\/td>\n<td>\u53cc\u5411<\/td>\n<td>\u2705<\/td>\n<td>\u53c2\u6570\u9700\u8981<\/td>\n<td>\u9700\u8981 (ref)<\/td>\n<\/tr>\n<tr>\n<td><code>out<\/code><\/td>\n<td>\u4ec5\u8f93\u51fa<\/td>\n<td>\u2705<\/td>\n<td>\u53c2\u6570<strong>\u4e0d\u9700\u8981<\/strong><\/td>\n<td>\u9700\u8981 (out)<\/td>\n<\/tr>\n<tr>\n<td><code>in<\/code><\/td>\n<td>\u4ec5\u8f93\u5165<\/td>\n<td>\u274c<\/td>\n<td>\u53c2\u6570\u9700\u8981<\/td>\n<td>\u9700\u8981 (in)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2>10. \u6cdb\u578b (Generic)<\/h2>\n<p>\u6cdb\u578b\u8ba9\u4f60\u7f16\u5199<strong>\u53ef\u590d\u7528<\/strong>\u7684\u4ee3\u7801\uff0c\u540c\u65f6\u4fdd\u6301<strong>\u7c7b\u578b\u5b89\u5168<\/strong>\u3002<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u9700\u8981\u6cdb\u578b\uff1f<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u274c \u95ee\u9898\uff1aArrayList \u4e27\u5931\u7c7b\u578b\u4fe1\u606f\nArrayList list = new ArrayList();\nlist.Add(1);\nlist.Add(\"hello\");        \/\/ \u9519\u8bef\u8fd0\u884c\u65f6\u624d\u53d1\u73b0\nstring s = (string)list[0]; \/\/ \u8fd0\u884c\u65f6\u5f02\u5e38\uff01\n\n\/\/ \u2705 \u89e3\u51b3\uff1a\u6cdb\u578b - \u7f16\u8bd1\u65f6\u7c7b\u578b\u68c0\u67e5\nList&lt;int&gt; list2 = new List&lt;int&gt;();\nlist2.Add(1);\nlist2.Add(\"hello\");       \/\/ \u274c \u7f16\u8bd1\u9519\u8bef\uff01\nint num = list2[0];       \/\/ \u2705 \u4e0d\u9700\u8981\u8f6c\u6362\n<\/code><\/pre>\n<h3>\u57fa\u672c\u8bed\u6cd5<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u5b9a\u4e49\u6cdb\u578b\u7c7b\npublic class Container&lt;T&gt;\n{\n    private T _item;\n\n    public T Item\n    {\n        get =&gt; _item;\n        set =&gt; _item = value;\n    }\n}\n\n\/\/ \u4f7f\u7528\nContainer&lt;int&gt; intContainer = new Container&lt;int&gt;();\nintContainer.Item = 123;\n\nContainer&lt;string&gt; strContainer = new Container&lt;string&gt;();\nstrContainer.Item = \"\u4f60\u597d\";\n<\/code><\/pre>\n<h3>\u6cdb\u578b\u65b9\u6cd5<\/h3>\n<pre><code class=\"language-csharp line-numbers\">public T Max&lt;T&gt;(T a, T b) where T : IComparable&lt;T&gt;\n{\n    return a.CompareTo(b) &gt; 0 ? a : b;\n}\n\nConsole.WriteLine(Max(3, 5));           \/\/ 5\nConsole.WriteLine(Max(\"apple\", \"banana\"));  \/\/ banana\n<\/code><\/pre>\n<h3>\u6cdb\u578b\u7ea6\u675f<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ where T : class        T \u5fc5\u987b\u662f\u5f15\u7528\u7c7b\u578b\n\/\/ where T : struct       T \u5fc5\u987b\u662f\u503c\u7c7b\u578b\n\/\/ where T : new()        T \u5fc5\u987b\u6709\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\n\/\/ where T : Person       T \u5fc5\u987b\u662f Person \u6216\u5176\u5b50\u7c7b\n\/\/ where T : IComparable  T \u5fc5\u987b\u5b9e\u73b0 IComparable \u63a5\u53e3\n\npublic class Repository&lt;T&gt; where T : class, new()\n{\n    public List&lt;T&gt; GetAll()\n    {\n        return new List&lt;T&gt;();  \/\/ \u56e0\u4e3a\u6709 new() \u7ea6\u675f\uff0c\u53ef\u4ee5\u521b\u5efa\u5b9e\u4f8b\n    }\n}\n<\/code><\/pre>\n<h3>\u5e38\u7528\u6cdb\u578b\u7c7b\u578b<\/h3>\n<pre><code class=\"language-csharp line-numbers\">\/\/ \u6cdb\u578b\u96c6\u5408\nList&lt;T&gt;           \/\/ \u52a8\u6001\u6570\u7ec4\nDictionary&lt;TKey, TValue&gt;  \/\/ \u952e\u503c\u5bf9\nHashSet&lt;T&gt;        \/\/ \u4e0d\u91cd\u590d\u96c6\u5408\nQueue&lt;T&gt;          \/\/ \u961f\u5217 (\u5148\u8fdb\u5148\u51fa)\nStack&lt;T&gt;          \/\/ \u6808 (\u5148\u8fdb\u540e\u51fa)\n\n\/\/ \u6cdb\u578b\u7c7b\/\u63a5\u53e3\nIComparable&lt;T&gt;\nIEnumerable&lt;T&gt;\nIComparer&lt;T&gt;\n<\/code><\/pre>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>C# \u8fdb\u9636\u77e5\u8bc6\u70b9\u603b\u7ed3 \u9002\u5408\u521d\u5b66\u8005\u7684C#\u8fdb\u9636\u6307\u5357 \u76ee\u5f55 \u679a\u4e3e (enum) \u7ed3\u6784\u4f53 (struct) \u8fd0\u7b97\u7b26\u91cd\u8f7d \u88c5\u7bb1\u4e0e\u62c6\u7bb1 Arr &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":1,"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":124,"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions\/124"}],"wp:attachment":[{"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}