{"id":827,"date":"2021-09-12T20:11:23","date_gmt":"2021-09-12T12:11:23","guid":{"rendered":"http:\/\/www.wayln.com\/?p=827"},"modified":"2021-09-12T20:11:23","modified_gmt":"2021-09-12T12:11:23","slug":"%e5%bc%82%e6%ad%a5%e6%b5%81-%e5%88%9b%e5%bb%ba%e5%bc%82%e6%ad%a5%e6%b5%81","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=827","title":{"rendered":"\u5f02\u6b65\u6d41\u2014\u2014\u521b\u5efa\u5f02\u6b65\u6d41"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_transparent no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#i\"><span class=\"toc_number toc_depth_1\">1<\/span> \u95ee\u9898<\/a><\/li><li><a href=\"#i-2\"><span class=\"toc_number toc_depth_1\">2<\/span> \u89e3\u51b3\u65b9\u6848<\/a><\/li><li><a href=\"#i-3\"><span class=\"toc_number toc_depth_1\">3<\/span> \u8ba8\u8bba<\/a><\/li><\/ul><\/div>\n<h1><span id=\"i\">\u95ee\u9898<\/span><\/h1>\n<p>\u5047\u8bbe\u9700\u8981\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u800c\u4e14\u9700\u8981\u5bf9\u6bcf\u4e2a\u503c\u6267\u884c\u4e00\u4e9b\u5f02\u6b65\u5904\u7406\u3002<br \/>\n&#8211; \u901a\u8fc7IEnumerable&lt;T&#62;\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u7136\u540e\u5bf9\u5176\u8fdb\u884c\u5f02\u6b65\u5904\u7406<br \/>\n&#8211; \u901a\u8fc7Task&lt;T&#62;\u5f02\u6b65\u8fd4\u56de\u5355\u4e2a\u503c\uff0c\u7136\u540e\u6dfb\u52a0\u7684\u5176\u4ed6\u7684\u8fd4\u56de\u503c<\/p>\n<h1><span id=\"i-2\">\u89e3\u51b3\u65b9\u6848<\/span><\/h1>\n<p>\u901a\u8fc7yield return \u53ef\u4ee5\u4ece\u4e00\u4e2a\u65b9\u6cd5\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u800c\u5f02\u6b65\u65b9\u6cd5\u4f7f\u7528async\u548cawait\u3002\u5728\u4f7f\u7528\u5f02\u6b65\u6d41\u65f6\uff0c\u53ef\u4ee5\u5c06\u8fd9\u4e24\u8005\u5408\u5e76\u3002\u53ea\u7528IAsyncEnumerable&lt;T&#62;\u7684\u8fd4\u56de\u7c7b\u578b\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">async IAsyncEnumerable&lt;int&gt; GetValueAsync()\n{\n    await Task.Delay(1000);\/\/\u5f02\u6b65\u5904\u7406\n    yield return 10;\n    await Task.Delay(1000);\/\/\u66f4\u591a\u5f02\u6b65\u5904\u7406\n    yield return 13;\n}\n<\/code><\/pre>\n<p>\u5728\u4e0b\u9762\u8fd9\u4e2a\u66f4\u5207\u5408\u5b9e\u9645\u7684\u5b9e\u4f8b\u4e2d\uff0cAPI\u4f7f\u7528\u4e86\u5206\u9875\u53c2\u6570\uff0c\u8be5\u5b9e\u4f8b\u5f02\u6b65\u679a\u4e3e\u4e86API\u7684\u6240\u6709\u8fd4\u56de\u7ed3\u679c<\/p>\n<pre><code class=\"language-CSharp line-numbers\">async IAsyncEnumerable&lt;string&gt; GetValuesAsync(HttpClient client)\n{\n    int offset=0;\n    const int limit=10;\n    while(true)\n    {\n        string result =await client.GetStringAsync($\"https:\/\/exampleurl\/api\/values?offset={offset}&amp;limit={limit}\");\n        string[] valuesOnThisPage=result.Split(\"\\n\");\n        foreach(string value in valuesOnThisPage)\n        {\n            yield return value;\n        }\n        if(valuesOnThisPage.Length!=limit)\n        {\n            break;\n        }\n        offset+=limit;\n    }\n}\n<\/code><\/pre>\n<h1><span id=\"i-3\">\u8ba8\u8bba<\/span><\/h1>\n<p>\u5bf9\u5f02\u6b65\u6d41\u800c\u8a00\uff0c\u8fd9\u662f\u4e00\u79cd\u5e38\u89c4\u6a21\u5f0f\uff0c\u5bf9\u8bb8\u591a\u6d41\u800c\u8a00\uff0c\u5927\u591a\u6570\u5f02\u6b65\u8fed\u4ee3\u5176\u5b9e\u662f\u540c\u6b65\u7684\uff0c\u5f02\u6b65\u6d41\u53ea\u662f\u5141\u8bb8\u4e00\u6b65\u83b7\u53d6\u4e0b\u4e00\u9879\u800c\u5df2\u3002\u4e0d\u8bba\u5f02\u6b65\u6d41\u83b7\u53d6\u9879\u7684\u65b9\u5f0f\u662f\u540c\u6b65\u8fd8\u662f\u5f02\u6b65\uff0c\u7531\u4e8e\u5176\u5b9e\u8d28\u4e0a\u4f7f\u7528\u4e86ValueTask&lt;T&#62;,\u56e0\u6b64\u5f02\u6b65\u6d41\u7684\u6548\u7387\u90fd\u5f97\u4ee5\u6700\u5927\u5316\u3002<br \/>\n\u5728\u5b9e\u73b0\u5f02\u6b65\u6d41\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u652f\u6301\u53d6\u6d88\u64cd\u4f5c\u3002\u53ef\u4ee5\u901a\u8fc7CancellationToken\u652f\u6301\u6070\u5f53\u7684\u53d6\u6d88\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u95ee\u98982 \u89e3\u51b3\u65b9\u68483 \u8ba8\u8bba \u95ee\u9898 \u5047\u8bbe\u9700\u8981\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u800c\u4e14\u9700\u8981\u5bf9\u6bcf\u4e2a\u503c\u6267\u884c\u4e00\u4e9b\u5f02\u6b65\u5904\u7406\u3002 &#038; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,41,2],"tags":[],"class_list":["post-827","post","type-post","status-publish","format-standard","hentry","category-c","category-41","category-2"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=827"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/827\/revisions"}],"predecessor-version":[{"id":828,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/827\/revisions\/828"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}