Asynchronous function returning Task (Of String) or String?

I am studying TAP , and I am wondering what feature of .NET allows you to use resultimplicitly in this method or interpret it as 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

First, if Await pipeReader.ReadLineAsync()"returns" a Task(Of String), why can I assign it resultwhich is declared as String?

Secondly, why can I say the Return resultreturn type though Task(Of String).

+4
source share
3 answers

TAP ( ) async-await, . async await. Task ( , ), , .

pipeReader.ReadLineAsync() Task(Of String), String. await - , "" , .

result , , Task(Of String), String ( , )

+2

Async/Await.

Async , , string, , , , .

Await Async, , Task, - , , , , (a string ).

+4

Await pipeReader.ReadLineAsync() Task<string>, pipeReader.ReadLineAsync(). await ed, .

Secondly, returning a string from a method that should return Task<string>is done due to asyncdecoration, which tells the compiler to do this conversion for you.

0
source

Source: https://habr.com/ru/post/1568367/


All Articles