Can constructors be async?

后端 未结 12 1342
迷失自我
迷失自我 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:55

    I was just wondering why we can't call await from within a constructor directly.

    I believe the short answer is simply: Because the .Net team has not programmed this feature.

    I believe with the right syntax this could be implemented and shouldn't be too confusing or error prone. I think Stephen Cleary's blog post and several other answers here have implicitly pointed out that there is no fundamental reason against it, and more than that - solved that lack with workarounds. The existence of these relatively simple workarounds is probably one of the reasons why this feature has not (yet) been implemented.

提交回复
热议问题