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
✗ Incorrect
Use Task<int> to represent an async operation that returns an integer.
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
✗ Incorrect
await pauses the method but lets other work run on the thread.
What is the return type of an async method that does not return a value?
ATask
BTask<void>
Cvoid
Dint
✗ Incorrect
Async methods that don't return a value should return Task.
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<T></code> from a method
DUsing <code>async</code> keyword
✗ Incorrect
Calling Wait() blocks the thread and can freeze the UI.
How do you declare an async method that returns a string?
Astring MethodName()
Basync void MethodName()
CTask MethodName()
Dasync Task<string> MethodName()
✗ Incorrect
Use async Task<string> to return a string asynchronously.
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.
Practice
(1/5)
1. What is the main difference between Task and Task<T> in C#?
easy
A. Task does not return a value, Task<T> returns a value of type T.
B. Task runs synchronously, Task<T> runs asynchronously.
C. Task is used only for UI threads, Task<T> for background threads.
D. Task can return multiple values, Task<T> returns only one.
Solution
Step 1: Understand Task behavior
Task represents an asynchronous operation that does not return a result.
Step 2: Understand Task<T> behavior
Task<T> represents an asynchronous operation that returns a value of type T when it completes.
Final Answer:
Task does not return a value, Task<T> returns a value of type T. -> Option A
Quick Check:
Return value presence = Task does not return a value, Task<T> returns a value of type T. [OK]
Hint: Remember: Task = no result, Task<T> = returns result [OK]
Common Mistakes:
Thinking Task runs synchronously
Confusing Task<T> with multiple return values
Assuming Task is only for UI threads
2. Which of the following is the correct way to declare a Task<int> that returns the number 5 asynchronously?
easy
A. Task<int> t = Task.Run(() => 5);
B. Task<int> t = new Task<int>(5);
C. Task<int> t = Task<int>.Run(5);
D. Task<int> t = Task.Run<int>(5);
Solution
Step 1: Check correct Task<T> creation syntax
Use Task.Run(() => value) to create and start a task returning a value.
Step 2: Validate each option
Task<int> t = Task.Run(() => 5); uses Task.Run(() => 5), which is correct. Options A, B, and C use invalid constructors or methods.