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

Returning values from async methods in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Return Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of async method returning Task
What is the output of this C# program when run?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program {
    static async Task<int> GetNumberAsync() {
        await Task.Delay(100);
        return 42;
    }

    static async Task Main() {
        int result = await GetNumberAsync();
        Console.WriteLine(result);
    }
}
A42
BTask`1
C0
DCompilation error
Attempts:
2 left
💡 Hint
Remember that awaiting an async method unwraps the Task and gives the actual return value.
Predict Output
intermediate
2:00remaining
Return type of async method without Task
What happens when you try to compile and run this code?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program {
    static async Task<int> GetNumberAsync() {
        await Task.Delay(100);
        return 10;
    }

    static async Task Main() {
        int result = await GetNumberAsync();
        Console.WriteLine(result);
    }
}
ACompilation error
BRuntime exception
CPrints 10
DPrints Task`1
Attempts:
2 left
💡 Hint
Async methods must return Task, Task, or void (rarely).
🔧 Debug
advanced
2:00remaining
Why does this async method return null?
Consider this code snippet. Why does the output print "Result is: " with no number after it?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program {
    static async Task<string> GetTextAsync() {
        await Task.Delay(50);
        string text = null;
        return text;
    }

    static async Task Main() {
        string result = await GetTextAsync();
        Console.WriteLine($"Result is: {result}");
    }
}
AThe method throws an exception causing no output.
BThe await is missing so result is null.
CThe method returns null string, so nothing is printed after the colon.
DThe Console.WriteLine is incorrect and does not print the variable.
Attempts:
2 left
💡 Hint
Check what value the async method returns and how null strings print.
Predict Output
advanced
2:00remaining
Output of async method returning Task without await
What is the output of this program?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program {
    static async Task DoWorkAsync() {
        Console.WriteLine("Start");
        // No await here
        Console.WriteLine("End");
    }

    static async Task Main() {
        await DoWorkAsync();
    }
}
ACompilation error
BStart\nEnd
CEnd
DStart
Attempts:
2 left
💡 Hint
Async methods can run synchronously if no await is present.
🧠 Conceptual
expert
2:00remaining
What is the type of the value returned by an async method with signature 'async Task'?
Given an async method declared as 'async Task ComputeAsync()', what is the type of the value you get when you await this method?
Avoid
BTask<int>
CTask
Dint
Attempts:
2 left
💡 Hint
Await unwraps the Task and gives the inner value.