Skip to content

Instantly share code, notes, and snippets.

@programmation
Created May 4, 2015 07:04
Show Gist options
  • Save programmation/f7eefa6d9d774c4a8c76 to your computer and use it in GitHub Desktop.
Save programmation/f7eefa6d9d774c4a8c76 to your computer and use it in GitHub Desktop.
Async factory method
// http://blog.stephencleary.com/2013/01/async-oop-2-constructors.html
public sealed class MyClass
{
private MyData asyncData;
private MyClass() { ... }
private async Task<MyClass> InitializeAsync()
{
asyncData = await GetDataAsync();
return this;
}
public static Task<MyClass> CreateAsync()
{
var ret = new MyClass();
return ret.InitializeAsync();
}
}
public static async Task UseMyClassAsync()
{
MyClass instance = await MyClass.CreateAsync();
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment