📜  c# 异步构造函数 - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:44.778000             🧑  作者: Mango

代码示例1
public class ViewModel       
{       
    public ObservableCollection Data { get; set; }       

    //static async method that behave like a constructor       
    async public static Task BuildViewModelAsync()  
    {       
        ObservableCollection tmpData = await GetDataTask();  
        return new ViewModel(tmpData);
    }       

    // private constructor called by the async method
    private ViewModel(ObservableCollection Data)
    {
        this.Data = Data;   
    }
}