{"id":411,"date":"2017-02-23T10:26:27","date_gmt":"2017-02-23T02:26:27","guid":{"rendered":"https:\/\/blog.freesilo.com\/?p=411"},"modified":"2017-02-23T10:26:50","modified_gmt":"2017-02-23T02:26:50","slug":"%e7%bc%96%e5%86%99%e6%9b%b4%e5%a5%bd%e7%9a%84c%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/freesilo.com\/?p=411","title":{"rendered":"\u7f16\u5199\u66f4\u597d\u7684C#\u4ee3\u7801"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>\u5f00\u53d1\u4eba\u5458\u603b\u662f\u559c\u6b22\u5c31\u7f16\u7801\u89c4\u8303\u8fdb\u884c\u4e89\u8bba\uff0c\u4f46\u66f4\u91cd\u8981\u7684\u662f\u5982\u4f55\u80fd\u591f\u5728\u9879\u76ee\u4e2d\u81ea\u59cb\u81f3\u7ec8\u5730\u9075\u5faa\u7f16\u7801\u89c4\u8303\uff0c\u4ee5\u4fdd\u8bc1\u9879\u76ee\u4ee3\u7801\u7684\u4e00\u81f4\u6027\u3002\u5e76\u4e14\u56e2\u961f\u4e2d\u7684\u6240\u6709\u4eba\u90fd\u9700\u8981\u660e\u786e\u7f16\u7801\u89c4\u8303\u6240\u8d77\u5230\u7684\u4f5c\u7528\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4f1a\u4ecb\u7ecd\u4e00\u4e9b\u5728\u6211\u591a\u5e74\u7684\u4ece\u4e1a\u8fc7\u7a0b\u4e2d\u6240\u5b66\u4e60\u548c\u603b\u7ed3\u7684\u4e00\u4e9b\u8f83\u597d\u7684\u5b9e\u8df5\u3002<\/p>\n<h2>\u4e3e\u4f8b\u4e3a\u5148<\/h2>\n<p>\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e2a FizzBuzz \u793a\u4f8b\u3002FizzBuzz \u8981\u6c42\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u904d\u5386\u4ece 1 \u5230 100 \u7684\u6570\u5b57\u3002\u5176\u4e2d\u5982\u679c\u67d0\u6570\u5b57\u662f 3 \u7684\u500d\u6570\uff0c\u5219\u7a0b\u5e8f\u8f93\u51fa \u201cFizz\u201d\u3002\u5982\u679c\u67d0\u6570\u5b57\u662f 5 \u7684\u500d\u6570\uff0c\u5219\u8f93\u51fa \u201cBuzz\u201d\u3002\u5982\u679c\u67d0\u6570\u5b57\u5373\u662f 3 \u7684\u500d\u6570\u4e5f\u662f 5 \u7684\u500d\u6570\uff0c\u5219\u8f93\u51fa \u201cFizzBuzz\u201d\u3002\u5982\u679c\u6570\u5b57\u65e2\u4e0d\u662f 3 \u7684\u500d\u6570\u4e5f\u4e0d\u662f 5 \u7684\u500d\u6570\uff0c\u5219\u53ea\u9700\u8f93\u51fa\u8be5\u6570\u5b57\u672c\u8eab\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-412\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/02214012-bcab190c1c724d7ea9455e701fa2ad47001.jpg\" alt=\"02214012-bcab190c1c724d7ea9455e701fa2ad47001\" width=\"246\" height=\"205\" \/><\/p>\n<p><strong>\u793a\u4f8b1\uff1a<\/strong><\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<p>1 public static void Test()<br \/>\n2 {<br \/>\n3 for (int i = 1; i &lt; 101; i++)<br \/>\n4 {<br \/>\n5 if (i % 3 == 0 &amp;&amp; i % 5 == 0)<br \/>\n6 {<br \/>\n7 Console.WriteLine(&#8220;FizzBuzz&#8221;);<br \/>\n8 }<br \/>\n9 else if (i % 3 == 0)<br \/>\n10 {<br \/>\n11 Console.WriteLine(&#8220;Fizz&#8221;);<br \/>\n12 }<br \/>\n13 else if (i % 5 == 0)<br \/>\n14 {<br \/>\n15 Console.WriteLine(&#8220;Buzz&#8221;);<br \/>\n16 }<br \/>\n17 else<br \/>\n18 {<br \/>\n19 Console.WriteLine(i);<br \/>\n20 }<br \/>\n21 }<br \/>\n22 }<\/p>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u4ec0\u4e48\u611f\u89c9\uff1f\u8fd9\u6bb5\u4ee3\u7801\u9700\u8981\u6539\u8fdb\u5417\uff1f<\/p>\n<p><strong>\u793a\u4f8b2\uff1a<\/strong><\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre> 1     public static void Check()\r\n 2     {\r\n 3       for (int i = 1; i &lt;= 100; i++)\r\n 4       {\r\n 5         string output = \"\";\r\n 6         if (i % 3 == 0) { output = \"Fizz\"; }\r\n 7         if (i % 5 == 0) { output = output + \"Buzz\"; }\r\n 8         if (output == \"\") { output = i.ToString(); }\r\n 9         Console.WriteLine(output);\r\n10       }\r\n11     }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u73b0\u5728\u611f\u89c9\u5982\u4f55\uff1f\u8fd8\u80fd\u4e0d\u80fd\u8fdb\u4e00\u6b65\u6539\u8fdb\uff1f<\/p>\n<p>\u597d\uff0c\u8ba9\u6211\u4eec\u6765\u5c1d\u8bd5\u6539\u8fdb\u4e0b\u3002\u4ee3\u7801\u547d\u540d\u5bf9\u6240\u6709\u8f6f\u4ef6\u5f00\u53d1\u4eba\u5458\u6765\u8bf4\u90fd\u662f\u4ef6\u975e\u5e38\u56f0\u96be\u7684\u4e8b\u60c5\u3002\u6211\u4eec\u82b1\u8d39\u4e86\u5927\u91cf\u7684\u65f6\u95f4\u6765\u505a\u8fd9\u4ef6\u4e8b\uff0c\u800c\u4e14\u6709\u592a\u591a\u7684\u9700\u8981\u88ab\u547d\u540d\u7684\u5143\u7d20\uff0c\u4f8b\u5982\u5c5e\u6027\u3001\u65b9\u6cd5\u3001\u7c7b\u3001\u6587\u4ef6\u3001\u9879\u76ee\u7b49\u3002\u4e0d\u8fc7\u6211\u4eec\u7684\u786e\u9700\u8981\u82b1\u8d39\u4e00\u4e9b\u7cbe\u529b\u5728\u8fd9\u4e9b\u547d\u540d\u4e0a\uff0c\u4ee5\u4f7f\u4ee3\u7801\u4e2d\u7684\u540d\u79f0\u66f4\u6709\u610f\u4e49\uff0c\u8fdb\u800c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre> 1     public void DoFizzBuzz()\r\n 2     {\r\n 3       for (int number = 1; number &lt;= 100; number++)\r\n 4       {\r\n 5         var output = GetFizzBuzzOutput(number);\r\n 6         Console.WriteLine(output);\r\n 7       }\r\n 8     }\r\n 9 \r\n10     private static string GetFizzBuzzOutput(int number)\r\n11     {\r\n12       string output = string.Empty;\r\n13       if (number % 3 == 0)\r\n14       {\r\n15         output = \"Fizz\";\r\n16       }\r\n17       if (number % 5 == 0)\r\n18       {\r\n19         output += \"Buzz\";\r\n20       }\r\n21       if (string.IsNullOrEmpty(output))\r\n22       {\r\n23         output = number.ToString();\r\n24       }\r\n25       return output;\r\n26     }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u8fd9\u6b21\u611f\u89c9\u600e\u6837\uff1f\u662f\u4e0d\u662f\u6bd4\u4e4b\u524d\u7684\u793a\u4f8b\u8981\u597d\u4e9b\uff1f\u662f\u4e0d\u662f\u53ef\u8bfb\u6027\u66f4\u597d\u4e9b\uff1f<\/p>\n<h2>\u4ec0\u4e48\u662f\u66f4\u597d\u7684\u4ee3\u7801\uff1f<\/h2>\n<p>\u9996\u5148\u5c31\u662f\u4ee3\u7801\u8981\u4e3a\u4eba\u6765\u7f16\u5199\uff0c\u5176\u6b21\u662f\u4e3a\u673a\u5668\u3002\u4ece\u957f\u671f\u6765\u770b\uff0c\u7f16\u5199\u53ef\u8bfb\u6027\u597d\u7684\u4ee3\u7801\u4e0d\u4f1a\u6bd4\u7f16\u5199\u6df7\u4e71\u7684\u4ee3\u7801\u8981\u82b1\u8d39\u66f4\u957f\u7684\u65f6\u95f4\u3002\u5982\u679c\u4f60\u80fd\u591f\u975e\u5e38\u5bb9\u6613\u5730\u8bfb\u61c2\u4f60\u5199\u7684\u4ee3\u7801\uff0c\u90a3\u4e48\u60f3\u786e\u8ba4\u5176\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u5c31\u66f4\u5bb9\u6613\u4e86\u3002\u8fd9\u5e94\u8be5\u5df2\u7ecf\u662f\u7f16\u5199\u6613\u8bfb\u4ee3\u7801\u8db3\u591f\u5145\u5206\u7684\u7406\u7531\u4e86\u3002\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u90fd\u9700\u8981\u9605\u8bfb\u4ee3\u7801\uff0c\u4f8b\u5982\u5728\u4ee3\u7801\u8bc4\u5ba1\u4e2d\u4f1a\u9605\u8bfb\u4f60\u5199\u7684\u4ee3\u7801\uff0c\u5728\u4f60\u6216\u8005\u5176\u4ed6\u4eba\u4fee\u590dBug\u65f6\u4f1a\u9605\u8bfb\u4f60\u5199\u7684\u4ee3\u7801\uff0c\u5728\u4ee3\u7801\u9700\u8981\u4fee\u6539\u65f6\u4e5f\u4f1a\u8bfb\u5230\u3002\u8fd8\u6709\u5c31\u662f\u5f53\u5176\u4ed6\u4eba\u51c6\u5907\u5728\u7c7b\u4f3c\u7684\u9879\u76ee\u6216\u6709\u7c7b\u4f3c\u529f\u80fd\u7684\u9879\u76ee\u4e2d\u5c1d\u8bd5\u590d\u7528\u4f60\u7684\u90e8\u5206\u4ee3\u7801\u65f6\u4e5f\u4f1a\u5148\u9605\u8bfb\u4f60\u7684\u4ee3\u7801\u3002<\/p>\n<p><em>\u201c\u5982\u679c\u4f60\u53ea\u4e3a\u4f60\u81ea\u5df1\u5199\u4ee3\u7801\uff0c\u4e3a\u4ec0\u4e48\u8981\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\uff1f\u201d<\/em><\/p>\n<p>\u597d\uff0c\u7f16\u5199\u6613\u8bfb\u7684\u4ee3\u7801\u6700\u4e3b\u8981\u7684\u539f\u56e0\u662f\uff0c\u5728\u672a\u6765\u7684\u4e00\u5230\u4e24\u5468\uff0c\u4f60\u5c06\u5de5\u4f5c\u5728\u53e6\u4e00\u4e2a\u9879\u76ee\u4e0a\u3002\u800c\u6b64\u65f6\uff0c\u6709\u5176\u4ed6\u4eba\u9700\u8981\u4fee\u590d\u5f53\u524d\u9879\u76ee\u7684\u4e00\u4e2aBug\uff0c\u90a3\u4e48\u5c06\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u6211\u6562\u4fdd\u8bc1\u4ed6\u80af\u5b9a\u4f1a\u8ff7\u5931\u5728\u4f60\u81ea\u5df1\u7f16\u5199\u7684\u6050\u6016\u4ee3\u7801\u4e2d\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-413\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/02222413-ba458b8e4fd745a88a99db9b56c26433002.jpg\" alt=\"02222413-ba458b8e4fd745a88a99db9b56c26433002\" width=\"600\" height=\"224\" srcset=\"https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/02222413-ba458b8e4fd745a88a99db9b56c26433002.jpg 600w, https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/02222413-ba458b8e4fd745a88a99db9b56c26433002-300x112.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>\u4ece\u6211\u7684\u4e2a\u4eba\u89c2\u70b9\u6765\u770b\uff0c\u597d\u7684\u4ee3\u7801\u5e94\u8be5\u62e5\u6709\u4ee5\u4e0b\u51e0\u4e2a\u7279\u5f81\uff1a<\/p>\n<ul>\n<li>\u4ee3\u7801\u5bb9\u6613\u7f16\u5199\uff0c\u5e76\u6613\u4e8e\u4fee\u6539\u548c\u6269\u5c55\u3002<\/li>\n<li>\u4ee3\u7801\u5e72\u51c0\uff0c\u5e76\u8868\u8ff0\u51c6\u786e\u3002<\/li>\n<li>\u4ee3\u7801\u6709\u4ef7\u503c\uff0c\u5e76\u6ce8\u91cd\u8d28\u91cf\u3002<\/li>\n<\/ul>\n<p>\u6240\u4ee5\uff0c\u8981\u65f6\u523b\u8003\u8651\u5148\u4e3a\u4eba\u6765\u7f16\u5199\u4ee3\u7801\uff0c\u7136\u540e\u518d\u6ee1\u8db3\u673a\u5668\u7684\u9700\u8981\u3002<\/p>\n<h2>\u5982\u4f55\u6539\u8fdb\u53ef\u8bfb\u6027\uff1f<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-414\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/02222437-2ae55902a5ca4ee8881ee141f1b01c40003.gif\" alt=\"02222437-2ae55902a5ca4ee8881ee141f1b01c40003\" width=\"214\" height=\"309\" \/><\/p>\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u9605\u8bfb\u5b66\u4e60\u5176\u4ed6\u4eba\u7f16\u5199\u7684\u4ee3\u7801\uff0c\u6765\u4e86\u89e3\u4ec0\u4e48\u662f\u597d\u7684\u4ee3\u7801\uff0c\u4ec0\u4e48\u662f\u4e0d\u597d\u7684\u4ee3\u7801\u3002\u4e5f\u5c31\u662f\u90a3\u4e9b\u4f60\u611f\u89c9\u975e\u5e38\u5bb9\u6613\u7406\u89e3\u7684\u4ee3\u7801\uff0c\u548c\u611f\u89c9\u770b\u8d77\u6765\u8d85\u7ea7\u590d\u6742\u7684\u4ee3\u7801\u3002\u7136\u540e\uff0c\u8fdb\u884c\u5b9e\u8df5\u3002\u6700\u540e\u82b1\u8d39\u4e00\u4e9b\u65f6\u95f4\u3001\u7ecf\u9a8c\u548c\u5b9e\u8df5\u6765\u6539\u8fdb\u4f60\u7684\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u4e00\u822c\u6765\u8bb2\u4ec5\u901a\u8fc7\u57f9\u8bad\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5728\u4efb\u4f55\u8f6f\u4ef6\u516c\u53f8\u4e2d\u63a8\u52a8\u7f16\u7801\u89c4\u8303\u90fd\u6709\u4e9b\u56f0\u96be\u3002\u800c\u8bf8\u5982\u7ed3\u5bf9\u4ee3\u7801\u8bc4\u5ba1\uff0c\u81ea\u52a8\u5316\u4ee3\u7801\u8bc4\u5ba1\u5de5\u5177\u7b49\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u3002\u76ee\u524d\u6d41\u884c\u7684\u5de5\u5177\u6709\uff1a<\/p>\n<ul>\n<li>FxCop\uff1a\u5bf9 .NET \u4ee3\u7801\u8fdb\u884c\u9759\u6001\u4ee3\u7801\u5206\u6790\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u89c4\u5219\u6765\u8fdb\u884c\u4e0d\u540c\u5f62\u5f0f\u7684\u5206\u6790\u3002<\/li>\n<li>StyleCop\uff1a\u5f00\u6e90\u9879\u76ee\uff0c\u5176\u4f7f\u7528\u4ee3\u7801\u98ce\u683c\u548c\u4e00\u81f4\u6027\u89c4\u8303\u6765\u5bf9\u5206\u6790C#\u4ee3\u7801\u3002\u53ef\u5728 Visual Studio \u4e2d\u8fd0\u884c\uff0c\u4e5f\u53ef\u4ee5\u96c6\u6210\u5230 MSBuild \u4e2d\u3002StyleCop \u4e5f\u5df2\u7ecf\u88ab\u96c6\u6210\u5230\u4e86\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5f00\u53d1\u5de5\u5177\u4e2d\u3002<\/li>\n<li>JetBrains ReSharper\uff1a\u975e\u5e38\u8457\u540d\u7684\u63d0\u5347\u751f\u4ea7\u529b\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u4f7f Microsoft Visual Studio IDE \u66f4\u52a0\u5f3a\u5927\u3002\u5168\u4e16\u754c\u7684 .NET \u5f00\u53d1\u4eba\u5458\u53ef\u80fd\u90fd\u65e0\u6cd5\u60f3\u8c61\uff0c\u5de5\u4f5c\u4e2d\u600e\u4e48\u80fd\u6ca1\u6709 ReSharper \u7684\u4ee3\u7801\u5ba1\u67e5\u3001\u4ee3\u7801\u81ea\u52a8\u91cd\u6784\u3001\u5feb\u901f\u5bfc\u822a\u548c\u7f16\u7801\u52a9\u624b\u7b49\u8fd9\u4e9b\u5f3a\u5927\u7684\u529f\u80fd\u5462\u3002<\/li>\n<\/ul>\n<h2>\u89c4\u8303\u662f\u4ec0\u4e48\uff1f<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-415\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/02224359-3915b3b26fec4d14bf1ce781fc4377df004.jpg\" alt=\"02224359-3915b3b26fec4d14bf1ce781fc4377df004\" width=\"392\" height=\"400\" srcset=\"https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/02224359-3915b3b26fec4d14bf1ce781fc4377df004.jpg 392w, https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/02224359-3915b3b26fec4d14bf1ce781fc4377df004-294x300.jpg 294w\" sizes=\"auto, (max-width: 392px) 100vw, 392px\" \/><\/p>\n<p>\u4f9d\u636e<a href=\"http:\/\/en.wikipedia.org\/wiki\/Coding_conventions\">\u7ef4\u57fa\u767e\u79d1\u4e0a\u7684\u63cf\u8ff0<\/a>\uff1a&#8221;<em>Coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices and methods for each aspect of a piece program written in this language. These conventions usually cover file organization, indentation, comments, declarations, statements, white space, naming conventions, programming practices, programming principles, programming rules of thumb, architectural best practices, etc. These are guidelines for software structural quality. Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Coding conventions are only applicable to the human maintainers and peer reviewers of a software project. Conventions may be formalized in a documented set of rules that an entire team or company follows, or may be as informal as the habitual coding practices of an individual. Coding conventions are not enforced by compilers. As a result, not following some or all of the rules has no impact on the executable programs created from the source code.<\/em>&#8220;\u3002<\/p>\n<p>\u4f60\u5e94\u8be5\u80fd\u8bf4\u51fa\u5c5e\u6027\u3001\u5c40\u90e8\u53d8\u91cf\u3001\u65b9\u6cd5\u540d\u3001\u7c7b\u540d\u7b49\u7684\u4e0d\u540c\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f7f\u7528\u4e0d\u540c\u7684\u5927\u5c0f\u5199\u7ea6\u5b9a\uff0c\u6240\u4ee5\u8fd9\u4e9b\u7ea6\u5b9a\u975e\u5e38\u6709\u4ef7\u503c\u3002\u901a\u8fc7\u4e92\u8054\u7f51\uff0c\u4f60\u5df2\u7ecf\u4e86\u89e3\u4e86\u5f88\u591a\u76f8\u5e94\u7684\u51c6\u5219\u548c\u89c4\u8303\uff0c\u4f60\u6240\u9700\u8981\u7684\u4ec5\u662f\u627e\u5230\u4e00\u79cd\u89c4\u8303\u6216\u8005\u5efa\u7acb\u4f60\u81ea\u5df1\u7684\u89c4\u8303\uff0c\u7136\u540e\u59cb\u7ec8\u9075\u5faa\u8be5\u89c4\u8303\u3002<\/p>\n<p>\u4e0b\u9762\u4f7f\u7528\u5230\u7684\u6e90\u4ee3\u7801\uff08<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms229042.aspx\">\u7c7b\u5e93\u8bbe\u8ba1\u51c6\u5219<\/a>\uff09\u662f\u7531\u5fae\u8f6f\u7684 Special Interest Group \u56e2\u961f\u5f00\u53d1\u7684\uff0c\u6211\u53ea\u662f\u505a\u4e86\u4e9b\u6269\u5c55\u3002<\/p>\n<h3>\u5927\u5c0f\u5199\u7ea6\u5b9a<\/h3>\n<p>\u4e0b\u9762\u662f\u4e00\u4e9b\u5173\u4e8eC#\u7f16\u7801\u6807\u51c6\u3001\u547d\u540d\u7ea6\u5b9a\u548c\u6700\u4f73\u5b9e\u8df5\u7684\u793a\u4f8b\uff0c\u53ef\u4ee5\u6839\u636e\u4f60\u81ea\u5df1\u7684\u9700\u8981\u6765\u4f7f\u7528\u3002<\/p>\n<p><strong>Pascal Casing<\/strong><\/p>\n<p>\u6807\u793a\u7b26\u4e2d\u7684\u9996\u5b57\u6bcd\uff0c\u540e\u7eed\u4e32\u8054\u7684\u6bcf\u4e2a\u5355\u8bcd\u7684\u9996\u5b57\u6bcd\u5747\u4e3a\u5927\u5199\u3002\u5982\u679c\u9700\u8981\uff0c\u6807\u793a\u7b26\u7684\u524d\u51e0\u4e2a\u5b57\u6bcd\u5747\u53ef\u5927\u5199\u3002<\/p>\n<p><strong>Camel Casing<\/strong><\/p>\n<p>\u6807\u793a\u7b26\u7684\u9996\u5b57\u6bcd\u4e3a\u5c0f\u5199\uff0c\u540e\u7eed\u4e32\u8054\u7684\u6bcf\u4e2a\u5355\u8bcd\u7684\u9996\u5b57\u6bcd\u4e3a\u5927\u5199\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-416\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/02230459-d11551b8215c434b91a7d01e738b4024005.jpg\" alt=\"02230459-d11551b8215c434b91a7d01e738b4024005\" width=\"508\" height=\"305\" srcset=\"https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/02230459-d11551b8215c434b91a7d01e738b4024005.jpg 508w, https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/02230459-d11551b8215c434b91a7d01e738b4024005-300x180.jpg 300w\" sizes=\"auto, (max-width: 508px) 100vw, 508px\" \/><\/p>\n<p>\u53c2\u8003\uff1a<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms229043.aspx\">\u6807\u793a\u7b26\u5927\u5c0f\u5199\u89c4\u5219<\/a><\/p>\n<h2>\u4e00\u4e9b\u547d\u540d\u7ea6\u5b9a\u793a\u4f8b<\/h2>\n<p>\u5728\u4e92\u8054\u7f51\u4e0a\u4f60\u53ef\u4ee5\u627e\u5230\u8db3\u591f\u591a\u7684\u8d44\u6e90\uff0c\u6211\u53ea\u662f\u63a8\u8350\u51e0\u4e2a\u5176\u4e2d\u6211\u6700\u559c\u6b22\u7684\uff1a<\/p>\n<ul>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/vstudio\/ff926074.aspx\">C# \u7f16\u7801\u7ea6\u5b9a<\/a><\/li>\n<li><a href=\"http:\/\/csharpguidelines.codeplex.com\/\">C# \u7f16\u7801\u51c6\u5219<\/a><\/li>\n<li><a href=\"http:\/\/www.dotnetspider.com\/tutorials\/BestPractices.aspx\">C# \u7f16\u7801\u6807\u51c6\u548c\u6700\u4f73\u5b9e\u8df5<\/a><\/li>\n<li><a href=\"http:\/\/www.dofactory.com\/reference\/csharp-coding-standards.aspx\">C# \u7f16\u7801\u89c4\u8303\u548c\u547d\u540d\u7ea6\u5b9a<\/a><\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u6211\u5c55\u793a\u4e86\u4e00\u4e9b\u6700\u57fa\u672c\u7684\u793a\u4f8b\uff0c\u4f46\u5c31\u50cf\u6211\u4e0a\u9762\u5df2\u7ecf\u63d0\u5230\u7684\uff0c\u627e\u5230\u4e00\u4e2a\u9002\u5408\u4f60\u7684\u89c4\u8303\uff0c\u7136\u540e\u575a\u6301\u4f7f\u7528\u3002<\/p>\n<p><strong>\u8981<\/strong>\u4f7f\u7528 Pascal Casing \u4e3a\u7c7b\u548c\u65b9\u6cd5\u547d\u540d\u3002<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>    public class Product\r\n    {\r\n      public void GetActiveProducts()\r\n      {\r\n        \/\/...\r\n      }\r\n      public void CalculateProductAdditinalCost()\r\n      {\r\n        \/\/...\r\n      }\r\n    }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p><strong>\u8981<\/strong>\u4f7f\u7528 Camel Casing \u4e3a\u65b9\u6cd5\u7684\u53c2\u6570\u548c\u5c40\u90e8\u53d8\u91cf\u547d\u540d\u3002<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>    public class ProductCategory\r\n    {\r\n      public void Save(ProductCategory productCategory)\r\n      {\r\n        \/\/ ...\r\n      }\r\n    }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p><strong>\u4e0d\u8981<\/strong>\u4f7f\u7528\u7f29\u5199\u8bed\u3002<\/p>\n<div class=\"cnblogs_code\">\n<pre>    \/\/ Correct\r\n    ProductCategory productCategory;\r\n\r\n    \/\/ Avoid\r\n    ProductCategory prodCat;<\/pre>\n<\/div>\n<p><strong>\u4e0d\u8981<\/strong>\u5728\u6807\u793a\u7b26\u4e2d\u4f7f\u7528\u4e0b\u5212\u7ebf\u3002<\/p>\n<div class=\"cnblogs_code\">\n<pre>    \/\/ Correct\r\n    ProductCategory productCategory;\r\n\r\n    \/\/ Avoid\r\n    ProductCategory product_Category;<\/pre>\n<\/div>\n<p><strong>\u8981<\/strong>\u5728\u63a5\u53e3\u540d\u79f0\u524d\u4f7f\u7528\u5b57\u6bcd I \u3002<\/p>\n<div class=\"cnblogs_code\">\n<pre>    public interface IAddress\r\n    {\r\n    }<\/pre>\n<\/div>\n<p><strong>\u8981<\/strong>\u5728\u7c7b\u7684\u9876\u7aef\u5b9a\u4e49\u6240\u6709\u6210\u5458\u53d8\u91cf\uff0c\u5728\u6700\u9876\u7aef\u5b9a\u4e49\u9759\u6001\u53d8\u91cf\u3002<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>  public class Product\r\n  {\r\n    public static string BrandName;\r\n\r\n    public string Name { get; set; }\r\n    public DateTime DateAvailable { get; set; }\r\n\r\n    public Product()\r\n    {\r\n      \/\/ ...\r\n    }\r\n  }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p><strong>\u8981<\/strong>\u4f7f\u7528\u5355\u6570\u7684\u8bcd\u6c47\u5b9a\u4e49\u679a\u4e3e\uff0c\u9664\u975e\u662fBitField\u679a\u4e3e\u3002<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>  public enum Direction\r\n  {\r\n    North,\r\n    East,\r\n    South,\r\n    West\r\n  }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p><strong>\u4e0d\u8981<\/strong>\u4e3a\u679a\u4e3e\u540d\u79f0\u6dfb\u52a0Enum\u540e\u7f00\u3002<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>  \/\/Avoid\r\n  public enum DirectionEnum\r\n  {\r\n    North,\r\n    East,\r\n    South,\r\n    West\r\n  }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<h2>\u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981\u7f16\u7801\u89c4\u8303\uff1f<\/h2>\n<p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u5f00\u53d1\u4eba\u5458\u4f1a\u5e38\u4f9d\u8d56\u4e8e\u7f16\u7801\u89c4\u8303\u3002\u4ed6\u4eec\u5efa\u7acb\u4e86\u5f88\u591a\u89c4\u8303\u548c\u51c6\u5219\uff0c\u4ee5\u81f3\u4e8e\u8bb0\u4f4f\u8fd9\u4e9b\u89c4\u8303\u548c\u51c6\u5219\u5df2\u7ecf\u53d8\u6210\u4e86\u65e5\u5e38\u5de5\u4f5c\u7684\u4e00\u90e8\u5206\u3002\u8ba1\u7b97\u673a\u5e76\u4e0d\u5173\u5fc3\u4f60\u5199\u7684\u4ee3\u7801\u53ef\u8bfb\u6027\u662f\u5426\u597d\uff0c\u6bd4\u8d77\u8bfb\u61c2\u90a3\u4e9b\u9ad8\u7ea7\u7684\u7a0b\u5e8f\u8bed\u8a00\u8bed\u53e5\uff0c\u8ba1\u7b97\u673a\u66f4\u5bb9\u6613\u7406\u89e3\u4e8c\u8fdb\u5236\u7684\u673a\u5668\u6307\u4ee4\u3002<\/p>\n<p>\u7f16\u7801\u89c4\u8303\u63d0\u4f9b\u4e86\u5f88\u591a\u660e\u663e\u7684\u597d\u5904\uff0c\u5f53\u7136\u6709\u53ef\u80fd\u4f60\u5f97\u5230\u7684\u66f4\u591a\u3002\u901a\u5e38\u8fd9\u4e9b\u9879\u76ee\u6574\u4f53\u8303\u56f4\u7684\u89c4\u5212\uff0c\u5c06\u4f7f\u80fd\u591f\u5c06\u7cbe\u529b\u66f4\u591a\u7684\u96c6\u4e2d\u5728\u4ee3\u7801\u4e2d\u66f4\u91cd\u8981\u7684\u90e8\u5206\u4e0a\u3002<\/p>\n<ul>\n<li>\u7f16\u7801\u89c4\u8303\u53ef\u4ee5\u5e2e\u52a9\u8de8\u9879\u76ee\u7684\u4f20\u9012\u77e5\u8bc6\u3002<\/li>\n<li>\u7f16\u7801\u89c4\u8303\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u65b0\u7684\u9879\u76ee\u4e0a\u66f4\u5feb\u901f\u7684\u7406\u89e3\u4ee3\u7801\u3002<\/li>\n<li>\u7f16\u7801\u89c4\u8303\u5f3a\u8c03\u7ec4\u7ec7\u4e2d\u5173\u8054\u9879\u76ee\u95f4\u7684\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<p>\u4f60\u9700\u8981\u7f16\u5199\u53ef\u8bfb\u6027\u9ad8\u7684\u4ee3\u7801\uff0c\u4ee5\u6b64\u6765\u5e2e\u52a9\u5176\u4ed6\u4eba\u6765\u7406\u89e3\u4f60\u7684\u4ee3\u7801\u3002\u4ee3\u7801\u547d\u540d\u5bf9\u6211\u4eec\u8f6f\u4ef6\u5f00\u53d1\u4eba\u5458\u6765\u8bf4\u662f\u4ef6\u975e\u5e38\u56f0\u96be\u7684\u4e8b\u60c5\uff0c\u6211\u4eec\u5728\u8fd9\u4e0a\u9762\u5df2\u7ecf\u82b1\u8d39\u4e86\u5927\u91cf\u7684\u65f6\u95f4\uff0c\u5e76\u4e14\u6709\u592a\u591a\u7684\u9700\u8981\u547d\u540d\u7684\u5143\u7d20\uff0c\u4f8b\u5982\u5c5e\u6027\u3001\u65b9\u6cd5\u3001\u7c7b\u3001\u6587\u4ef6\u3001\u9879\u76ee\u7b49\u3002\u6240\u4ee5\u6211\u4eec\u786e\u5b9e\u9700\u8981\u82b1\u8d39\u4e00\u4e9b\u7cbe\u529b\u5728\u547d\u540d\u89c4\u8303\u4e0a\uff0c\u4ee5\u4f7f\u540d\u79f0\u66f4\u6709\u610f\u4e49\uff0c\u8fdb\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p>\u8fd8\u6709\uff0c\u7f16\u7801\u89c4\u8303\u53ef\u4ee5\u8ba9\u4f60\u665a\u4e0a\u7761\u5f97\u66f4\u9999\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-417\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/03023601-68107c764c674fa99ec13361802a2cf4006.jpg\" alt=\"03023601-68107c764c674fa99ec13361802a2cf4006\" width=\"240\" height=\"148\" \/><\/p>\n<h2>\u5f00\u53d1\u4eba\u5458\u6700\u5e94\u8be5\u9075\u5faa\u7684\u51e0\u4e2a\u89c4\u5219<\/h2>\n<h3><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-418\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/03131159-f7ce88dbede9458aafe23a98603f4174007.jpg\" alt=\"03131159-f7ce88dbede9458aafe23a98603f4174007\" width=\"394\" height=\"510\" srcset=\"https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03131159-f7ce88dbede9458aafe23a98603f4174007.jpg 394w, https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03131159-f7ce88dbede9458aafe23a98603f4174007-232x300.jpg 232w\" sizes=\"auto, (max-width: 394px) 100vw, 394px\" \/><\/h3>\n<h3>\u59cb\u7ec8\u63a7\u5236\u7c7b\u7684\u5927\u5c0f<\/h3>\n<p>\u6211\u66fe\u7ecf\u770b\u5230\u8fc7\uff0c\u5e76\u4e14\u4e5f\u66fe\u5199\u8fc7\u4e00\u4e9b\u8d85\u5927\u7684\u7c7b\u3002\u800c\u4e14\u4e0d\u5e78\u7684\u662f\uff0c\u7ed3\u679c\u603b\u662f\u4e0d\u597d\u7684\u3002\u540e\u6765\u6211\u627e\u5230\u4e86\u771f\u6b63\u539f\u56e0\uff0c\u5c31\u662f\u90a3\u4e9b\u8d85\u5927\u7684\u7c7b\u5728\u5c1d\u8bd5\u505a\u592a\u591a\u7684\u4e8b\u60c5\uff0c\u8fd9\u8fdd\u53cd\u4e86<a href=\"http:\/\/en.wikipedia.org\/wiki\/Single_responsibility_principle\">\u5355\u4e00\u804c\u8d23\u539f\u5219<\/a>\uff08SRP\uff09\uff0c\u4e5f\u5c31\u662f\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219 <a href=\"http:\/\/en.wikipedia.org\/wiki\/SOLID_%28object-oriented_design%29\">SOLID<\/a> \u4e2d\u7684 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Single_responsibility_principle\">S<\/a>\u3002<\/p>\n<p><em>\u201cThe single responsibility principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.\u201d<\/em><\/p>\n<p>\u6216\u8005\u6309\u7167 <a href=\"http:\/\/www.objectmentor.com\/resources\/articles\/srp.pdf\">Martin Fowler<\/a> \u7684\u5b9a\u4e49\uff1a<em>&#8220;THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE.&#8221;<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-419\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/03023730-4a795533cfd3463788d38bcb8ffbe965008.jpg\" alt=\"03023730-4a795533cfd3463788d38bcb8ffbe965008\" width=\"555\" height=\"370\" srcset=\"https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03023730-4a795533cfd3463788d38bcb8ffbe965008.jpg 555w, https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03023730-4a795533cfd3463788d38bcb8ffbe965008-300x200.jpg 300w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><\/p>\n<p>\u4e3a\u4ec0\u4e48\u4e00\u5b9a\u8981\u5c06\u4e24\u4e2a\u804c\u8d23\u5206\u79bb\u5230\u5355\u72ec\u7684\u7c7b\u4e2d\u5462\uff1f\u56e0\u4e3a\u6bcf\u4e00\u4e2a\u804c\u8d23\u90fd\u662f\u53d8\u5316\u7684\u4e2d\u5fc3\u3002\u5728\u9700\u6c42\u53d8\u66f4\u65f6\uff0c\u8fd9\u4e2a\u53d8\u66f4\u5c06\u4f1a\u51fa\u73b0\u5728\u8d1f\u8d23\u8be5\u804c\u8d23\u7684\u7c7b\u4e2d\u3002\u5982\u679c\u4e00\u4e2a\u7c7b\u627f\u62c5\u4e86\u591a\u4e2a\u804c\u8d23\uff0c\u5c31\u4f1a\u6709\u4e00\u4e2a\u4ee5\u4e0a\u7684\u539f\u56e0\u5bfc\u81f4\u5176\u53d8\u5316\u3002\u5982\u679c\u4e00\u4e2a\u7c7b\u6709\u591a\u91cd\u804c\u8d23\uff0c\u5219\u8bf4\u660e\u8fd9\u4e9b\u804c\u8d23\u5df2\u7ecf\u8026\u5408\u5230\u4e86\u4e00\u8d77\u3002\u5e76\u4e14\u67d0\u4e2a\u804c\u8d23\u7684\u53d8\u5316\u5c06\u6709\u53ef\u80fd\u524a\u5f31\u6216\u9650\u5236\u8fd9\u4e2a\u7c7b\u6ee1\u8db3\u5176\u4ed6\u804c\u8d23\u7684\u80fd\u529b\u3002\u8fd9\u79cd\u8026\u5408\u5c06\u4f1a\u5bfc\u81f4\u975e\u5e38\u8106\u5f31\u7684\u8bbe\u8ba1\uff0c\u8fdb\u800c\u5728\u804c\u8d23\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u8bbe\u8ba1\u53ef\u80fd\u88ab\u610f\u60f3\u4e0d\u5230\u7684\u7834\u574f\u4e86\u3002<\/p>\n<h3>\u907f\u514d\u8fc7\u65f6\u7684\u6ce8\u91ca<\/h3>\n<p>\u5148\u8bf4\u4ec0\u4e48\u8fc7\u65f6\u7684\u6ce8\u91ca\u3002\u6309\u7167 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Robert_Cecil_Martin\">Robert C. Martin<\/a> \u7684\u5b9a\u4e49\uff1a<\/p>\n<p><em>&#8220;A comment that has gotten old, irrelevant, and incorrect is obsolete. Comments get old quickly. It is best not to write a comment that will become obsolete. If you find an obsolete comment, it is best to update it or get rid of it as quickly as possible. Obsolete comments tend to migrate away from the code they once described. They become floating islands of irrelevance and misdirection in the code.&#8221;<\/em><\/p>\n<p>\u9488\u5bf9\u8fd9\u4e2a\u4e3b\u9898\uff0c\u4e0d\u540c\u6c34\u5e73\u7684\u5f00\u53d1\u4eba\u5458\u53ef\u80fd\u90fd\u4f1a\u6709\u81ea\u5df1\u7684\u89c1\u89e3\u3002\u6211\u7684\u5efa\u8bae\u662f\u5c1d\u8bd5\u907f\u514d\u4e3a\u5355\u72ec\u7684\u65b9\u6cd5\u6216\u77ed\u5c0f\u7684\u7c7b\u8fdb\u884c\u6ce8\u91ca\u3002\u56e0\u4e3a\u6211\u6240\u89c1\u8fc7\u7684\u5927\u90e8\u5206\u7684\u6ce8\u91ca\u90fd\u662f\u5728\u5c1d\u8bd5\u63cf\u8ff0\u4ee3\u7801\u7684\u76ee\u7684\u6216\u610f\u56fe\uff0c\u6216\u8005\u67d0\u4e9b\u6ce8\u91ca\u53ef\u80fd\u672c\u8eab\u5c31\u6ca1\u4ec0\u4e48\u610f\u4e49\u3002\u901a\u5e38\u5f00\u53d1\u4eba\u5458\u901a\u8fc7\u5199\u6ce8\u91ca\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u4f46\u8981\u4fdd\u8bc1\u4f60\u6240\u5199\u7684\u6ce8\u91ca\u4e0d\u4f1a\u6210\u4e3a\u4ee3\u7801\u4e2d\u7684\u566a\u97f3\u3002\u6bd4\u8d77\u6ce8\u91ca\uff0c\u6211\u8ba4\u4e3a\u5408\u7406\u7684\u65b9\u6cd5\u547d\u540d\u5c06\u66f4\u4e3a\u6709\u6548\uff0c\u6bd4\u5982\u4f60\u53ef\u4ee5\u4e3a\u4e00\u4e2a\u65b9\u6cd5\u8d77\u4e00\u4e2a\u66f4\u6709\u610f\u4e49\u7684\u540d\u5b57\u3002\u5927\u90e8\u5206\u6ce8\u91ca\u90fd\u53ef\u80fd\u53d8\u6210\u4e86\u65e0\u610f\u4e49\u7684\u4ee3\u7801\u566a\u97f3\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e0b\u9762\u8fd9\u4e9b\u6ce8\u91ca\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>      \/\/ensure that we are not exporting\r\n      \/\/deleted products\r\n      if (product.IsDeleted &amp;&amp; !product.IsExported)\r\n      {\r\n        ExportProducts = false;\r\n      }\r\n\r\n      \/\/ This is a for loop that prints the 1 million times\r\n      for (int i = 0; i &lt; 1000000; i++)\r\n      {\r\n        Console.WriteLine(i);\r\n      }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u5982\u679c\u6211\u4eec\u4e0d\u5199\u6ce8\u91ca\uff0c\u800c\u662f\u547d\u540d\u4e00\u4e2a\u65b9\u6cd5\uff0c\u6bd4\u5982\u53eb CancelExportForDeletedProducts() \uff0c\u60c5\u51b5\u4f1a\u600e\u6837\uff1f\u6240\u4ee5\uff0c\u5408\u9002\u7684\u65b9\u6cd5\u547d\u540d\u6bd4\u6ce8\u91ca\u66f4\u6709\u6548\u3002\u7136\u800c\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4ee3\u7801\u6ce8\u91ca\u4e5f\u4f1a\u975e\u5e38\u6709\u5e2e\u52a9\uff0c\u6bd4\u5982 Visual Studio \u4f1a\u4ece\u6ce8\u91ca\u751f\u6210 API \u6587\u6863\u3002\u6b64\u5904\u7684\u6ce8\u91ca\u7565\u6709\u4e0d\u540c\uff0c\u4f60\u9700\u8981\u4f7f\u7528 \u201c\/\/\/\u201d \u6807\u8bc6\u7b26\u6765\u6ce8\u91ca\uff0c\u8fd9\u6837\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u624d\u80fd\u770b\u5230 API \u6216\u7c7b\u5e93\u7684\u667a\u80fd\u63d0\u793a\u3002<\/p>\n<p>\u6211\u6ca1\u6709\u8bf4\u603b\u662f\u8981\u907f\u514d\u6ce8\u91ca\u3002\u6309\u7167 Kent Beck \u8bf4\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u66f4\u591a\u7684\u6ce8\u91ca\u6765\u63cf\u8ff0\u7a0b\u5e8f\u6574\u4f53\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u800c\u4e0d\u662f\u5bf9\u5355\u72ec\u7684\u65b9\u6cd5\u8fdb\u884c\u6ce8\u91ca\u3002\u5982\u679c\u6ce8\u91ca\u662f\u5728\u5c1d\u8bd5\u63cf\u8ff0\u4ee3\u7801\u7684\u76ee\u7684\u6216\u610f\u56fe\uff0c\u90a3\u5c31\u9519\u4e86\u3002\u5982\u679c\u4f60\u5728\u4ee3\u7801\u4e2d\u770b\u5230\u4e86\u5bc6\u5bc6\u9ebb\u9ebb\u7684\u7684\u6ce8\u91ca\uff0c\u4f60\u53ef\u80fd\u5c31\u4f1a\u610f\u8bc6\u5230\u6709\u8fd9\u4e48\u591a\u6ce8\u91ca\u8bf4\u660e\u4ee3\u7801\u5199\u7684\u5f88\u7cdf\u7cd5\u3002\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\u53ef\u4ee5\u9605\u8bfb\u4e0b\u9762\u8fd9\u51e0\u672c\u4e66\uff1a<\/p>\n<ul>\n<li>\u300aProfessional Refactoring in C# and ASP.NET\u300b by Danijel Arsenovski<\/li>\n<li>\u300a\u91cd\u6784\uff1a\u6539\u5584\u65e2\u6709\u4ee3\u7801\u8bbe\u8ba1\u300b by Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts<\/li>\n<\/ul>\n<h3>\u907f\u514d\u4e0d\u5fc5\u8981\u7684Region<\/h3>\n<p>Region \u662f Visual Studio \u63d0\u4f9b\u7684\u4e00\u4e2a\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u4f60\u5c06\u4ee3\u7801\u5206\u5757\u3002Region \u7684\u5b58\u5728\u662f\u56e0\u4e3a\u5b83\u53ef\u4ee5\u4f7f\u5927\u6587\u4ef6\u5bfc\u822a\u53d8\u5f97\u5bb9\u6613\u3002Region \u8fd8\u5e38\u88ab\u7528\u4e8e\u9690\u85cf\u4e11\u964b\u7684\u4ee3\u7801\uff0c\u6216\u8005\u7c7b\u5df2\u7ecf\u81a8\u80c0\u7684\u975e\u5e38\u5927\u4e86\u9700\u8981\u5206\u5757\u3002\u800c\u5982\u679c\u4e00\u4e2a\u7c7b\u505a\u4e86\u592a\u591a\u7684\u4e8b\u60c5\uff0c\u4e5f\u5c31\u8bf4\u660e\u5176\u8fdd\u53cd\u4e86\u5355\u4e00\u804c\u8d23\u539f\u5219\u3002\u6240\u4ee5\uff0c\u4e0b\u6b21\u5f53\u4f60\u60f3\u65b0\u589e\u4e00\u4e2a Region \u65f6\uff0c\u5148\u8003\u8651\u4e0b\u6709\u6ca1\u6709\u53ef\u80fd\u5c06\u8fd9\u4e2a Region \u5206\u79bb\u5230\u4e00\u4e2a\u5355\u72ec\u7684\u7c7b\u4e2d\u3002<\/p>\n<h3>\u4fdd\u6301\u65b9\u6cd5\u7684\u77ed\u5c0f<\/h3>\n<p>\u65b9\u6cd5\u4e2d\u7684\u4ee3\u7801\u884c\u6570\u8d8a\u591a\uff0c\u5219\u65b9\u6cd5\u8d8a\u96be\u7406\u89e3\u3002\u6211\u4eec\u63a8\u8350\u6bcf\u4e2a\u65b9\u6cd5\u4e2d\u53ea\u5305\u542b 20-25 \u884c\u4ee3\u7801\u3002\u4f46\u6709\u4e9b\u4eba\u8bf4 1-10 \u884c\u66f4\u5408\u7406\uff0c\u8fd9\u53ea\u662f\u4e9b\u4e2a\u4eba\u559c\u597d\uff0c\u6ca1\u6709\u786c\u6027\u7684\u89c4\u5219\u3002\u62bd\u53d6\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u7684\u91cd\u6784\u65b9\u5f0f\u4e4b\u4e00\u3002\u5982\u679c\u4f60\u53d1\u73b0\u4e00\u4e2a\u65b9\u6cd5\u8fc7\u957f\uff0c\u6216\u8005\u5df2\u7ecf\u9700\u8981\u4e00\u4e2a\u6ce8\u91ca\u6765\u63cf\u8ff0\u5b83\u7684\u76ee\u7684\u4e86\uff0c\u90a3\u4e48\u4f60\u5c31\u53ef\u4ee5\u5e94\u7528\u62bd\u53d6\u65b9\u6cd5\u4e86\u3002\u4eba\u4eec\u603b\u662f\u4f1a\u95ee\u4e00\u4e2a\u65b9\u6cd5\u5230\u5e95\u591a\u957f\u5408\u9002\uff0c\u4f46\u5176\u5b9e\u957f\u5ea6\u5e76\u4e0d\u662f\u95ee\u9898\u7684\u6839\u6e90\u3002\u5f53\u4f60\u5728\u5904\u7406\u590d\u6742\u7684\u65b9\u6cd5\u65f6\uff0c\u8ddf\u8e2a\u6240\u6709\u5c40\u90e8\u53d8\u91cf\u662f\u6700\u590d\u6742\u548c\u6d88\u8017\u65f6\u95f4\u7684\uff0c\u800c\u901a\u8fc7\u62bd\u53d6\u4e00\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u8282\u7701\u4e00\u4e9b\u65f6\u95f4\u3002\u53ef\u4ee5\u4f7f\u7528 Visual Studio \u6765\u62bd\u53d6\u65b9\u6cd5\uff0c\u5b83\u4f1a\u5e2e\u52a9\u4f60\u8ddf\u8e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9\u65b0\u7684\u65b9\u6cd5\u6216\u8005\u63a5\u6536\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<p><strong>Using ReSharper<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-420\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/03023956-599930cb3b4e468a838b79be91ba7bd3009.png\" alt=\"03023956-599930cb3b4e468a838b79be91ba7bd3009\" width=\"899\" height=\"613\" srcset=\"https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03023956-599930cb3b4e468a838b79be91ba7bd3009.png 899w, https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03023956-599930cb3b4e468a838b79be91ba7bd3009-300x205.png 300w, https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03023956-599930cb3b4e468a838b79be91ba7bd3009-768x524.png 768w\" sizes=\"auto, (max-width: 899px) 100vw, 899px\" \/><\/p>\n<p><strong>Using Microsoft Visual Studio<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-421\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/03024003-a70ce745b5b54050a79cdb546b66fd5f010.png\" alt=\"03024003-a70ce745b5b54050a79cdb546b66fd5f010\" width=\"320\" height=\"33\" srcset=\"https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03024003-a70ce745b5b54050a79cdb546b66fd5f010.png 320w, https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03024003-a70ce745b5b54050a79cdb546b66fd5f010-300x31.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/p>\n<p>\u66f4\u591a\u7684\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003 <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/0s21cwxk.aspx\">MSDN<\/a>\u3002<\/p>\n<p>\u6309\u7167\u300a\u91cd\u6784\uff1a\u6539\u5584\u65e2\u6709\u4ee3\u7801\u8bbe\u8ba1\u300b\u4e2d\u7684\u63cf\u8ff0\uff0c<\/p>\n<p><em>&#8220;Extract Method is one of the most common refactoring I do. I look at a method that is too long or look at code that needs a comment to understand its purpose. I then turn that fragment of code into its own method. I prefer short, well-named methods for several reasons. First, it increases the chances that other methods can use a method when the method is finely grained. Second, it allows the higher-level methods to read more like a series of comments. Overriding also is easier when the methods are finely grained. It does take a little getting used to if you are used to seeing larger methods. And small methods really work only when you have good names, so you need to pay attention to naming. People sometimes ask me what length I look for in a method. To me length is not the issue. The key is the semantic distance between the method name and the method body. If extracting improves clarity, do it, even if the name is longer than the code you have extracted.&#8221;<\/em><\/p>\n<h3>\u907f\u514d\u8fc7\u591a\u7684\u53c2\u6570<\/h3>\n<p>\u901a\u8fc7\u58f0\u660e\u4e00\u4e2a\u7c7b\u6765\u4ee3\u66ff\u591a\u4e2a\u53c2\u6570\u3002\u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u7528\u4e8e\u5305\u542b\u6240\u6709\u7684\u53c2\u6570\u3002\u901a\u5e38\u6765\u8bb2\uff0c\u8fd9\u662f\u4e00\u4e2a\u8f83\u597d\u7684\u8bbe\u8ba1\uff0c\u5e76\u4e14\u8fd9\u4e2a\u62bd\u8c61\u975e\u5e38\u7684\u6709\u4ef7\u503c\u3002<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>    \/\/avoid\r\n    public void Checkout(string shippingName, string shippingCity,\r\n      string shippingSate, string shippingZip, string billingName,\r\n      string billingCity, string billingSate, string billingZip)\r\n    {\r\n    }\r\n    \/\/DO\r\n    public void Checkout(ShippingAddress shippingAddress, BillingAddress billingAddress)\r\n    {\r\n    }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u6211\u4eec\u9700\u8981\u5f15\u5165\u7c7b\u6765\u4ee3\u66ff\u6240\u6709\u7684\u53c2\u6570\u3002<\/p>\n<h3>\u907f\u514d\u590d\u6742\u7684\u8868\u8fbe\u5f0f<\/h3>\n<div class=\"cnblogs_code\">\n<pre>    if(product.Price&gt;500 &amp;&amp; !product.IsDeleted &amp;&amp; \r\n      !product.IsFeatured &amp;&amp; product.IsExported)\r\n    {\r\n      \/\/ do something\r\n    }<\/pre>\n<\/div>\n<p>\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u610f\u5473\u7740\u5176\u80cc\u540e\u9690\u85cf\u4e86\u4e00\u4e9b\u6db5\u4e49\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5c5e\u6027\u6765\u5c01\u88c5\u8fd9\u4e9b\u8868\u8fbe\u5f0f\uff0c\u8fdb\u800c\u4f7f\u4ee3\u7801\u66f4\u6613\u8bfb\u4e9b\u3002<\/p>\n<h3>\u628a\u8b66\u544a\u7b49\u540c\u4e8e\u9519\u8bef<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-422\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/03024302-8f1fe7b9840d477caa77b30823c373f4011.jpg\" alt=\"03024302-8f1fe7b9840d477caa77b30823c373f4011\" width=\"463\" height=\"312\" srcset=\"https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03024302-8f1fe7b9840d477caa77b30823c373f4011.jpg 463w, https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03024302-8f1fe7b9840d477caa77b30823c373f4011-300x202.jpg 300w\" sizes=\"auto, (max-width: 463px) 100vw, 463px\" \/><\/p>\n<p>\u5982\u679c\u4f60\u6ce8\u610f\u770b\u4ee3\u7801\uff0c\u4f60\u4f1a\u53d1\u73b0\u4e00\u4e2a\u53d8\u91cf\u88ab\u58f0\u660e\u4e86\u4f46\u4ece\u6ca1\u88ab\u4f7f\u7528\u8fc7\u3002\u6b63\u5e38\u6765\u8bb2\uff0c\u6211\u4eec\u7f16\u8bd1\u5de5\u7a0b\u540e\u4f1a\u5f97\u5230\u4e00\u4e2a\u8b66\u544a\uff0c\u4f46\u4ecd\u53ef\u4ee5\u8fd0\u884c\u5de5\u7a0b\u800c\u4e0d\u4f1a\u53d1\u751f\u4efb\u4f55\u9519\u8bef\u3002\u4f46\u662f\u6211\u4eec\u5e94\u8be5\u5c3d\u53ef\u80fd\u5730\u79fb\u9664\u8fd9\u4e9b\u8b66\u544a\u3002\u901a\u8fc7\u5982\u4e0b\u6b65\u9aa4\u53ef\u4ee5\u5728\u5de5\u7a0b\u4e0a\u8bbe\u7f6e\u5c06\u8b66\u544a\u7b49\u540c\u4e8e\u9519\u8bef\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/03024343-99671d64243a417c8782dfd45329b375012.jpg\" alt=\"03024343-99671d64243a417c8782dfd45329b375012\" width=\"386\" height=\"423\" srcset=\"https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03024343-99671d64243a417c8782dfd45329b375012.jpg 386w, https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03024343-99671d64243a417c8782dfd45329b375012-274x300.jpg 274w\" sizes=\"auto, (max-width: 386px) 100vw, 386px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-424\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/03024350-84bc7afa76954cd5831106c5e14d668c013.jpg\" alt=\"03024350-84bc7afa76954cd5831106c5e14d668c013\" width=\"574\" height=\"540\" srcset=\"https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03024350-84bc7afa76954cd5831106c5e14d668c013.jpg 574w, https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03024350-84bc7afa76954cd5831106c5e14d668c013-300x282.jpg 300w\" sizes=\"auto, (max-width: 574px) 100vw, 574px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-425\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2016\/12\/03024358-887816f834514b60ba3fff92cede64f4014.jpg\" alt=\"03024358-887816f834514b60ba3fff92cede64f4014\" width=\"402\" height=\"152\" srcset=\"https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03024358-887816f834514b60ba3fff92cede64f4014.jpg 402w, https:\/\/freesilo.com\/wp-content\/uploads\/2016\/12\/03024358-887816f834514b60ba3fff92cede64f4014-300x113.jpg 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/p>\n<h3>\u7cbe\u7b80\u591a\u5904\u8fd4\u56de<\/h3>\n<p>\u5728\u6bcf\u6bb5\u7a0b\u5e8f\u4e2d\u90fd\u51cf\u5c11\u51fd\u6570\u8fd4\u56de\u7684\u6570\u91cf\u3002\u5047\u8bbe\u4ece\u5e95\u90e8\u5f00\u59cb\u9605\u8bfb\u4ee3\u7801\uff0c\u4f60\u5f88\u96be\u610f\u8bc6\u5230\u6709\u53ef\u80fd\u5728\u4e0a\u9762\u7684\u67d0\u5904\u5df2\u7ecf\u8fd4\u56de\u4e86\uff0c\u8fd9\u6837\u7684\u4ee3\u7801\u5c06\u662f\u975e\u5e38\u96be\u7406\u89e3\u7684\u3002<\/p>\n<p>\u4ec5\u4f7f\u7528\u4e00\u5904\u8fd4\u56de\u53ef\u4ee5\u589e\u5f3a\u53ef\u8bfb\u6027\u3002\u5982\u679c\u7a0b\u5e8f\u8fd9\u4e48\u5199\u7684\u8bdd\u53ef\u80fd\u770b\u8d77\u6765\u6bd4\u8f83\u5e72\u51c0\uff0c\u4f46\u4e0d\u7acb\u5373\u8fd4\u56de\u4e5f\u610f\u5473\u7740\u9700\u8981\u7f16\u5199\u66f4\u591a\u4ee3\u7801\u3002<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>      \/\/avoid\r\n      if(product.Price&gt;15)\r\n      {\r\n         return false;\r\n      }\r\n      else if(product.IsDeleted)\r\n      {\r\n         return false;\r\n      }\r\n      else if(!product.IsFeatured)\r\n      {\r\n         return false;\r\n      }\r\n      else if()\r\n      {\r\n         \/\/.....\r\n      }\r\n      return true;<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>      \/\/DO\r\n      var isValid = true;\r\n      if(product.Price&gt;15)\r\n      {\r\n         isValid= false;\r\n      }\r\n      else if(product.IsDeleted)\r\n      {\r\n         isValid= false;\r\n      }\r\n      else if(!product.IsFeatured)\r\n      {\r\n         isValid= false;\r\n      }\r\n      return isValid;<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u4f60\u53ef\u4ee5\u60f3\u8c61\u5728\u8fd9 20-30 \u884c\u4ee3\u7801\u4e2d\u5c31\u6563\u843d\u4e86 4 \u4e2a\u9000\u51fa\u70b9\uff0c\u8fd9\u4f1a\u4f7f\u4f60\u975e\u5e38\u96be\u7406\u89e3\u5230\u5e95\u7a0b\u5e8f\u5185\u90e8\u505a\u4e86\u4ec0\u4e48\uff0c\u5230\u5e95\u4f1a\u6267\u884c\u4ec0\u4e48\uff0c\u4ec0\u4e48\u65f6\u5019\u6267\u884c\u3002<\/p>\n<p>\u5173\u4e8e\u8fd9\u4e00\u70b9\u6211\u5f97\u5230\u4e86\u5f88\u591a\u4eba\u7684\u56de\u590d\uff0c\u4e00\u4e9b\u4eba\u540c\u610f\u8fd9\u4e2a\u89c2\u70b9\uff0c\u6709\u4e9b\u5219\u4e0d\u540c\u610f\u8fd9\u662f\u4e00\u4e2a\u597d\u7684\u7f16\u7801\u6807\u51c6\u3002\u4e3a\u4e86\u627e\u51fa\u6f5c\u5728\u7684\u95ee\u9898\uff0c\u6211\u505a\u4e86\u4e9b\u5355\u5143\u6d4b\u8bd5\uff0c\u53d1\u73b0\u5982\u679c\u590d\u6742\u7684\u65b9\u6cd5\u5305\u542b\u591a\u4e2a\u9000\u51fa\u70b9\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u4f1a\u9700\u8981\u4e00\u7ec4\u6d4b\u8bd5\u6765\u8986\u76d6\u6240\u6709\u7684\u8def\u5f84\u3002<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>      if( BADFunction() == true)\r\n      {\r\n          \/\/ expression\r\n          if( anotherFunction() == true )\r\n          {\r\n           \/\/ expression\r\n           return true;\r\n          }\r\n          else\r\n          {\r\n               \/\/error\r\n          }\r\n      }\r\n      else\r\n      {\r\n          \/\/error\r\n      }\r\n      return false;<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>      if( !GoodFunction())\r\n      {\r\n          \/\/ error.\r\n          return false\r\n      } \r\n      \/\/ expression\r\n      if( !GoodFunction2())\r\n      {\r\n          \/\/error.\r\n          return false;\r\n      }\r\n      \/\/ more expression\r\n      return true;<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/blog.freesilo.com\/wp-content\/uploads\/2017\/02\/9148256b89e79e5d03bf03a93cf12ac4.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u8fdb\u4e00\u6b65\u7406\u89e3\u53ef\u4ee5\u53c2\u8003 <a href=\"http:\/\/cc2e.com\/\">Steve McConnell \u7684\u300a\u4ee3\u7801\u5927\u5168\u300b<\/a>\u3002<\/p>\n<h3>\u4f7f\u7528\u65ad\u8a00<\/h3>\n<p>\u5728\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u65ad\u8a00\u4ee3\u7801\u5e38\u88ab\u7528\u4e8e\u68c0\u67e5\u7a0b\u5e8f\u4ee3\u7801\u662f\u5426\u6309\u7167\u5176\u8bbe\u8ba1\u5728\u6267\u884c\u3002\u901a\u5e38 True \u4ee3\u8868\u6240\u6709\u64cd\u4f5c\u6309\u7167\u9884\u671f\u7684\u5b8c\u6210\uff0cFalse \u4ee3\u8868\u5df2\u7ecf\u4fa6\u6d4b\u5230\u4e86\u4e00\u4e9b\u610f\u5916\u7684\u9519\u8bef\u3002\u65ad\u8a00\u901a\u5e38\u4f1a\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u5e03\u5c14\u578b\u7684\u8868\u8fbe\u5f0f\u7528\u4e8e\u4e00\u4e2a\u63cf\u8ff0\u5047\u8bbe\u4e3a\u771f\u7684\u5047\u5b9a\uff0c\u4e00\u4e2a\u6d88\u606f\u53c2\u6570\u7528\u4e8e\u63cf\u8ff0\u65ad\u8a00\u5931\u8d25\u7684\u539f\u56e0\u3002<\/p>\n<p>\u5c24\u5176\u5728\u5f00\u53d1\u5927\u578b\u7684\u3001\u590d\u6742\u7684\u9ad8\u53ef\u9760\u7cfb\u7edf\u4e2d\uff0c\u65ad\u8a00\u901a\u5e38\u662f\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\u3002<\/p>\n<p>\u4f8b\u5982\uff1a\u5982\u679c\u7cfb\u7edf\u5047\u8bbe\u5c06\u6700\u591a\u652f\u6301 100,000 \u7528\u6237\u8bb0\u5f55\uff0c\u7cfb\u7edf\u4e2d\u53ef\u80fd\u4f1a\u5305\u542b\u4e00\u4e2a\u65ad\u8a00\u6765\u68c0\u67e5\u7528\u6237\u8bb0\u5f55\u6570\u5c0f\u4e8e\u7b49\u4e8e 100,000\uff0c\u5728\u8fd9\u79cd\u8303\u56f4\u4e0b\uff0c\u65ad\u8a00\u4e0d\u4f1a\u8d77\u4f5c\u7528\u3002\u4f46\u5982\u679c\u7528\u6237\u8bb0\u5f55\u6570\u91cf\u8d85\u8fc7\u4e86 100,000\uff0c\u5219\u65ad\u8a00\u5c06\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u6765\u544a\u8bc9\u4f60\u8bb0\u5f55\u6570\u5df2\u7ecf\u8d85\u51fa\u4e86\u8303\u56f4\u3002<\/p>\n<h3>\u68c0\u67e5\u5faa\u73af\u7aef\u70b9\u503c<\/h3>\n<p>\u4e00\u4e2a\u5faa\u73af\u901a\u5e38\u4f1a\u6d89\u53ca\u4e09\u79cd\u6761\u4ef6\u503c\uff1a\u7b2c\u4e00\u4e2a\u503c\u3001\u4e2d\u95f4\u7684\u67d0\u503c\u548c\u6700\u540e\u4e00\u4e2a\u503c\u3002\u4f46\u5982\u679c\u4f60\u6709\u4efb\u4f55\u5176\u4ed6\u7684\u7279\u5b9a\u6761\u4ef6\uff0c\u4e5f\u9700\u8981\u8fdb\u884c\u68c0\u6d4b\u3002\u5982\u679c\u5faa\u73af\u4e2d\u5305\u542b\u4e86\u590d\u6742\u7684\u8ba1\u7b97\uff0c\u8bf7\u4e0d\u8981\u4f7f\u7528\u8ba1\u7b97\u5668\uff0c\u8981\u624b\u5de5\u68c0\u67e5\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u901a\u5e38\u5728\u4efb\u4f55\u8f6f\u4ef6\u516c\u53f8\u4e2d\u63a8\u884c\u7f16\u7801\u89c4\u8303\u90fd\u9700\u8981\u6309\u7167\u7ec4\u7ec7\u884c\u4e3a\u3001\u9879\u76ee\u5c5e\u6027\u548c\u9886\u57df\u6765\u8fdb\u884c\uff0c\u5728\u6b64\u6211\u60f3\u518d\u6b21\u5f3a\u8c03\u201c\u627e\u5230\u4e00\u4e2a\u9002\u5408\u4f60\u7684\u7f16\u7801\u89c4\u8303\uff0c\u5e76\u4e00\u76f4\u9075\u5faa\u5b83\u201d\u3002<\/p>\n<p>\u5982\u679c\u4f60\u8ba4\u4e3a\u6211\u9057\u6f0f\u4e86\u67d0\u4e2a\u7279\u522b\u6709\u7528\u7684\u7f16\u7801\u51c6\u5219\uff0c\u8bf7\u5728\u8bc4\u8bba\u4e2d\u63cf\u8ff0\uff0c\u6211\u4f1a\u5c1d\u8bd5\u8865\u5145\u5230\u6587\u7ae0\u4e2d\u3002<\/p>\n<p><strong>Coding For Fun.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/www.codeproject.com\/Articles\/539179\/Some-practices-to-write-better-Csharp-NET-code\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<p>\u8bd1\u8005\u6ce8\uff1a\u539f\u6587\u4f5c\u8005 <a href=\"http:\/\/www.codeproject.com\/Members\/Monjurul-Habib\">Monjurul Habib<\/a>\u00a0\u662f\u5b5f\u52a0\u62c9\u56fd\u4eba\uff0c\u6211\u4e0d\u77e5\u9053\u5b5f\u52a0\u62c9\u56fd\u7684\u4eba\u8bf4\u4ec0\u4e48\u8bed\u8a00\uff0c\u4f46\u6211\u611f\u89c9\u4f5c\u8005\u7684\u82f1\u8bed\u975e\u5e38\u751f\u786c\uff0c\u6709\u4e9b\u5730\u65b9\u6211\u5b9e\u5728\u662f\u7406\u89e3\u4e0d\u4e0a\u53bb\uff0c\u8bfb\u8005\u89c1\u8c05\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 \u5f00\u53d1\u4eba\u5458\u603b\u662f\u559c\u6b22\u5c31\u7f16\u7801\u89c4\u8303\u8fdb\u884c\u4e89\u8bba\uff0c\u4f46\u66f4\u91cd\u8981\u7684\u662f\u5982\u4f55\u80fd\u591f\u5728\u9879\u76ee\u4e2d\u81ea\u59cb\u81f3\u7ec8\u5730&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/freesilo.com\/?p=411\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-411","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=411"}],"version-history":[{"count":3,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/411\/revisions"}],"predecessor-version":[{"id":501,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/411\/revisions\/501"}],"wp:attachment":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}