Can constructors be async?

后端 未结 12 1135
迷失自我
迷失自我 2020-11-22 09:18

I have a project where I\'m trying to populate some data in a constructor:

public class ViewModel
{
    public ObservableCollection Data { get;          


        
12条回答
  •  死守一世寂寞
    2020-11-22 09:40

    if you make constructor asynchronous, after creating an object, you may fall into problems like null values instead of instance objects. For instance;

    MyClass instance = new MyClass();
    instance.Foo(); // null exception here
    

    That's why they don't allow this i guess.

提交回复
热议问题