Why does setting the property in the constructor of a struct not work?

前端 未结 2 1856
清酒与你
清酒与你 2021-01-12 16:44

I have following code which is not allowed (error below), why?

    struct A
    {
        private int b;

        public A(int x)
        {
            B = x         


        
2条回答
  •  忘掉有多难
    2021-01-12 17:08

    Change your constructor to:

    public A(int x) :
        this()
    {
        B = x;
    }
    

    As to "why", refer to 11.3.8 Constructors and 5.3 Definite assignment.

提交回复
热议问题