0
0
C Sharp (C#)programming~5 mins

Returning values from async methods in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the return type of an async method that returns a value in C#?
The return type is Task<T>, where T is the type of the value returned. For example, async Task<int> returns an integer asynchronously.
Click to reveal answer
beginner
How do you return a value from an async method?
Use the return keyword followed by the value inside the async method. The value will be wrapped automatically in a Task<T>.
Click to reveal answer
beginner
What keyword do you use to wait for the result of an async method that returns a value?
You use the await keyword to wait for the completion of the async method and get its returned value.
Click to reveal answer
intermediate
Can an async method return void?
Yes, but only for event handlers. Normally, async methods should return Task or Task<T> to allow callers to await them and handle exceptions.
Click to reveal answer
intermediate
What happens if you return a value directly from an async method without wrapping it in a Task?
You cannot return a value directly without wrapping it in Task<T>. The compiler requires async methods to return Task, Task<T>, or void.
Click to reveal answer
What is the correct return type for an async method that returns a string?
Avoid
Bstring
CTask<string>
DTask
Which keyword do you use to get the result from an async method?
Aawait
Byield
Casync
Dreturn
What is the return type of an async method that does not return a value?
ATask
Bvoid
CTask<void>
Dint
Can you use 'await' inside a method that is not marked async?
AYes, always
BNo, the method must be async
COnly if the method returns void
DOnly in constructors
What happens if you forget to await an async method that returns a value?
AThe value is returned immediately
BThe method runs synchronously
CThe program crashes
DYou get the Task object instead of the value
Explain how to write an async method that returns an integer value and how to get that value when calling the method.
Think about the method signature and how to wait for the result.
You got /4 concepts.
    Describe the difference between async methods returning Task, Task, and void.
    Consider how each return type affects calling and error handling.
    You got /4 concepts.