简体   繁体   English

字符串常量类应该是静态的吗?

[英]Should a string constants class be static?

I am working on a new project and I have noticed some code that I am not sure is true. 我正在开发一个新项目,我注意到一些代码,我不确定是否属实。 The names and values I am using to demonstrate the question are fake. 我用来证明问题的名称和值是假的。

public class MyConsts //Should it be static?
{
    public const string MyConst1 = "a";
    public const string MyConst2 = "b";
    public const string MyConst3 = "c";
    public const string MyConst4 = "d";
    ....
}

For my logic this class (that contains only consts values) should be static, so no option to initialize it, which has no sense, am I correct? 对于我的逻辑,这个类(只包含consts值)应该是静态的,所以没有选项来初始化它,这没有任何意义,我是否正确?

EDIT: I was writing the code blind so I have confused the order of string and const - and because it wasn't the target of my question I've fixed this. 编辑:我正在编写代码盲,所以我混淆了字符串和常量的顺序 - 因为它不是我的问题的目标,我已经修复了这个。

Yes, it makes sense for it to be static. 是的,它是静态的。 That signifies your intention , prevents clients from even declaring a variable of that type, etc. 这表示您的意图 ,阻止客户甚至声明该类型的变量等。

You'll need to move the const modifier before the type part though: 您需要在类型部分之前移动const修饰符:

public const string MyConst1 = "a";
...

If the values could ever change, consider using public static readonly fields instead of const though - otherwise the value will be baked into any code which refers to the constants, which means you need to rebuild any client code if the values change. 如果值可能会发生变化,请考虑使用public static readonly字段而不是const - 否则该值将被烘焙到任何引用常量的代码中,这意味着如果值发生更改,则需要重建任何客户端代码。

(Another option is to make the constants internal instead of public .) (另一个选择是使常量internal而不是public 。)

Yes the class should be static . 是的,班级应该是static But related, should those values actually be const ? 但相关,这些值实际上应该是const吗?

Anything declared const will be compiled into any referencing assemblies, so if this is a class library, say, and you put out a new version with changes to those const values, they won't be picked up by the assemblies that reference them. 声明为const任何内容都将被编译到任何引用程序集中,因此,如果这是一个类库,并且您推出了一个新版本,并对这些const值进行了更改,那么它们将不会被引用它们的程序集拾取。

In that particular case public static readonly string makes sense. 在那种特殊情况下, public static readonly string是有意义的。

However, if those are only visible within a single particular assembly (for instance a console application or WinForms app), then you should declare that class internal and the const s are fine as-is. 但是,如果这些只在单个特定程序集中可见(例如控制台应用程序或WinForms应用程序),那么您应该声明该类是internal并且const是原样的。

Yes, it should be static if the values never change. 是的,如果值永远不会改变,它应该是静态的。 As you say, it makes no sense to allow client code to instantiate it. 正如您所说,允许客户端代码实例化它是没有意义的。

如果它只包含const并且不包含任何其他内容,那么最好将它作为静态类来防止实例化并因此阻塞。

Really, it's up to you. 真的,这取决于你。 Static does come in handy for purely utility classes. 静态确实派上了纯粹的实用类。

Keep in mind that a static class is sealed; 请记住,静态类是密封的; it cannot be inherited from. 它不能继承自己。

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

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