编译错误.将属性与结构一起使用 [英] Compilation error. Using properties with struct

查看:35
本文介绍了编译错误.将属性与结构一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解释结构构造函数中的以下错误.如果我将结构更改为类错误消失了.

Please explain the following error on struct constructor. If i change struct to class the erros are gone.

public struct DealImportRequest
{
    public DealRequestBase DealReq { get; set; }
    public int ImportRetryCounter { get; set; }

    public DealImportRequest(DealRequestBase drb)
    {
        DealReq = drb;
        ImportRetryCounter = 0;
    }
}

  • 错误 CS0188:在将所有字段分配给this"对象之前,无法使用该对象
  • 错误 CS0843:自动实现的属性的支持字段'DealImportRequest.DealReq' 必须在控制返回给调用者之前完全分配.考虑从构造函数初始值设定项调用默认构造函数.
  • 推荐答案

    正如错误消息所建议的,您可以通过从构造函数初始值设定项调用默认构造函数来解决此问题.

    As the error message recommends, you can resolve this by calling the default constructor from a constructor initializer.

    public DealImportRequest(DealRequestBase drb) : this()
    {
       DealReq = drb;
       ImportRetryCounter = 0;
    }
    

    来自语言规范:

    10.7.3 自动实现的属性

    当一个属性是指定为自动实现的属性,隐藏的支持字段可自动用于属性和访问器是实现读取和写入那个后盾.[...] 因为支持字段不可访问,它可以只能通过属性访问器,即使在包含类型.[...] 这个限制也意味着确定结构类型的分配自动实现的属性只能使用标准来实现结构的构造函数,因为分配给财产本身要求结构是绝对的分配.这意味着用户定义构造函数必须调用默认值构造函数.

    When a property is specified as an automatically implemented property, a hidden backing field is automatically available for the property, and the accessors are implemented to read from and write to that backing field. [...] Because the backing field is inaccessible, it can be read and written only through the property accessors, even within the containing type. [...] This restriction also means that definite assignment of struct types with auto-implemented properties can only be achieved using the standard constructor of the struct, since assigning to the property itself requires the struct to be definitely assigned. This means that user-defined constructors must call the default constructor.

    当然,另一种(更详细的)替代方法是手动实现属性并在构造函数中自己设置支持字段.

    The other (more verbose) alternative, of course, is to manually implement the properties and set the backing fields yourself in the constructor.

    请注意,您拥有的结构是可变的.不推荐这样做.我建议您将类型设为类(您的编译问题应该立即消失)或使类型不可变.假设您提供的代码是整个 结构,实现此目的的最简单方法是将 setter 设为私有 (get; private set;).当然,您还应该确保之后不要向结构添加任何依赖私有访问来修改字段的变异方法.或者,您可以使用 readonly 支持字段支持属性并完全摆脱 setter.

    Do note that the struct you have there is mutable. This is not recommended. I suggest you either make the type a class (your compilation problems should go away immediately) or make the type immutable. The easiest way to accomplish this, assuming the code you have presented is the entire struct, would be to make the setters private (get; private set;). Of course, you should also make sure that you don't add any mutating methods to the struct afterwards that rely on private access to modify the fields. Alternatively, you could back the properties with readonly backing fields and get rid of the setters altogether.

    这篇关于编译错误.将属性与结构一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆