在链接的构造函数中设置只读字段 [英] Setting read-only fields in a chained constructor

查看:38
本文介绍了在链接的构造函数中设置只读字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个在构造函数中设置的只读字段的类.我有一个派生类,希望在构造函数中将它们设置为 different 值;但是,尝试这样做会导致CS1091(无法将只读字段分配给错误(在构造函数或变量初始化程序中除外).

I have a class with two read-only fields that are being set in the constructor. I have a derived class that would like to set these to different values in the constructor; however, trying to do so results in a CS1091 (A readonly field cannot be assigned to (except in a constructor or a variable initializer) error.

我不明白为什么会这样-我 am 分配给构造函数中的字段.只是不是定义它们的类之一.

I don't see why this is - I am assigning to the fields in a constructor. Just not the one of the class where they're defined.

我是否缺少一些微妙的语法问题,或者这是不可能的?

Is there some subtle syntax issue that I'm missing, or is this just not possible?

(如果无法解决,有很多方法可以解决-可能是没有setter的受保护虚拟属性,以及私有只读后备字段;但是,从语法上讲,它不会那么干净,因此,如果可以的话,我想避免使用它们)

(There's ways around it if this isn't possible - probably a protected virtual property without a setter, and private readonly backing fields; syntactically it's not going to be as clean, however, so I wanted to avoid them if I can.)

public class BaseClass
{
    protected readonly ushort OffsetRoutine;
    protected readonly ushort OffsetString;

    public BaseClass()
    {
        this.OffsetRoutine = this.GetWord(Addresses.Header.OffsetRoutine);
        this.OffsetString = this.GetWord(Addresses.Header.OffsetString);
    }

    protected ushort GetWord(byte address)
    {
        // Chosen by fair dice roll on a d100.
        return 42;
    }
}

public class DerivedClass : BaseClass
{
    public DerivedClass()
        : base()
    {
        this.OffsetRoutine = 0;
        this.OffsetString = 0;
    }
}

推荐答案

其他答案未提供关键详细信息-仅在声明或构造函数中允许设置 readonly 字段相同班级的人.这只是 readonly const 之间的区别之一. const 修饰符不允许您在构造函数中设置字段-仅在声明中.因此,由于字段不在同一类中,因此根本不允许在派生类中设置 readonly .我认为您不需要解决方法,因为您似乎知道如果需要的话,并且还有其他答案可以讨论.从MSDN( http://msdn.microsoft.com/en-us/library/acdd6hb7.aspx ):

The other answers are not providing a key detail - setting a readonly field is allowed ONLY in the declaration OR a constructor of the same class. This is just one of the differences between readonly and const. The const modifier does not allow you to set the field in a constructor - only in the declaration. So setting the readonly in a derived class is simply not allowed since the field is not in the same class. I assume you don't need workarounds as you seemed to know how to go about it if necessary, and there are other answers discussing that. From MSDN (http://msdn.microsoft.com/en-us/library/acdd6hb7.aspx):

当字段声明包含只读修饰符时,声明引入的字段只能作为声明或在同一类的构造函数中.

When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

仅举几个例子:

public class A
{
    protected const string constString = "Test";          //Allowed
    protected readonly string readonlyString = "Test";    //Allowed

    public A(){
      constString = "Test";      //Not allowed
      readonlyString = "Test";   //Allowed
    } 
}

public class B: A
{
    public B(){
      constString = "Test";      //Not allowed
      readonlyString = "Test";   //Not allowed
    }
}

这篇关于在链接的构造函数中设置只读字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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