Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动属性和结构_C#_.net_Properties_Struct_Automatic Properties - Fatal编程技术网

C# 自动属性和结构

C# 自动属性和结构,c#,.net,properties,struct,automatic-properties,C#,.net,Properties,Struct,Automatic Properties,我想知道下面的C代码: 编译时未出现错误“在将其所有字段分配给之前无法使用“this”对象”。对于类似的类,它的编译没有任何问题 可以通过重构以下内容使其工作: struct Structure { private int _propertyA; private int _propertyB; public Structure(int a, int b) { _propertyA = a; _propertyB = b;

我想知道下面的C代码:

编译时未出现错误“在将其所有字段分配给之前无法使用“this”对象”。对于类似的类,它的编译没有任何问题

可以通过重构以下内容使其工作:

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; }
    }
}

但是,我认为在C#中引入自动属性的全部目的是为了避免以后编写代码。这是否意味着自动属性与结构无关

您需要首先调用默认构造函数,如下所示:

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

如您所见,当在构造函数中引用
PropertyA
时,您正在访问
this
对象,编译器将不允许该对象,因为您的字段尚未初始化

要解决这个问题,您需要找到一种初始化字段的方法。一种方法是您的示例:如果您不使用自动属性,那么字段是显式的,您可以初始化它们

另一种方法是让构造函数调用另一个初始化字段的构造函数。结构始终隐式具有无参数构造函数,该构造函数将其字段初始化为零,因此请使用:

public Structure(int a, int b)
    : this()
{
    PropertyA = a;
    PropertyB = b;
}
在C#6中,这就消失了;问题中的代码编译得很好


虽然Stefan已经找到了答案,但我必须建议你不要使用可变结构,因为它会咬你。可变结构是邪恶的

在国际海事组织,这里的“正确”解决方案很简单:

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; } }
}

您使用的是什么版本的.Net?每次您编写可变结构时,都会有人cries@TheGuyOfDoom-没关系。。。在任何情况下,行为都是相同的;这是一个C#3.0编译器特性,“修复”是根据Stefan可能重复的注释,这在C#6I中不再是问题,我只会使用
PropertyA{get;private set;}
。Readonly给您带来的好处很少——避免多次设置属性几乎总是微不足道的。如果您不使用
只读
,您仍然在使用自动实现的属性,因此问题仍然存在。@Joren-它表达了我们的意图;)并且避免了必须调用
:this()
@Joren的麻烦,对于那些将来可能会偶然发现这一点的人:如果您试图从构造函数中设置属性,您的建议不会编译。@Eric实际上在C#6中是这样的;您不需要
:this()
任何more@Eric-它编译得很好<代码>结构Foo{Foo(intx):this(){x=x;}public intx{get;private set;}注意,在C#6中不再是这种情况
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; } }
}