[英]Is it possible to have a getter for a const?
Just curious, is there a way to have a getter for a constant variable? 只是好奇,有没有办法让恒定变量的吸气剂? I have a sort of internal version number to ensure that two versions of a library are still speaking the same language, but I'd like the programmer to be able to check what version they're using.
我有一种内部版本号,以确保两个版本的库仍然使用相同的语言,但我希望程序员能够检查他们正在使用的版本。 Right now I use:
现在我使用:
private const Int16 protocol_version = 1;
public Int16 ProtocolVersion { get { return protocol_version; } }
But I'd prefer to do it with just the const if there's a way. 但如果有办法,我宁愿只用const来做。
You could declare a property with only a get accessor (without even declaring the set accessor, not even private): 你可以声明一个只有一个get访问器的属性(甚至没有声明set访问器,甚至不是私有的):
private const Int16 protocol_version = 1;
public Int16 ProtocolVersion {
get { return protocol_version; }
}
This is not the same as defining a constant only: the constant would be resolved at compile time, so if you update the library without recompiling the dependent program, the program would still see the "old" value. 这与仅定义常量不同:常量将在编译时解析,因此如果更新库而不重新编译依赖程序,程序仍将看到“旧”值。 Consider this example:
考虑这个例子:
// The class library
using System;
namespace MyClassLibrary {
public class X {
public const Int16 protocol_version = 1;
public Int16 ProtocolVersion { get { return protocol_version; } }
}
}
// The program
using System;
using MyClassLibrary;
class Program {
static void Main(string[] args) {
X x = new X();
Console.WriteLine("Constant : {0}", X.protocol_version);
Console.WriteLine("Getter: {0}", x.ProtocolVersion);
}
}
Now, compile the first time and execute the program. 现在,第一次编译并执行程序。 You will see
你会看见
Constant : 1
Getter : 1
Then, modify protocol_version to 2, and recompile the class library only, without recompiling the program , then put the new class library in the program folder and execute it. 然后,将protocol_version修改为2,并仅重新编译类库,而不重新编译程序 ,然后将新类库放在程序文件夹中并执行它。 You will see:
你会看见:
Constant : 1
Getter : 2
The fact is that if it's just a constant, the value is replaced at compile time . 事实是,如果它只是一个常量,则在编译时替换该值。
I think that what you are actually looking for is a static readonly
variable: in that way, you will avoid the compile-time const replacement, and the variable will not be modifiable after initialization: 我认为您实际需要的是一个
static readonly
变量:这样,您将避免编译时const替换,并且该变量在初始化后将无法修改:
public static readonly Int16 protocol_version = 1;
You have to keep in mind the reason for the existance of getters/setters. 你必须记住存在吸气剂/制定者的原因。 It is to control access to an encapsulated variable, specifically to control how a variable is changed and who can change it.
它是控制对封装变量的访问,特别是控制变量的更改方式以及谁可以更改变量。 Since a const is set only once and remains read-only on runtime there is no reason to create a property for it.
由于const只设置一次并且在运行时保持只读,因此没有理由为它创建属性。 Setting the constant to public is completely acceptable since it is not a private variable that needs to be protected.
将常量设置为public是完全可以接受的,因为它不是需要保护的私有变量。
If you really... really want to make it a property then just define it as a readonly property, skip the setter entirely: 如果你真的...真的想让它成为一个属性,那么只需将它定义为只读属性,完全跳过setter:
public Int16 ProtocolVersion { get { return protocol_version; } }
But just so we are clear, I would say normally you would have public constants with the same coding style as properties: 但是我们很清楚,我会说通常你会使用与属性相同的编码风格的公共常量:
public const Int16 ProtocolVersion = 1
Just do: 做就是了:
public const Int16 protocol_version = 1;
This will provide a public getter as a const
cannot have a setter. 这将提供一个公共getter,因为
const
不能有setter。
常量不能被重新分配,因此它们被称为常量,因此只需将protocol_version public
private const Int16 protocol_version = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.