📖 Scenario: You are building a simple program that simulates asynchronous work using Task and Task<T> in C#. This is common when you want to run operations without blocking the main program, like loading data or performing calculations in the background.
🎯 Goal: Learn how to create and use Task for running code asynchronously without a return value, and Task<T> for running code that returns a result asynchronously.
📋 What You'll Learn
Create a Task that runs a simple action asynchronously.
Create a Task<int> that returns a number asynchronously.
Use await to get the result from Task<int>.
Print the results to the console.
💡 Why This Matters
🌍 Real World
Asynchronous programming is used in apps to keep the interface responsive while doing work like loading files, fetching data from the internet, or processing large calculations.
💼 Career
Understanding <code>Task</code> and <code>Task<T></code> is essential for C# developers working on modern applications, especially those involving UI, web services, or any background processing.
Progress0 / 4 steps
1
Create a simple Task
Write a line of code to create a Task called simpleTask that runs a lambda expression printing "Task is running" to the console.
C Sharp (C#)
Hint
Use new Task(() => ...) to create a Task that runs the code inside the lambda.
2
Create a Task<int> that returns a value
Add a line of code to create a Task<int> called numberTask that returns the number 42 asynchronously using a lambda expression.
C Sharp (C#)
Hint
Use new Task(() => 42) to create a Task that returns 42.
3
Start the tasks and await the result
Write code to start both simpleTask and numberTask using Start(). Then use await to get the integer result from numberTask into a variable called result.
C Sharp (C#)
Hint
Call Start() on each task to run them. Use await to get the result from numberTask.
4
Print the results
Write two Console.WriteLine statements: one to print "Simple task completed" after simpleTask finishes, and another to print "Number from task: {result}" using the result variable.
C Sharp (C#)
Hint
Use await simpleTask to wait for it to finish before printing. Use Console.WriteLine with an interpolated string for the number.
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.