结构体构造函数出现问题(编译器大喊我没有完全初始化结构体的所有自动属性)
我有以下代码:
public struct Interval
{
public double Min { get; set; }
public double Max { get; set; }
public Interval(double min = double.MinValue, double max = double.MaxValue)
{
Min = min;
Max = max;
}
}
编译器抱怨说
自动支持字段 已实施的财产必须完全 在控制权返回之前分配 来电者。考虑致电 来自构造函数的默认构造函数 初始化器。
这是我不明白的事情,因为我的构造函数正在完全初始化该结构的值。不是吗?
I have the following bit of code:
public struct Interval
{
public double Min { get; set; }
public double Max { get; set; }
public Interval(double min = double.MinValue, double max = double.MaxValue)
{
Min = min;
Max = max;
}
}
The compiler is complaining that
Backing field for automatically
implemented property must be fully
assigned before control is returned to
the caller. Consider calling the
default constructor from a constructor
initializer.
Which is something that I don't understand, since my constructor is fully initializing the values of this struct. Isn't it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的构造函数正在尝试设置属性 - 在它知道所有字段已初始化之前它无法执行此操作。 (在明确分配结构体的所有字段之前,您无法调用任何实例方法或访问任何属性。)当您使用自动实现的属性时,这是一个表现出来的怪癖:除了通过之外,您无法访问其他字段属性,但在为字段赋值之前不能使用该属性!解决方法很简单 - 只需添加对无参数构造函数的调用:
这是有效的,因为无参数构造函数将默认值分配给所有字段,之后您可以毫无问题地使用这些属性。
但是,我建议首先不要使用可变结构。
Your constructor is trying to set properties - which it can't do until it knows that all the fields have been initialized. (You can't call any instance methods or access any properties until all the struct's fields are definitely assigned.) It's a quirk which manifests itself when you're using automatically implemented properties: you have fields that you can't access other than via the property, but you can't use the property before assigning the field a value! The fix is simple - just add a call to the parameterless constructor:
This works because the parameterless constructor assigns the default values to all fields, after which you can use the properties without any problems.
However, I'd recommend against using mutable structs in the first place.
注意错误消息并添加对默认构造函数的调用,如下所示:
问题是,如所写,支持字段未初始化;这让编译器非常不高兴。但是,默认的无参数构造函数将为您初始化这些字段,这就是为什么当我们链接调用该构造函数时问题就消失了。
Heed the error message and add a call to the default constructor like so:
The issue is that as written the backing fields aren't initialized; that makes the compiler very unhappy. However, the default parameterless constructor will initialize these fields for you which is why the problem disappears when we chain a call to that constructor.
将 :this() 添加到 ctor 中:
Add :this() to the ctor:
我前几天刚写过这篇文章。
http://www.abhisheksur.com/2010 /10/hidden-facts-on-c-constructor-in.html
如果您为结构编写构造函数,则需要在从构造函数返回之前初始化结构的每个成员。支持字段可能会给您的代码带来一些问题,您可以使用对默认构造函数的调用来修复它。
这()
I just wrote this few days back.
http://www.abhisheksur.com/2010/10/hidden-facts-on-c-constructor-in.html
If you write a constructor for your structure, you need to initialize each and every member of your struct before returning from the constructor. Backing fields might be creating some issue with your code, you can fix it using the call to Default constructor.
this()
它对我来说编译得很好: http://ideone.com/mgBpt
您使用的是哪个版本的 C# 和编译器?
It compiles just fine for me: http://ideone.com/mgBpt
What version of C# and compiler are you using?