C# 存储我的程序使用的一组常量的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1724025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the best way to store a group of constants that my program uses?
提问by Matthew
I have various constants that my program uses... string
's, int
's,double
's, etc... What is the best way to store them? I don't think I want an Enum
, because the data is not all the same type, and I want to manually set each value. Should I just store them all in an empty class? Or is there a better way?
我的程序使用了各种常量...... string
's, int
's, double
's 等......存储它们的最佳方法是什么?我不认为我想要一个Enum
,因为数据不是所有相同的类型,我想手动设置每个值。我应该将它们全部存储在一个空类中吗?或者,还有更好的方法?
采纳答案by Daniel A. White
You probably could have them in a static class, with static read-only properties.
您可能可以将它们放在静态类中,并具有静态只读属性。
public static class Routes
{
public static string SignUp => "signup";
}
回答by Chris Marisic
IMO using a class full of constants is fine for constants. If they will change semi-occasionally I recommend using AppSettings in your config and the ConfigurationManager class instead.
IMO 使用一个充满常量的类对常量来说很好。如果它们会偶尔更改,我建议在您的配置和 ConfigurationManager 类中使用 AppSettings。
When I have "constants" that are actually pulled in from AppSettings or similar I still will always have a "constants" class that wraps the reading from configuration manager. It's always more meaningful to have Constants.SomeModule.Setting
instead of having to resort directly to ConfigurationManager.AppSettings["SomeModule/Setting"]
on any place that wants to consume said setting value.
当我有从 AppSettings 或类似的实际拉入的“常量”时,我仍然会有一个“常量”类来包装来自配置管理器的读数。拥有Constants.SomeModule.Setting
而不是直接求助于ConfigurationManager.AppSettings["SomeModule/Setting"]
任何想要消耗所述设置值的地方总是更有意义。
Bonus points for this setup, since SomeModule
would likely be a nested class inside the Constants file, you could easily use Dependency Injection to inject either SomeModule
directly into classes that depend on it. You could also even extract an interface on top of SomeModule
and then create a depenedency to ISomeModuleConfiguration
in your consuming code, this would then allow you to decouple the dependency to the Constants files, and even potentially make testing easier, especially if these settings come from AppSettings and you change them using config transformations because the settings are environment specific.
此设置的加分项,因为它SomeModule
可能是 Constants 文件中的一个嵌套类,您可以轻松地使用依赖注入将其中任何一个SomeModule
直接注入到依赖它的类中。你甚至可以在上面提取一个接口SomeModule
,然后ISomeModuleConfiguration
在你的消费代码中创建一个依赖,这将允许你将依赖解耦到 Constants 文件,甚至可能使测试更容易,特别是如果这些设置来自 AppSettings 和您可以使用配置转换更改它们,因为设置是特定于环境的。
回答by Marcel Gosselin
What I like to do is the following (but make sure to read to the end to use the proper type of constants):
我喜欢做的是以下内容(但请务必阅读到最后以使用正确类型的常量):
internal static class ColumnKeys
{
internal const string Date = "Date";
internal const string Value = "Value";
...
}
Read thisto know why const
might not be what you want. Possible type of constantsare:
阅读本文以了解为什么const
可能不是您想要的。可能的常量类型是:
const
fields. Do not use across assemblies (public
orprotected
) if value mightchange in future because the value will be hardcoded at compile-time in those other assemblies. If you change the value, the old value will be used by the other assemblies until they are re-compiled.static readonly
fieldsstatic
property withoutset
const
领域。如果值将来可能会更改,请不要跨程序集 (public
或protected
)使用,因为该值将在编译时在其他程序集中进行硬编码。如果更改该值,则其他程序集将使用旧值,直到它们被重新编译。static readonly
领域static
没有财产set
回答by emptyset
An empty static class is appropriate. Consider using several classes, so that you end up with good groups of related constants, and not one giant Globals.cs file.
一个空的静态类是合适的。考虑使用多个类,以便最终得到一组良好的相关常量,而不是一个巨大的 Globals.cs 文件。
Additionally, for some int constants, consider the notation:
此外,对于某些 int 常量,请考虑以下符号:
[Flags]
enum Foo
{
}
As this allows for treating the values like flags.
因为这允许像 flags 一样处理值。
回答by bruno conde
Yes, a static class
for storing constants would be just fine, except for constants that are related to specific types.
是的,static class
用于存储常量的 a 就可以了,除了与特定类型相关的常量。
回答by 3Dave
Another vote for using web.config or app.config. The config files are a good place for constants like connection strings, etc. I prefer not to have to look at the source to view or modify these types of things. A static class which reads these constants from a .config file might be a good compromise, as it will let your application access these resources as though they were defined in code, but still give you the flexibility of having them in an easily viewable/editable space.
使用 web.config 或 app.config 的另一票。配置文件是保存连接字符串等常量的好地方。我不想查看源代码来查看或修改这些类型的东西。从 .config 文件中读取这些常量的静态类可能是一个很好的折衷方案,因为它可以让您的应用程序访问这些资源,就好像它们是在代码中定义的一样,但仍然可以让您灵活地将它们放在易于查看/可编辑的位置空间。
回答by Philip Wallace
This is the best way IMO. No need for properties, or readonly:
这是 IMO 的最佳方式。不需要属性,或只读:
public static class Constants
{
public const string SomeConstant = "Some value";
}
回答by Aaron
If these Constants are service references or switches that effect the application behavior I would set them up as Application user settings. That way if they need to be changed you do not have to recompile and you can still reference them through the static properties class.
如果这些常量是影响应用程序行为的服务引用或开关,我会将它们设置为应用程序用户设置。这样,如果需要更改它们,您不必重新编译,您仍然可以通过静态属性类引用它们。
Properties.Settings.Default.ServiceRef
回答by KAPIL SHARMA
I would suggest static class with static readonly. Please find the code snippet below:
我建议使用静态只读的静态类。请在下面找到代码片段:
public static class CachedKeysManager
{
public static readonly string DistributorList = "distributorList";
}