Auto-properties and structs

南楼画角 提交于 2019-12-17 20:35:18

问题


I am wondering about the following C#-code:

struct Structure
{
    public Structure(int a, int b)
    {
        PropertyA = a;
        PropertyB = b;
    }
    public int PropertyA { get; set; }
    public int PropertyB { get; set; }
}

It is not compiling with an error "The 'this' object cannot be used before all of its fields are assigned to". For the analogous class it is compiling without any problems.

It can be made working by refactoring to the following:

struct Structure
{
    private int _propertyA;
    private int _propertyB;

    public Structure(int a, int b)
    {
        _propertyA = a;
        _propertyB = b;
    }

    public int PropertyA
    {
        get { return _propertyA; }
        set { _propertyA = value; }
    }

    public int PropertyB
    {
        get { return _propertyB; }
        set { _propertyB = value; }
    }
}

But, I though that the whole point of introducing auto-properties to the C# was to avoid writing later code. Does that mean that auto-properties are not relevant for the structs?


回答1:


In C# 6, this simply goes away; the code in the question compiles fine.


While Stefan has the answer than addresses the question, I have to advise you not to use a mutable struct - it will bite you. Mutable structs are evil.

IMO, the "correct" fix here is simply:

struct Structure
{
    public Structure(int a, int b)
    {
        propertyA = a;
        propertyB = b;
    }
    private readonly int propertyA, propertyB;
    public int PropertyA { get { return propertyA; } }
    public int PropertyB { get { return propertyB; } }
}



回答2:


You need to call the default constructor first, like so:

struct Structure
{
    public Structure(int a, int b) : this()
    {
        PropertyA = a;
        PropertyB = b;
    }
    public int PropertyA { get; set; }
    public int PropertyB { get; set; }
}



回答3:


As you've seen, when referring to PropertyA in your constructor, you're accessing the this object, which the compiler won't allow because your fields haven't been initialized yet.

To get around this, you need to find a way to initialize the fields. One way is your example: If you don't use auto-properties, then the fields are explicit and you can initialize them.

Another way is to have your constructor call another constructor that initializes the fields. Structs always implicitly have a parameterless constructor that initializes its fields to zero, so use that:

public Structure(int a, int b)
    : this()
{
    PropertyA = a;
    PropertyB = b;
}


来源:https://stackoverflow.com/questions/5029188/auto-properties-and-structs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!