Last active
October 21, 2018 11:12
-
-
Save mariusGundersen/f7d38a571b82a9a7df41dae07dbf1128 to your computer and use it in GitHub Desktop.
Duct-typed extension methods Example 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.CompilerServices; | |
using System.Threading.Tasks; | |
// Example 3: await Lazy<Task<T>> | |
// Just a dummy async function | |
public static Task<string> GetSomethingAsync(int number) => Task.FromResult($"Something {number}"); | |
// We need a lazy variable to work with | |
var lazySomething = new Lazy<Task<string>>(() => GetSomethingAsync(10)); | |
// This extension method lets us await a Lazy<Task<T>> directly | |
public static TaskAwaiter<T> GetAwaiter<T>(this Lazy<Task<T>> lazyTask) | |
=> lazyTask.Value.GetAwaiter(); | |
// This line will compile now | |
Console.WriteLine(await lazySomething); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment