Async Contructors #934
Unanswered
bilal-fazlani
asked this question in
General
Replies: 2 comments
-
Workaround 3: public class MyClass
{
private Task _initalization;
public MyClass()
{
_initalization = doSomeAsyncSetupWork();
}
public async Task WhenInitalizedAsync()
{
await _initalization;
}
}
class Program
{
public static async Task main(string[] args)
{
MyClass myClass = new MyClass();
await myClass.WhenInitalizedAsync();
}
} Workaround 4: public class MyClass
{
private Task _initalization;
private MyClass()
{
_initalization = doSomeAsyncSetupWork();
}
public async static Task<MyClass> CreateAsync()
{
MyClass myClass = MyClass();
await myClass._initalization;
return myClass;
}
}
class Program
{
public static async Task main(string[] args)
{
MyClass myClass = await MyClass.CreateAsync();
}
} Last one ensures initalization |
Beta Was this translation helpful? Give feedback.
0 replies
-
Duplicate of #419, though with different requested syntax. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am proposing something like this:
Without this feature, it would have to be something like this.
Workaround 1:
Or Workaround 2:
Both of these workarounds have some problem. The former obviously because of it's blocking behaviour.
The issue with latter is, I even though Setup() in a mandatory requirement for object to exist and function correctly, I still have to call it explicitly whenever I create instance of object, otherwise, the the object is incomplete and won't function and throw runtime exceptions.
Beta Was this translation helpful? Give feedback.
All reactions