你的代码等同于以下代码:
public struct DealImportRequest
{
private DealRequestBase _dr;
private int _irc;
public DealRequestBase DealReq
{
get { return _dr; }
set { _dr = value; }
}
public int ImportRetryCounter
{
get { return _irc; }
set { _irc = value; }
}
/* Note we aren't allowed to do this explicitly - this is didactic code only and isn't allowed for real*/
public DealImportRequest()
{
this._dr = default(DealRequestBase); // i.e. null or default depending on whether this is reference or value type.
this._irc = default(int); // i.e. 0
}
public DealImportRequest(DealRequestBase drb)
{
this.DealReq = drb;
this.ImportRetryCounter = 0;
}
}
现在,所有我在这里所做的是删除语法糖是:
- 实现自动属性。
- 计算出哪些成员处理相对于
this
。
- 提供所有
struct
s默认无参数构造函数。
前两个是可选的(你可以明确他们写,如果你愿意),但第三个是不是 - 我们不允许我们自己写的代码为struct
的参数的构造函数,我们一起去一个像上面代码中那样工作的工具会自动提供给我们。
现在,看看这里,突然两个错误的含义变得清晰了 - 您的构造函数在分配字段(错误188)之前隐式使用this
,而那些字段是支持自动属性(错误843)的字段。
这是不同自动功能的组合,通常我们不必考虑,但在这种情况下效果不佳。我们可以通过下面的在843错误信息的建议,并调用默认的构造函数的显式构造的一部分,解决这个问题:
public DealImportRequest(DealRequestBase drb)
:this()
{
DealReq = drb;
ImportRetryCounter = 0;
}
关于我的扩展上面代码的版本考虑到这一点,你可以看到这解决了这个问题,因为它会在继续进行之前调用分配给后台字段的构造函数。
可能的重复http://stackoverflow.com/questions/2534960/c-struct-constructor-fields-must-be-fully-assigned-before-control-is-returned – Hps 2010-12-02 14:02:59
@HPS,我不同意。虽然它涉及到与该问题相同的问题,但它与隐式字段相比(支持自动属性)而不是明确字段的事实足以阻止某人看到这两个问题相关的原因。这应该足以认为他们不是重复IMO。 – 2010-12-02 14:29:55