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

Async and await keywords 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 asynchronous method.

C Sharp (C#)
public [1] Task<int> GetNumberAsync() {
    return Task.FromResult(5);
}
Drag options to blanks, or click blank then click option'
Aasync
Bawait
Cstatic
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'await' instead of 'async' in the method declaration.
Using 'void' as return type for async methods that return a value.
2fill in blank
medium

Complete the code to wait for the asynchronous method to finish and get the result.

C Sharp (C#)
int result = [1] GetNumberAsync();
Drag options to blanks, or click blank then click option'
ATask
Basync
Cawait
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'async' instead of 'await' when calling the method.
Not using any keyword and trying to assign the Task directly to an int.
3fill in blank
hard

Fix the error in the asynchronous method by completing the code.

C Sharp (C#)
public async Task<string> GetMessageAsync() {
    [1] Task.Delay(1000);
    return "Done";
}
Drag options to blanks, or click blank then click option'
Aasync
Bawait
Creturn
Dyield
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use 'await' before Task.Delay.
Using 'async' inside the method body instead of in the method signature.
4fill in blank
hard

Fill both blanks to create an asynchronous method that returns the length of a string after a delay.

C Sharp (C#)
public [1] Task<int> GetLengthAsync(string text) {
    [2] Task.Delay(500);
    return Task.FromResult(text.Length);
}
Drag options to blanks, or click blank then click option'
Aasync
Bawait
Cstatic
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Not marking the method as async.
Not awaiting the Task.Delay call.
5fill in blank
hard

Fill all three blanks to create an async method that awaits a delay, then returns the uppercase version of a string.

C Sharp (C#)
public [1] Task<string> ConvertToUpperAsync(string input) {
    [2] Task.Delay(700);
    return Task.FromResult(input[3]);
}
Drag options to blanks, or click blank then click option'
Aasync
Bawait
C.ToUpper()
D.ToLower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using .ToLower() instead of .ToUpper().
Forgetting to await the Task.Delay.
Not marking the method as async.