6

I'm learning TAP, and I'm wondering what feature of .NET allows the result in this method to be implicitly cast into or interpreted as a 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 to result, which is declared as a String?

Second, why can I say Return result though the return type is Task(Of String).

4
  • @I3arnon -- I deleted your VB.NET tag. My example is in VB.NET but my question applies equally to C# (or any other CLR language). Commented Dec 19, 2014 at 18:07
  • It doesn't matter. The tag doesn't limit exposure, it expands it. Most .Net question are language-agnostic, but they are still tagged with the language they are in. Commented Dec 19, 2014 at 18:10
  • @I3arnon -- Gotcha. I added it back. Why does SO limit it to 5 tags max then? Commented Dec 19, 2014 at 18:12
  • That's a question for meta, but 5 is probably enough. Commented Dec 19, 2014 at 18:13

3 Answers 3

5

The key is in the Async/Await keywords.

Async tells the compiler that you're going to pretend you're returning a string, and it needs to deal with the complexity around the fact that you're actually returning a Task that will eventually yield a string.

Await can only be used in an Async method, and it tells the compiler that the next expression is going to return a Task that produces something, and the compiler needs to address the complexity around pretending that the rest of the code after this point is given that thing that the task will return (a String in this case).

Sign up to request clarification or add additional context in comments.

Comments

2

The feature is TAP itself (Task-based Asynchronous Pattern) or async-await as it's mostly called. The async keyword tells the compiler to generate a state machine and so you are able to use await. It also generates a Task (with the result value, if there is one) or any exception that may arise while the method is running.

In your case pipeReader.ReadLineAsync() returns a Task(Of String) and not simply String. Await is what enables you to "extract" the actual result out of that task when it's completed.

And when you return result yourself, the compiler knows to generate a Task(Of String) that when awaited results in a String (or an exception, if there was one)

Comments

0

For your first question, not the Await pipeReader.ReadLineAsync() returns Task<string> but the pipeReader.ReadLineAsync() method itself. Having it awaited, you get the result of execution.

Secondly, returning the string from the method that is supposed to return Task<string> is done thanks to async decoration, that indicates the compiler to make this conversion for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.