加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 此Struct类型的定义有什么问题

发布时间:2020-12-15 18:34:56 所属栏目:百科 来源:网络整理
导读:我已经定义了这样的结构: 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; } p
我已经定义了这样的结构:
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.”

解决方法

嗯,真的有两个问题:

1.您使用自动属性,但随后也提供字段,两者之间没有布线.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读