{"id":803,"date":"2021-09-08T20:30:52","date_gmt":"2021-09-08T12:30:52","guid":{"rendered":"http:\/\/www.wayln.com\/?p=803"},"modified":"2021-09-08T20:30:52","modified_gmt":"2021-09-08T12:30:52","slug":"%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e5%9f%ba%e7%a1%80-%e4%bd%bf%e7%94%a8valuetask","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=803","title":{"rendered":"\u5f02\u6b65\u7f16\u7a0b\u57fa\u7840\u2014\u2014\u4f7f\u7528ValueTask"},"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>\u9700\u8981\u4f7f\u7528ValueTask&lt;T&#62;\u503c<\/p>\n<h1><span id=\"i-2\">\u89e3\u51b3\u65b9\u6848<\/span><\/h1>\n<p>\u8981\u4f7f\u7528ValueTask&lt;T&#62;\u503c\u6216ValueTask\u503c\uff0c\u6700\u76f4\u63a5\u3001\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528await\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u53ea\u9700\u8981\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">ValueTask&lt;int&gt; methodAsync();\nasync Task ConsumingMethodAsync()\n{\n    int value=await MethodAsync();\n}\n<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u5728\u6267\u884c\u5e76\u53d1\u64cd\u4f5c\u540e\u6267\u884cawait,\u8fd9\u548cTask&lt;T&#62;\u7684\u7528\u6cd5\u4e00\u6837<\/p>\n<pre><code class=\"language-CSharp line-numbers\">ValueTask&lt;int&gt; MethodAsync();\nasync Task ConsumingMethodAsync()\n{\n    ValueTask&lt;int&gt; valueTask=MethodAsync();\n    ...\/\/\u5176\u4ed6\u5e76\u53d1\u5de5\u4f5c\n    int value=await valueTask;\n}\n<\/code><\/pre>\n<p><strong>\u5bf9ValueTask\u6216ValueTask&lt;T&#62;\u53ea\u53ef\u7b49\u5f85\u4e00\u6b21<\/strong><br \/>\n\u82e5\u8981\u8fdb\u884c\u66f4\u4e3a\u590d\u6742\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528AsTask\u628aValueTask&lt;T&#62;\u8f6c\u6362\u4e3aTask&lt;T&#62;\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">ValueTask&lt;int&gt; MethodAsync();\nasync Task ConsumingMethodAsync()\n{\n    Task&lt;int&gt; task=MethodAsync().AsTask();\n    ...\/\/\u5176\u4ed6\u5e76\u53d1\u5de5\u4f5c\n    int value=await task;\n    int anotherValue=await task;\n}\n<\/code><\/pre>\n<p>\u5bf9Task&lt;T&#62;\u591a\u6267\u884cawait\u662f\u7edd\u5bf9\u5b89\u5168\u7684\u3002\u901a\u8fc7\u5b83\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u5176\u4ed6\u64cd\u4f5c\uff0c\u6bd4\u5982\u5f02\u6b65\u7b49\u5f85\u591a\u4e2a\u64cd\u4f5c\u5b8c\u6210<\/p>\n<pre><code class=\"language-CSharp line-numbers\">ValueTask&lt;int&gt; MethodAsync();\nasync Task ConsumingMethodAsync()\n{\n    Task&lt;int&gt; task1=MethodAsync().AsTask();\n    Task&lt;int&gt; task2=MethodAsync().AsTask();\n    int[] results=awaitTask.WhenAll(task1,task2);\n}\n<\/code><\/pre>\n<p>\u4f46\u662f\uff0c\u5bf9\u4e8e\u6ca1\u4e2aValueTask&lt;T&#62;\uff0c\u53ea\u80fd\u8c03\u7528\u4e00\u6b21AsTask\u3002\u4e00\u79cd\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u5c06\u5176\u5373\u53ef\u8f6c\u6362\u4e3aTask&lt;T&#62;\u3002\u7136\u540e\u65e0\u89c6ValueTask&lt;T&#62;\u3002\u4e0d\u80fd\u518d\u540c\u4e00\u4e2aValueTask&lt;T&#62;\u4e2d\u5bf9AsTask\u540c\u65f6\u52a0\u4ee5await\u548c\u8c03\u7528\u3002<br \/>\n\u5927\u591a\u6570\u7684\u4ee3\u7801\u786c\u86cb\u4e2a\u8981\u4e48\u5373\u53ef\u5bf9ValueTask&lt;T&#62;\u52a0\u4ee5await,\u8981\u4e48\u5c06\u8f6c\u6362\u4e3aTask&lt;T&#62;<\/p>\n<h1><span id=\"i-3\">\u8ba8\u8bba<\/span><\/h1>\n<p>ValueTask&lt;T&#62;\u7684\u5176\u4ed6\u5c5e\u6027\u7528\u4e8e\u66f4\u9ad8\u7ea7\u7684\u76ee\u7684\uff0c\u4ed6\u4eec\u7684\u8868\u73b0\u65b9\u5f0f\u4e0e\u90a3\u4e9b\u5e38\u89c1\u7684\u5c5e\u6027\u4e0d\u4e00\u6837\uff0c\u5c24\u5176\u662fValueTask&lt;T&#62;.Result\u8f83\u4e4b\u4e0eTask&lt;T&#62;.Result\u7684\u5c40\u9650\u66f4\u591a\u3002\u4eceValueTask&lt;T&#62;\u4e2d\u540c\u6b65\u83b7\u53d6\u7ed3\u679c\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u8c03\u7528ValueTask&lt;T&#62;.Result\u6216\u8005ValueTask&lt;T&#62;.GetAwaiter().GetResult(),\u4f46\u5728ValueTask&lt;T&#62;\u5c1a\u672a\u5b8c\u6210\u4e4b\u524d\uff0c\u7edd\u4e0d\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e9b\u5c5e\u6027\u3002\u4eceTask<T\/>\u540c\u6b65\u83b7\u53d6\u7ed3\u679c\u4f1a\u963b\u585e\u8c03\u7528\u7ebf\u7a0b\uff0c\u76f4\u81f3\u4efb\u52a1\u5b8c\u6210\u4e3a\u6b62\uff0c\u800cValueTask<T\/>\u6ca1\u6709\u8fd9\u79cd\u4fdd\u969c\u3002<\/p>\n<p>\u60f3\u4eceValueTask\u6216ValueTask&lt;T&#62;\u4e2d\u540c\u6b65\u4e8e\u83b7\u53d6\u7ed3\u679c\uff0c\u53ea\u80fd\u83b7\u53d6\u4e00\u6b21\uff0c\u4e14\u987b\u5728ValueTask\u5b8c\u6210\u4e4b\u540e\uff0c\u5e76\u4e14\u4e0d\u53ef\u5bf9\u8be5ValueTask\u52a0\u4ee5await,\u4e5f\u4e0d\u53ef\u80fd\u5c06\u5176\u8f6c\u6362\u4e3a\u4efb\u52a1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u95ee\u98982 \u89e3\u51b3\u65b9\u68483 \u8ba8\u8bba \u95ee\u9898 \u9700\u8981\u4f7f\u7528ValueTask&lt;T&#62;\u503c \u89e3\u51b3\u65b9\u6848 [&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-803","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\/803","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=803"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/803\/revisions"}],"predecessor-version":[{"id":804,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/803\/revisions\/804"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}