📖 Scenario: You are building a simple program that fetches user data asynchronously, simulating a real-world scenario where data comes from a slow source like a web server.
🎯 Goal: Create an async method that returns a value, then call it and display the result.
📋 What You'll Learn
Create an async method called GetUserNameAsync that returns a Task<string>.
Inside GetUserNameAsync, simulate a delay of 1 second using await Task.Delay(1000).
Return the string "Alice" from GetUserNameAsync.
Call GetUserNameAsync from Main using await and store the result in a variable called userName.
Print the value of userName to the console.
💡 Why This Matters
🌍 Real World
Async methods that return values are common when fetching data from web APIs, databases, or files without freezing the app.
💼 Career
Understanding async methods and how to get their results is essential for writing responsive and efficient C# applications.
Progress0 / 4 steps
1
Create the async method skeleton
Write an async method called GetUserNameAsync that returns Task<string> and inside it, return the string "Alice" immediately.
C Sharp (C#)
Hint
Remember to mark the method with async and set the return type to Task<string>.
2
Add delay simulation inside the async method
Inside the GetUserNameAsync method, add a 1 second delay using await Task.Delay(1000); before returning "Alice".
C Sharp (C#)
Hint
Use await Task.Delay(1000); to pause for 1 second before returning the value.
3
Call the async method and store the result
Inside the Main method, call GetUserNameAsync() using await and store the result in a variable called userName.
C Sharp (C#)
Hint
Use string userName = await GetUserNameAsync(); inside Main.
4
Print the returned value
Add a Console.WriteLine statement inside Main to print the value of userName.
C Sharp (C#)
Hint
Use Console.WriteLine(userName); to display the result.
Practice
(1/5)
1. What type should an async method return if it needs to return a value of type int?
easy
A. void
B. Task
C. int
D. Task<int>
Solution
Step 1: Understand async method return types
Async methods that return a value must return Task<T> where T is the value type.
Step 2: Match the return type to the value type
Since the method returns an int, the return type should be Task<int>.
Final Answer:
Task<int> -> Option D
Quick Check:
Async method returning int uses Task<int> [OK]
Hint: Async methods returning values use Task<T> [OK]
Common Mistakes:
Using void for async methods that return values
Returning int directly from async method
Using Task without generic type for returning values
2. Which of the following is the correct syntax to declare an async method that returns a string value?
easy
A. async Task<string> GetNameAsync()
B. string async Task GetNameAsync()"
C. Task async string GetNameAsync()
D. async string GetNameAsync()
Solution
Step 1: Identify correct async method syntax
An async method returning a value must have the signature: async Task<T> MethodName().
Step 2: Check each option
async Task<string> GetNameAsync() matches the correct syntax: async Task<string> GetNameAsync(). Others have wrong order or missing Task<T>.
async Task<int> GetNumberAsync() { return 5; }
async Task Main()
{
int result = await GetNumberAsync();
Console.WriteLine(result);
}
medium
A. Task<int>
B. 5
C. Compilation error
D. 0
Solution
Step 1: Understand async method return and await
The method GetNumberAsync returns a Task<int> with value 5. Await unwraps the value.
Step 2: Check what is printed
The awaited result is 5, so Console.WriteLine(result) prints 5.
Final Answer:
5 -> Option B
Quick Check:
Await unwraps Task<int> to int value 5 [OK]
Hint: Await unwraps Task<T> to get the value [OK]
Common Mistakes:
Printing Task<int> instead of awaited int
Expecting compilation error due to async
Assuming default int value 0 is printed
4. Identify the error in this async method:
async Task<int> CalculateAsync()
{
int value = 10;
return Task.FromResult(value);
}
medium
A. No error, code is correct
B. Method should return int, not Task<int>
C. Missing await keyword before Task.FromResult
D. Returning Task.FromResult inside async method causes nested Task
Solution
Step 1: Analyze return statement in async method
Async methods can return Task<T> directly, such as Task.FromResult(value), which is a common and valid pattern for completed tasks.
Step 2: Verify the code runs correctly
The async state machine properly handles the returned Task<int>, producing a single Task<int> with value 10 for the caller. No nesting or errors occur.
Final Answer:
No error, code is correct -> Option A
Quick Check:
Task.FromResult is valid in async Task<int> methods [OK]
Hint: Task.FromResult(value) is valid in async Task<T> methods [OK]
Common Mistakes:
Misunderstanding that Task.FromResult causes nested Tasks
Thinking async methods must return T directly, not Task<T>
Believing await is required before Task.FromResult
5. You want to write an async method that fetches user data and returns the user's name as a string. The fetching is simulated by a delay. Which of the following implementations correctly returns the name without blocking the caller?
hard
A. async string GetUserNameAsync() { await Task.Delay(1000); return \"Alice\"; }
B. Task<string> GetUserNameAsync() { return \"Alice\"; }
C. async Task<string> GetUserNameAsync() { await Task.Delay(1000); return \"Alice\"; }
D. async Task GetUserNameAsync() { await Task.Delay(1000); return \"Alice\"; }"
Solution
Step 1: Check method signature and return type
The method must be async and return Task<string> to return a string asynchronously.
Step 2: Verify method body and return statement
async Task<string> GetUserNameAsync() { await Task.Delay(1000); return \"Alice\"; } uses await Task.Delay(1000) to simulate delay and returns the string correctly. Others have wrong return types or missing async.