Skip to content

Instantly share code, notes, and snippets.

@mariusGundersen
Last active October 21, 2018 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariusGundersen/f7d38a571b82a9a7df41dae07dbf1128 to your computer and use it in GitHub Desktop.
Save mariusGundersen/f7d38a571b82a9a7df41dae07dbf1128 to your computer and use it in GitHub Desktop.
Duct-typed extension methods Example 3
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