{"id":1089,"date":"2025-05-22T14:42:03","date_gmt":"2025-05-22T06:42:03","guid":{"rendered":"https:\/\/www.hyy.net\/?p=1089"},"modified":"2025-05-22T14:42:03","modified_gmt":"2025-05-22T06:42:03","slug":"csharp-extension-methods","status":"publish","type":"post","link":"https:\/\/diji.net\/?p=1089","title":{"rendered":"C# \u6269\u5c55\u65b9\u6cd5"},"content":{"rendered":"<p>C# \u6269\u5c55\u65b9\u6cd5<\/p>\n<h2>\u7b80\u4ecb<\/h2>\n<p>\u5728 C# \u4e2d\uff0c\u6269\u5c55\u65b9\u6cd5\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u9759\u6001\u65b9\u6cd5\uff0c\u53ef\u4ee5\u50cf\u5b9e\u4f8b\u65b9\u6cd5\u4e00\u6837\u8c03\u7528\uff0c\u4f46\u5b9e\u9645\u4e0a\u662f\u9759\u6001\u7684\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6269\u5c55\u73b0\u6709\u7c7b\u578b\u7684\u529f\u80fd\uff0c\u800c\u65e0\u9700\u4fee\u6539\u7c7b\u578b\u7684\u5b9a\u4e49\u3002<\/p>\n<h2>\u5b9a\u4e49\u6269\u5c55\u65b9\u6cd5\u7684\u6b65\u9aa4<\/h2>\n<ol>\n<li>\u9759\u6001\u7c7b\uff1a\u6269\u5c55\u65b9\u6cd5\u5fc5\u987b\u5b9a\u4e49\u5728\u4e00\u4e2a\u9759\u6001\u7c7b\u4e2d\u3002<\/li>\n<li>\u9759\u6001\u65b9\u6cd5\uff1a\u6269\u5c55\u65b9\u6cd5\u672c\u8eab\u5fc5\u987b\u662f\u9759\u6001\u7684\u3002<\/li>\n<li>this \u53c2\u6570\uff1a\u6269\u5c55\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u524d\u52a0\u4e0a this \u5173\u952e\u5b57\uff0c\u6307\u5b9a\u8981\u6269\u5c55\u7684\u7c7b\u578b\u3002<\/li>\n<\/ol>\n<h2>\u793a\u4f8b<\/h2>\n<h3>\u6269\u5c55\u5b57\u7b26\u4e32\u7c7b\u578b<\/h3>\n<p>\u6269\u5c55 string \u7c7b\u578b\uff0c\u6dfb\u52a0\u4e00\u4e2a\u65b9\u6cd5 ToReverse \u6765\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u53cd\u8f6c\u3002<\/p>\n<pre><code>public static class StringExtensions\n{\n    public static string ToReverse(this string str)\n    {\n        if (string.IsNullOrEmpty(str))\n            return str;\n\n        char[] charArray = str.ToCharArray();\n        Array.Reverse(charArray);\n        return new string(charArray);\n    }\n}\n\n\/\/ \u4f7f\u7528\u6269\u5c55\u65b9\u6cd5\nclass Program\n{\n    static void Main()\n    {\n        string text = &quot;hello&quot;;\n        string reversed = text.ToReverse(); \/\/ \u8c03\u7528\u6269\u5c55\u65b9\u6cd5\n        Console.WriteLine(reversed); \/\/ \u8f93\u51fa\uff1aolleh\n    }\n}<\/code><\/pre>\n<h3>\u6269\u5c55\u96c6\u5408\u7c7b\u578b<\/h3>\n<p>\u6269\u5c55 IEnumerable<T>\uff0c\u6dfb\u52a0\u4e00\u4e2a\u65b9\u6cd5 ToFormattedString\uff0c\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code>public static class CollectionExtensions\n{\n    public static string ToFormattedString&lt;T&gt;(this IEnumerable&lt;T&gt; collection)\n    {\n        return string.Join(&quot;, &quot;, collection);\n    }\n}\n\n\/\/ \u4f7f\u7528\u6269\u5c55\u65b9\u6cd5\nclass Program\n{\n    static void Main()\n    {\n        var numbers = new List&lt;int&gt; { 1, 2, 3, 4, 5 };\n        string result = numbers.ToFormattedString(); \/\/ \u8c03\u7528\u6269\u5c55\u65b9\u6cd5\n        Console.WriteLine(result); \/\/ \u8f93\u51fa\uff1a1, 2, 3, 4, 5\n    }\n}<\/code><\/pre>\n<h2>\u5e38\u7528\u573a\u666f<\/h2>\n<ol>\n<li>\u6269\u5c55\u6846\u67b6\u7c7b\u578b\uff1a\u4e3a\u7cfb\u7edf\u7c7b\u578b\uff08\u5982 string, int, DateTime\uff09\u6dfb\u52a0\u65b0\u65b9\u6cd5\u3002<\/li>\n<li>\u7b80\u5316\u4ee3\u7801\uff1a\u63d0\u4f9b\u66f4\u7b80\u6d01\u7684\u8c03\u7528\u65b9\u5f0f\u3002<\/li>\n<li>\u589e\u5f3a LINQ \u529f\u80fd\uff1a\u8bb8\u591a LINQ \u65b9\u6cd5\uff08\u5982 Where, Select\uff09\u672c\u8d28\u4e0a\u662f\u6269\u5c55\u65b9\u6cd5\u3002<\/li>\n<li>\u4ee3\u7801\u590d\u7528\uff1a\u4e3a\u901a\u7528\u64cd\u4f5c\u521b\u5efa\u5de5\u5177\u65b9\u6cd5\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\u3002<\/li>\n<\/ol>\n<h2>\u6ce8\u610f\u4e8b\u9879<\/h2>\n<ol>\n<li>\u4f18\u5148\u7ea7\uff1a\u5982\u679c\u5b9e\u4f8b\u65b9\u6cd5\u548c\u6269\u5c55\u65b9\u6cd5\u540c\u540d\uff0c\u5b9e\u4f8b\u65b9\u6cd5\u4f1a\u4f18\u5148\u8c03\u7528\u3002<\/li>\n<li>\u547d\u540d\u7a7a\u95f4\uff1a\u4f7f\u7528\u6269\u5c55\u65b9\u6cd5\u65f6\uff0c\u5fc5\u987b\u5f15\u5165\u5305\u542b\u6269\u5c55\u65b9\u6cd5\u7684\u547d\u540d\u7a7a\u95f4\u3002<\/li>\n<li>\u6cdb\u578b\u652f\u6301\uff1a\u6269\u5c55\u65b9\u6cd5\u53ef\u4ee5\u662f\u6cdb\u578b\u7684\uff0c\u7528\u4e8e\u66f4\u52a0\u901a\u7528\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<h2>\u6269\u5c55\u65b9\u6cd5\u7684\u5c40\u9650\u6027<\/h2>\n<p>\u2022  \u65e0\u6cd5\u91cd\u5199\u73b0\u6709\u65b9\u6cd5\u3002<br \/>\n\u2022  \u65e0\u6cd5\u6dfb\u52a0\u65b0\u5c5e\u6027\u3002<br \/>\n\u2022  \u4e0d\u80fd\u6539\u53d8\u7c7b\u578b\u7684\u884c\u4e3a\uff0c\u53ea\u80fd\u63d0\u4f9b\u989d\u5916\u7684\u65b9\u6cd5\u3002<\/p>\n<h2>\u6269\u5c55\u65b9\u6cd5\u7684\u4f18\u52bf<\/h2>\n<p>\u6269\u5c55\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u3001\u548c\u5f00\u53d1\u4f53\u9a8c\uff0c\u867d\u7136\u5176\u672c\u8d28\u4e0e\u8c03\u7528\u9759\u6001\u7c7b\u4e2d\u7684\u9759\u6001\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u6269\u5c55\u65b9\u6cd5\u5e26\u6765\u4e86\u663e\u8457\u7684\u4fbf\u5229\u548c\u4ee3\u7801\u8bed\u4e49\u4e0a\u7684\u6539\u5584\u3002<\/p>\n<ol>\n<li>\u66f4\u76f4\u89c2\u7684\u8c03\u7528\u65b9\u5f0f<\/li>\n<\/ol>\n<p>\u6269\u5c55\u65b9\u6cd5\u53ef\u4ee5\u50cf\u5b9e\u4f8b\u65b9\u6cd5\u4e00\u6837\u8c03\u7528\uff0c\u7b26\u5408\u81ea\u7136\u8bed\u8a00\u4e60\u60ef\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<pre><code>\/\/ \u9759\u6001\u65b9\u6cd5\u8c03\u7528\uff1a\nvar reversed = StringHelper.Reverse(&quot;hello&quot;);\n\/\/ \u6269\u5c55\u65b9\u6cd5\u8c03\u7528\uff1a\nvar reversed = &quot;hello&quot;.ToReverse();<\/code><\/pre>\n<ol start=\"2\">\n<li>\u66f4\u7b26\u5408\u9762\u5411\u5bf9\u8c61\u7684\u8bbe\u8ba1\u7406\u5ff5<\/li>\n<\/ol>\n<p>\u6269\u5c55\u65b9\u6cd5\u7684\u8c03\u7528\u65b9\u5f0f\u8ba9\u5916\u90e8\u64cd\u4f5c\u770b\u8d77\u6765\u50cf\u662f\u7c7b\u7684\u5b9e\u4f8b\u884c\u4e3a\uff0c\u7b26\u5408\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u4e60\u60ef\u3002\u8fd9\u6837\u65e0\u9700\u4fee\u6539\u7c7b\u7684\u5b9a\u4e49\u5373\u53ef\u6269\u5c55\u5176\u529f\u80fd\u3002<\/p>\n<h3>\u7b80\u5316\u94fe\u5f0f\u8c03\u7528<\/h3>\n<p>\u6269\u5c55\u65b9\u6cd5\u652f\u6301\u94fe\u5f0f\u8c03\u7528\uff0c\u7b80\u5316\u4e86\u64cd\u4f5c\u6b65\u9aa4\uff0c\u8ba9\u4ee3\u7801\u66f4\u52a0\u7d27\u51d1\u3002<\/p>\n<pre><code>\/\/ \u4f7f\u7528\u6269\u5c55\u65b9\u6cd5\uff1a\nvar result = &quot;hello&quot;.ToReverse().ToUpper();\n\n\/\/ \u4f7f\u7528\u9759\u6001\u65b9\u6cd5\uff1a\nvar reversed = StringHelper.Reverse(&quot;hello&quot;);\nvar result = StringHelper.ToUpper(reversed);<\/code><\/pre>\n<h3>\u4e0e LINQ \u7d27\u5bc6\u96c6\u6210<\/h3>\n<p>\u8bb8\u591a LINQ \u65b9\u6cd5\uff08\u5982 Where, Select, OrderBy\uff09\u90fd\u662f\u6269\u5c55\u65b9\u6cd5\uff0c\u8c03\u7528\u65b9\u5f0f\u975e\u5e38\u6d41\u7545\u3002<\/p>\n<pre><code>\/\/ \u6269\u5c55\u65b9\u6cd5\uff1a\nvar result = numbers.Where(x =&gt; x &gt; 10).OrderBy(x =&gt; x).ToList();\n\n\/\/ \u9759\u6001\u65b9\u6cd5\uff1a\nvar filtered = LinqHelper.Where(numbers, x =&gt; x &gt; 10);\nvar ordered = LinqHelper.OrderBy(filtered, x =&gt; x);\nvar result = LinqHelper.ToList(ordered);<\/code><\/pre>\n<ol start=\"3\">\n<li>\u63d0\u9ad8\u5f00\u53d1\u4f53\u9a8c<\/li>\n<\/ol>\n<p>\u6269\u5c55\u65b9\u6cd5\u5728\u8c03\u7528\u65f6\u652f\u6301\u667a\u80fd\u63d0\u793a\uff0c\u80fd\u76f4\u63a5\u663e\u793a\u53ef\u7528\u7684\u6269\u5c55\u65b9\u6cd5\uff0c\u8ba9\u5f00\u53d1\u66f4\u9ad8\u6548\u3002<br \/>\n\u2022  \u9759\u6001\u65b9\u6cd5\uff1a \u5f00\u53d1\u8005\u9700\u8981\u8bb0\u4f4f\u9759\u6001\u7c7b\u540d\u79f0\u3002<br \/>\n\u2022  \u6269\u5c55\u65b9\u6cd5\uff1a \u5f00\u53d1\u8005\u53ea\u9700\u805a\u7126\u4e8e\u5bf9\u8c61\uff0cIDE \u81ea\u52a8\u63d0\u793a\u53ef\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C# \u6269\u5c55\u65b9\u6cd5 \u7b80\u4ecb \u5728 C# \u4e2d\uff0c\u6269\u5c55\u65b9\u6cd5\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u9759\u6001\u65b9\u6cd5\uff0c\u53ef\u4ee5\u50cf\u5b9e\u4f8b\u65b9\u6cd5\u4e00\u6837\u8c03\u7528\uff0c\u4f46\u5b9e\u9645\u4e0a\u662f\u9759\u6001\u7684\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6269\u5c55\u73b0\u6709\u7c7b\u578b\u7684\u529f\u80fd\uff0c\u800c\u65e0\u9700\u4fee\u6539\u7c7b\u578b\u7684\u5b9a\u4e49\u3002 \u5b9a\u4e49\u6269\u5c55\u65b9\u6cd5\u7684\u6b65\u9aa4 \u9759\u6001\u7c7b\uff1a\u6269\u5c55\u65b9\u6cd5\u5fc5\u987b\u5b9a\u4e49\u5728\u4e00\u4e2a\u9759\u6001\u7c7b\u4e2d\u3002 \u9759\u6001\u65b9\u6cd5\uff1a\u6269\u5c55\u65b9\u6cd5\u672c\u8eab\u5fc5\u987b\u662f\u9759\u6001\u7684\u3002 this \u53c2\u6570\uff1a\u6269\u5c55\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u524d\u52a0\u4e0a this \u5173\u952e\u5b57\uff0c\u6307\u5b9a\u8981\u6269\u5c55\u7684\u7c7b\u578b\u3002 \u793a\u4f8b \u6269\u5c55\u5b57\u7b26\u4e32\u7c7b\u578b \u6269\u5c55 string \u7c7b\u578b\uff0c\u6dfb\u52a0\u4e00\u4e2a\u65b9\u6cd5 ToReverse \u6765\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u53cd\u8f6c\u3002 public static class StringExtensions { public static string ToReverse(this string str) { if (string.IsNullOrEmpty(str)) return str; char[] charArray = str.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } } \/\/ \u4f7f\u7528\u6269\u5c55\u65b9\u6cd5 class Program { static void Main() { [&hellip;]<\/p>\n","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-1089","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/1089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1089"}],"version-history":[{"count":0,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/1089\/revisions"}],"wp:attachment":[{"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}