Calling multiple async operations in a C# class initializer

38 views Asked by At

Given a C# class like this

public class MyData
{
    public int Value1 { get; set; }
    public int Value2 { get; set; }
}

Could it ever create a concurrency problem to instantiate and initialize this class like so:

var myInstance = new MyData
{
    Value1 = await MyFirstAsync(),
    Value2 = await MySecondAsync()
};

Assuming for example that MySecondAsync() relies on the same non-thread safe resources MyFirstAsync() does, or any other potential issue that could cause a pattern like this to be something to avoid?

0

There are 0 answers