{"id":3492,"date":"2025-04-13T13:11:23","date_gmt":"2025-04-13T05:11:23","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3492"},"modified":"2025-04-13T13:11:23","modified_gmt":"2025-04-13T05:11:23","slug":"c-%e5%be%aa%e7%8e%af%ef%bc%88loops%ef%bc%89%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/13\/c-%e5%be%aa%e7%8e%af%ef%bc%88loops%ef%bc%89%e7%bb%93%e6%9e%84\/","title":{"rendered":"C# \u5faa\u73af\uff08Loops\uff09\u7ed3\u6784"},"content":{"rendered":"\n<p>\u672c\u7ae0\u8282\u91cd\u70b9\u5173\u4e8e\u8bb2\u89e3 <strong>C# \u5faa\u73af\uff08Loops\uff09\u7ed3\u6784<\/strong>\uff0c\u6db5\u76d6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5404\u7c7b\u5faa\u73af\u8bed\u6cd5\u4e0e\u4f7f\u7528\u573a\u666f<\/li>\n\n\n\n<li>\u793a\u4f8b\u4ee3\u7801\u4e0e\u6ce8\u610f\u4e8b\u9879<\/li>\n\n\n\n<li>\u5b98\u65b9\u6587\u6863\u53c2\u8003\u94fe\u63a5<\/li>\n\n\n\n<li>\u591a\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff08\u5982 <code>for<\/code>, <code>while<\/code>, <code>foreach<\/code>, <code>do...while<\/code>\uff09<\/li>\n\n\n\n<li>\u6613\u9519\u70b9\u4e0e\u6700\u4f73\u5b9e\u8df5<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udcda <strong>\u5b98\u65b9\u6743\u5a01\u94fe\u63a5<\/strong>\uff08\u4fe1\u606f\u5747\u6765\u81ea Microsoft \u5b98\u65b9\u6587\u6863\uff09\uff1a<br>\ud83d\udd17 <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/statements\/iteration-statements\">C# Iteration Statements (loops) &#8211; Microsoft Docs<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001C# \u652f\u6301\u7684\u5faa\u73af\u7ed3\u6784<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5faa\u73af\u7c7b\u578b<\/th><th>\u8bed\u6cd5\u5173\u952e\u5b57<\/th><th>\u5178\u578b\u4f7f\u7528\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td><code>for<\/code><\/td><td><code>for<\/code><\/td><td>\u5df2\u77e5\u5faa\u73af\u6b21\u6570\uff0c\u7d22\u5f15\u8fed\u4ee3<\/td><\/tr><tr><td><code>while<\/code><\/td><td><code>while<\/code><\/td><td>\u4e0d\u786e\u5b9a\u5faa\u73af\u6b21\u6570\uff0c\u6761\u4ef6\u5148\u5224\u65ad<\/td><\/tr><tr><td><code>do-while<\/code><\/td><td><code>do...while<\/code><\/td><td>\u4e0d\u786e\u5b9a\u5faa\u73af\u6b21\u6570\uff0c\u4f46\u81f3\u5c11\u6267\u884c\u4e00\u6b21<\/td><\/tr><tr><td><code>foreach<\/code><\/td><td><code>foreach<\/code><\/td><td>\u7528\u4e8e\u904d\u5386\u6570\u7ec4\u3001\u96c6\u5408\u3001\u5217\u8868\u7b49\u53ef\u679a\u4e3e\u5bf9\u8c61<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5404\u7c7b\u578b\u5faa\u73af\u8be6\u89e3<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 <code>for<\/code> \u5faa\u73af<\/h3>\n\n\n\n<p>\u7528\u4e8e\u5df2\u77e5\u8fed\u4ee3\u6b21\u6570\u7684\u5faa\u73af\uff0c\u8bed\u6cd5\u7b80\u6d01\uff0c\u9002\u5408\u6570\u7ec4\u7d22\u5f15\u8bbf\u95ee\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = 0; i &lt; 5; i++) {\n    Console.WriteLine($\"\u7b2c {i} \u6b21\u5faa\u73af\");\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u7ed3\u6784<\/strong>\uff1a\u521d\u59cb\u5316\uff1b\u6761\u4ef6\u5224\u65ad\uff1b\u6bcf\u8f6e\u5faa\u73af\u540e\u7684\u8868\u8fbe\u5f0f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 <code>while<\/code> \u5faa\u73af<\/h3>\n\n\n\n<p>\u5148\u5224\u65ad\u6761\u4ef6\uff0c\u518d\u51b3\u5b9a\u662f\u5426\u6267\u884c\u3002\u9002\u7528\u4e8e\u7528\u6237\u8f93\u5165\u3001\u6570\u636e\u6d41\u7b49\u4e0d\u786e\u5b9a\u6761\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int i = 0;\nwhile (i &lt; 5) {\n    Console.WriteLine(i);\n    i++;\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u6ce8\u610f<\/strong>\uff1a\u5faa\u73af\u4f53\u5185\u5fc5\u987b\u6539\u53d8\u6761\u4ef6\u53d8\u91cf\uff0c\u907f\u514d\u6b7b\u5faa\u73af\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3\ufe0f\u20e3 <code>do...while<\/code> \u5faa\u73af<\/h3>\n\n\n\n<p>\u4e0e <code>while<\/code> \u7c7b\u4f3c\uff0c\u4f46\u5148\u6267\u884c\u4e00\u6b21\u5faa\u73af\u4f53\uff0c\u518d\u5224\u65ad\u6761\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int i = 0;\ndo {\n    Console.WriteLine(i);\n    i++;\n} while (i &lt; 5);<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u81f3\u5c11\u9700\u8981\u6267\u884c\u4e00\u6b21\u7684\u903b\u8f91\uff0c\u5982\u201c\u8f93\u5165\u9a8c\u8bc1\u201d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4\ufe0f\u20e3 <code>foreach<\/code> \u5faa\u73af<\/h3>\n\n\n\n<p>\u7528\u4e8e\u904d\u5386\u96c6\u5408\u3001\u6570\u7ec4\u3001\u5217\u8868\uff0c\u8bed\u6cd5\u5b89\u5168\uff0c\u907f\u514d\u8d8a\u754c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string&#91;] names = { \"Alice\", \"Bob\", \"Charlie\" };\nforeach (string name in names) {\n    Console.WriteLine(name);\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc \u4e0d\u652f\u6301\u7d22\u5f15\u8bbf\u95ee\u6216\u4fee\u6539\u5143\u7d20\u5185\u5bb9\uff08\u53ea\u8bfb\u904d\u5386\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u5faa\u73af\u63a7\u5236\u8bed\u53e5<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5173\u952e\u5b57<\/th><th>\u529f\u80fd\u8bf4\u660e<\/th><th>\u793a\u4f8b<\/th><\/tr><\/thead><tbody><tr><td><code>break<\/code><\/td><td>\u8df3\u51fa\u5f53\u524d\u5faa\u73af<\/td><td><code>if (x &gt; 5) break;<\/code><\/td><\/tr><tr><td><code>continue<\/code><\/td><td>\u8df3\u8fc7\u5f53\u524d\u5faa\u73af\u4e2d\u7684\u5269\u4f59\u8bed\u53e5\uff0c\u8fdb\u5165\u4e0b\u4e00\u8f6e<\/td><td><code>if (x % 2 == 0) continue;<\/code><\/td><\/tr><tr><td><code>return<\/code><\/td><td>\u7ec8\u6b62\u65b9\u6cd5\u6267\u884c\uff0c\u76f4\u63a5\u8fd4\u56de<\/td><td><code>return;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u5d4c\u5957\u5faa\u73af<\/h2>\n\n\n\n<p>\u5faa\u73af\u4e2d\u53ef\u5d4c\u5957\u5176\u4ed6\u5faa\u73af\uff0c\u5982\u8f93\u51fa\u4e5d\u4e5d\u4e58\u6cd5\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = 1; i &lt;= 9; i++) {\n    for (int j = 1; j &lt;= i; j++) {\n        Console.Write($\"{j}\u00d7{i}={i * j}\\t\");\n    }\n    Console.WriteLine();\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001<code>foreach<\/code> vs <code>for<\/code><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5bf9\u6bd4\u70b9<\/th><th><code>for<\/code><\/th><th><code>foreach<\/code><\/th><\/tr><\/thead><tbody><tr><td>\u662f\u5426\u53ef\u4fee\u6539\u5143\u7d20<\/td><td>\u2705 \u662f<\/td><td>\u274c \u4e0d\u53ef<\/td><\/tr><tr><td>\u662f\u5426\u6709\u7d22\u5f15<\/td><td>\u2705 \u6709<\/td><td>\u274c \u65e0<\/td><\/tr><tr><td>\u5b89\u5168\u6027<\/td><td>\u6613\u51fa\u9519\uff08\u5982\u8d8a\u754c\uff09<\/td><td>\u66f4\u5b89\u5168<\/td><\/tr><tr><td>\u4f7f\u7528\u573a\u666f<\/td><td>\u7cbe\u786e\u63a7\u5236\u5faa\u73af\uff08\u5982\u8df3\u8dc3\u8bbf\u95ee\uff09<\/td><td>\u7b80\u6d01\u904d\u5386\u96c6\u5408<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5e38\u89c1\u8bef\u533a &amp; \u6700\u4f73\u5b9e\u8df5<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u26a0\ufe0f \u5e38\u89c1\u9519\u8bef<\/th><th>\u2705 \u5efa\u8bae\u5199\u6cd5\u6216\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u6b7b\u5faa\u73af<\/td><td>\u786e\u4fdd\u5faa\u73af\u4e2d\u6761\u4ef6\u53d8\u91cf\u88ab\u6b63\u786e\u66f4\u65b0<\/td><\/tr><tr><td><code>foreach<\/code> \u4e2d\u4fee\u6539\u96c6\u5408\u5143\u7d20<\/td><td>\u4e0d\u5141\u8bb8\uff0c\u9700\u4f7f\u7528 <code>for<\/code> \u66ff\u4ee3<\/td><\/tr><tr><td>\u672a\u52a0 <code>{}<\/code> \u5bfc\u81f4\u903b\u8f91\u9519\u8bef<\/td><td>\u6c38\u8fdc\u4f7f\u7528 <code>{}<\/code> \u5305\u88f9\u5faa\u73af\u4f53\uff0c\u54ea\u6015\u53ea\u6709\u4e00\u884c<\/td><\/tr><tr><td>\u6ee5\u7528\u5d4c\u5957\u5faa\u73af\u6548\u7387\u4f4e\u4e0b<\/td><td>\u5c3d\u91cf\u7b80\u5316\u5d4c\u5957\u5c42\u7ea7\uff0c\u5fc5\u8981\u65f6\u8003\u8651\u4f7f\u7528 <code>yield return<\/code><\/td><\/tr><tr><td>\u4e0d\u89c4\u8303\u547d\u540d\u6216\u6ce8\u91ca\u6df7\u4e71<\/td><td>\u517b\u6210\u6ce8\u91ca\u548c\u6e05\u6670\u547d\u540d\u7684\u4e60\u60ef\uff0c\u63d0\u5347\u4ee3\u7801\u53ef\u8bfb\u6027<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u9ad8\u7ea7\u6280\u5de7\u4e0e\u62d3\u5c55\u7528\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 <code>Enumerable.Range<\/code> + <code>foreach<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>foreach (var i in Enumerable.Range(0, 5)) {\n    Console.WriteLine(i);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 <code>Parallel.For<\/code> \u5e76\u884c\u5faa\u73af\uff08\u9700\u5f15\u5165 <code>System.Threading.Tasks<\/code>\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Parallel.For(0, 10, i =&gt; {\n    Console.WriteLine($\"\u7ebf\u7a0b {Thread.CurrentThread.ManagedThreadId}\uff1a{i}\");\n});<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u63a8\u8350\u9605\u8bfb\u4e0e\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udcd8 <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/statements\/iteration-statements\">C# \u5faa\u73af\u8bed\u53e5\u5b98\u65b9\u6587\u6863\uff08Microsoft Docs\uff09<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 <a href=\"https:\/\/learn.microsoft.com\/en-us\/training\/modules\/csharp-if-elseif-switch\/\">Microsoft Learn: \u63a7\u5236\u6d41\u7a0b\u6559\u7a0b<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/statements\/iteration-statements#the-foreach-statement\">foreach \u5faa\u73af\uff08\u5fae\u8f6f\u5b98\u65b9\uff09<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\uff0c\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7ae0\u8282\u91cd\u70b9\u5173\u4e8e\u8bb2\u89e3 C# \u5faa\u73af\uff08Loops\uff09\u7ed3\u6784\uff0c\u6db5\u76d6\uff1a \ud83d\udcda \u5b98\u65b9\u6743\u5a01\u94fe\u63a5\uff08\u4fe1\u606f\u5747\u6765\u81ea Microsoft \u5b98 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3494,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3492","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3492"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3492\/revisions"}],"predecessor-version":[{"id":3495,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3492\/revisions\/3495"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3494"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}