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

Returning values from async methods in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an async method that returns an integer.

C Sharp (C#)
public async Task<[1]> GetNumberAsync() {
    return 42;
}
Drag options to blanks, or click blank then click option'
ATask
Bvoid
Cstring
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as return type for async methods that return a value.
Using Task without specifying the type parameter.
2fill in blank
medium

Complete the code to await an async method and get its result.

C Sharp (C#)
int result = await GetNumberAsync();
Console.WriteLine([1]);
Drag options to blanks, or click blank then click option'
Aawait
BGetNumberAsync()
Cresult
DTask<int>
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to print the method call instead of the awaited result.
Using await inside Console.WriteLine without storing the result.
3fill in blank
hard

Fix the error in the async method to correctly return a string value.

C Sharp (C#)
public async Task<string> GetMessageAsync() {
    [1] "Hello";
}
Drag options to blanks, or click blank then click option'
Areturn
Byield
Cawait
Dthrow
Attempts:
3 left
💡 Hint
Common Mistakes
Using await without returning a value.
Using yield in an async method.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters async results.

C Sharp (C#)
var results = new Dictionary<string, int> {
    {"score", [1],
    {"count", [2]
};
Drag options to blanks, or click blank then click option'
Aawait GetScoreAsync()
Bawait GetCountAsync()
CGetScoreAsync()
DGetCountAsync()
Attempts:
3 left
💡 Hint
Common Mistakes
Using async method calls without await, resulting in Task objects instead of values.
Mixing awaited and non-awaited calls.
5fill in blank
hard

Fill all three blanks to create an async method that returns a dictionary with awaited values.

C Sharp (C#)
public async Task<Dictionary<string, int>> GetDataAsync() {
    return new Dictionary<string, int> {
        {"a", [1],
        {"b", [2],
        {"c", [3]
    };
}
Drag options to blanks, or click blank then click option'
Aawait GetAAsync()
Bawait GetBAsync()
Cawait GetCAsync()
DGetAAsync()
Attempts:
3 left
💡 Hint
Common Mistakes
Returning Tasks instead of awaited results.
Forgetting to await one or more async calls.