【发布时间】:2015-02-18 16:50:42
【问题描述】:
我正在学习TAP,我想知道.NET 的哪些功能允许将此方法中的result 隐式转换为Task(Of String) 或解释为Task(Of String):
Public Async Function CheckHostInstructionAsync() As Task(Of String)
Dim result As String
result = Await pipeReader.ReadLineAsync() 'pipeReader is a System.IO.StreamReader
If (result.Equals("exit", StringComparison.InvariantCultureIgnoreCase)) Then terminate = True
Return result
End Function
首先,如果Await pipeReader.ReadLineAsync()“返回”一个Task(Of String),为什么我可以将它分配给result,它被声明为一个String?
第二,为什么我可以说Return result,虽然返回类型是Task(Of String)。
【问题讨论】:
-
@I3arnon -- 我删除了你的 VB.NET 标签。我的示例在 VB.NET 中,但我的问题同样适用于 C#(或任何其他 CLR 语言)。
-
没关系。标签不会限制曝光,它会扩展它。大多数 .Net 问题与语言无关,但它们仍被标记为所使用的语言。
-
@I3arnon -- 明白了。我加回来了。那么为什么 SO 将其限制为最多 5 个标签呢?
-
这是meta的问题,但5个可能就足够了。
标签: .net vb.net task-parallel-library async-await