How to set autoproperty in structure constructor?

Why is this permissible

public struct MyStruct
{
    public MyStruct(double value)
    {
        myField = value;
    }

    private double myField;

    public double MyProperty
    {
        get
        {
            return myField;
        }
        set
        {
            myField = value;
        }
    }
}

and it is not

public struct MyStruct
{
    public MyStruct(double value)
    {
        MyProperty = value;
    }
    public double MyProperty
    { 
        get; 
        set;
    }
}
+3
source share
3 answers

You need this syntax:

public struct MyStruct 
{
    public MyStruct(double value) : this()
    {
        MyProperty = value;
    }

    public double MyProperty { get; set; }
}

I got this information from after the SO post.

+3
source

Can you change your constructor to this:

public MyStruct(double value)  : this()
{
    myField = value;
}

, , . , , , . , ( ).

, .

+3

get, - . . : { ; ; }

+1

Source: https://habr.com/ru/post/1724251/


All Articles