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

Task and Task of T types in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a Task in C#?
A Task represents an asynchronous operation that does not return a value. It lets your program do work in the background without blocking the main thread.
Click to reveal answer
beginner
What is the difference between Task and Task<T>?
Task is for async operations that return no result. Task<T> is for async operations that return a value of type T when completed.
Click to reveal answer
beginner
How do you get the result from a Task<T>?
You can use the await keyword to wait for the task to finish and get its result, like: T result = await task;
Click to reveal answer
intermediate
What happens if you call Wait() on a Task?
Calling Wait() blocks the current thread until the task finishes. This can cause your program to freeze if used on the main thread.
Click to reveal answer
intermediate
Why is using async and await better than blocking with Wait()?
Because <code>async</code> and <code>await</code> let your program continue running other work while waiting, improving responsiveness and avoiding freezes.
Click to reveal answer
Which type should you use for an async method that returns a number?
Avoid
BTask
CTask<int>
Dint
What does await do when used with a Task?
ABlocks the thread until the task finishes
BPauses the method until the task completes without blocking the thread
CStarts the task
DCancels the task
What is the return type of an async method that does not return a value?
ATask
BTask<void>
Cvoid
Dint
Which of these can cause your UI to freeze?
ACalling <code>Wait()</code> on a Task on the UI thread
BUsing <code>await</code> on a Task
CReturning <code>Task&lt;T&gt;</code> from a method
DUsing <code>async</code> keyword
How do you declare an async method that returns a string?
Astring MethodName()
Basync void MethodName()
CTask MethodName()
Dasync Task<string> MethodName()
Explain the difference between Task and Task<T> and when to use each.
Think about whether your async method returns a value or not.
You got /4 concepts.
    Describe how await works with Task and why it is preferred over blocking calls.
    Consider what happens to the program flow when you await a task.
    You got /4 concepts.