我已经定义了这样的结构:
- struct Test
- {
- private string assayName;
- public string AssayName { get; set; }
- private string oldUnitName;
- public string OldUnitName { get; set; }
- private string newUnitName;
- public string NewUnitName { get; set; }
- public Test(string name,string oldValue,string newValue)
- {
- assayName = name;
- oldUnitName = oldValue;
- newUnitName = newValue;
- }
- }
但它给了我以下错误:
“Error 13 Backing field for automatically implemented property
‘EnterResults.frmApplication.Test.NewUnitName’ must be fully assigned
before control is returned to the caller. Consider calling the default
constructor from a constructor initializer.”
解决方法
嗯,真的有两个问题:
2.当您使用自动属性时,因为这是一个结构,所以必须先对它们进行初始化.您可以通过调用默认构造函数来完成此操作.所以修改后的版本是:
- struct Test
- {
- public Test(string name,string newValue)
- : this()
- {
- AssayName = name;
- OldUnitName = oldValue;
- NewUnitName = newValue;
- }
- public string AssayName { get; private set; }
- public string OldUnitValue { get; private set; }
- public string NewUnitValue { get; private set; }
- }