简体   繁体   English

Const vs protected static readonly

[英]Const vs protected static readonly

I've got a wrapper class which encapsulates a piece of information that needs to be transmitted as a byte array. 我有一个包装类,它封装了一段需要作为字节数组传输的信息。

In that way, the class encapsulates the necessary header (with fields like DATA_LENGTH or MESSAGE_TYPE) into the corresponding byte positions. 这样,该类将必要的头部(包括DATA_LENGTH或MESSAGE_TYPE等字段)封装到相应的字节位置。 For that I want to define positions and length in constants, for example: 为此,我想在常量中定义位置和长度,例如:

HEADER_DATA_LENGTH_IX = 0;
HEADER_DATA_LENGTH_LENGTH = 2;

which means DATA_LENGTH starts at 0 and takes two bytes. 这意味着DATA_LENGTH从0开始并占用两个字节。

but so far I'm struggling with making them constants or static readonly fields. 但到目前为止,我正在努力使它们成为常量或静态只读字段。 Const cannot be protected, therefore I won't be able to derive a new class and change the constants if a use them, on the other way I might declare new constants in the derived class and the use them. const不能被保护,因此我将无法派生新类并在使用它们时更改常量,另一方面我可能在派生类中声明新常量并使用它们。

What will be your approach? 你的方法是什么?

If you want to change the value of these params in a derived class, you can make them readonly and change them in the constructor of the derived class 如果要在派生类中更改这些参数的值,可以使它们只读 ,并在派生类的构造函数中更改它们

I wouldn't make them const anyhow, because they're not... 不管怎么说,我不会让他们成为const ,因为他们不是......

The basic difference is when the variable is initialized. 基本区别在于初始化变量。 'readonly' is set at initialization, or in the contructor, while 'const' is set at compile time. 'readonly'在初始化时或在构造函数中设置,而'const'在编译时设置。

I think the big decision is if you want to inherit the class and override the value. 我认为最大的决定是你想继承这个类并覆盖它。 If you do, go readonly. 如果你这样做,请继续阅读。 Otherwise I don't think it really matters. 否则我认为这不重要。

readonly C#ref: http://msdn.microsoft.com/en-us/library/acdd6hb7.aspx readonly C#ref: http//msdn.microsoft.com/en-us/library/acdd6hb7.aspx

const C# ref: http://msdn.microsoft.com/en-us/library/e6w8fe1b.aspx const C#ref: http//msdn.microsoft.com/en-us/library/e6w8fe1b.aspx

这个确切的问题由msdn上的官方c#faq回答

I wouldn't make these constant because they simply aren't constants. 我不会让它们保持不变,因为它们根本不是常量。 When declaring something as const you should ask yourself: can this change? 当宣布某事为const时,你应该问自己:这可以改变吗? Your message lengths might change one day, so they are better to be made readonly. 您的消息长度可能会在某一天发生变化,因此最好只读取它们。

Create an inner class with the constants. 使用常量创建内部类。 The deriving classes can then later override the inner class and change the constants as necessary. 然后,派生类可以覆盖内部类,并根据需要更改常量。

eg base class: 例如基类:

public class Stuff
{
  public class HeaderInformation
  {
    public const int HEADER_DATA_LENGTH_IX = 0;
    public const int  HEADER_DATA_LENGTH_LENGTH = 2;
  }
}

Then the derived class can do this: 然后派生类可以这样做:

public class DerivedStuff : Stuff
{
  public new class HeaderInformation : Stuff.HeaderInformation
  {
    public new const int HEADER_DATA_LENGTH_IX = 10;
  }
}

This way, you have flexibility. 这样,您就具有灵活性。 In DerivedStuff , the HeaderInformation class has all of the constants in the base Stuff.HeaderInformation class, but can change any of them, or keep the ones it has. DerivedStuff ,HeaderInformation类包含基本Stuff.HeaderInformation类中的所有常量,但可以更改它们中的任何一个,或者保留它所具有的常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM