无法分配结构体中自动实现的属性
我有下一个代码:
struct T
{
public T(int u)
{
this.U = 10; //Errors are here
}
public int U { get; private set; }
}
C# 编译器在指定行中给出了一对错误: 1) 在控制返回给调用者之前,必须完全分配自动实现的属性“TestConsoleApp.Program.TU”的支持字段。考虑从构造函数初始值设定项调用默认构造函数。 之前,不能使用“this”对象。
2) 在将“this”对象的所有字段分配给“我做错了什么?” 帮助我理解。
I have a next code:
struct T
{
public T(int u)
{
this.U = 10; //Errors are here
}
public int U { get; private set; }
}
C# compiler give me a pair of errors in stated line:
1) Backing field for automatically implemented property 'TestConsoleApp.Program.T.U' must be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer.
2) The 'this' object cannot be used before all of its fields are assigned to
What I do wrong? Help me understand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自 C# 规范:
所以你需要这个:
From the C# Specification:
So you need this:
好吧,首先您要创建一个可变结构 - 这几乎总是一个非常糟糕的主意。可变结构有时会以您意想不到的方式运行。好吧,它只是私有可变的,但是您编写了代码来改变它的事实是一个坏兆头。
第二个错误的原因是,在分配所有字段之前,您无法使用该结构的任何属性或方法,因此您需要链接到隐式无参数构造函数:
编译器要求任何构造函数都明确分配所有字段(这就是为什么您之前收到 first 错误;编译器不“知道”该属性为字段分配了值) - 通过链接到
this()
,您要确保在到达构造函数时body 中,所有字段已经都已明确分配,您无需再担心。但是,除非您实际上想要允许突变,我建议您将其设为真正的只读属性:
现在更明显表明您不想对其进行突变甚至在结构本身内。
Well, for a start you're creating a mutable struct - that's almost always a really bad idea. Mutable structs can sometimes behave in ways you don't expect. Okay, it's only privately mutable, but the fact that you've written code to mutate it is a bad sign.
The reason for the second error is that you can't use any properties or methods of the struct until all fields have been assigned, so you need to chain to the implicit parameterless constructor:
The compiler requires that any constructor leaves all the fields definitely assigned (which is why you were getting the first error before; the compiler doesn't "know" that the property assigns the field a value) - by chaining to
this()
, you're making sure that by the time you get to your constructor body, all the fields are already definitely assigned, and you don't need to worry about it any more.However, unless you actually want to allow mutation, I suggest you just make it a genuinely read-only property:
Now it's more obvious that you don't want to mutate it even within the struct itself.
添加对默认构造函数的调用:
Add a call to the default constructor:
你必须在这里使用默认构造函数:
you have to use the default constructor here:
从 C# 6 开始,这不再是问题,并且可以正确编译。请查看此处
From C# 6 this is not an issue anymore and it compiles correctly. Look here