Say I have a class that needs to perform some async initialization using an InitializeAsync() method. I want to make sure that the initialization is performed only once. If
Yes. Use Stephen Cleary's AsyncLazy (available on the AsyncEx nuget):
private static readonly AsyncLazy myResource = new AsyncLazy(
async () =>
{
var ret = new MyResource();
await ret.InitAsync();
return ret;
}
);
public async Task UseResource()
{
MyResource resource = await myResource;
// ...
}
Or the visual studio SDK's AsyncLazy if you prefer a Microsoft implementation.