Created
May 4, 2015 07:04
-
-
Save programmation/f7eefa6d9d774c4a8c76 to your computer and use it in GitHub Desktop.
Async factory method
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
// 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