C#中的静态常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/842590/
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
Static Constants in C#
提问by anonymous coward
I have this code;
我有这个代码;
using System;
namespace Rapido
{
class Constants
{
public static const string FrameworkName = "Rapido Framework";
}
}
Visual Studio tells me: The constant 'Rapido.Constants.FrameworkName' cannot be marked static
Visual Studio 告诉我: The constant 'Rapido.Constants.FrameworkName' cannot be marked static
How can I make this constant available from other classes without having to create a new instance of it? (ie. directly accessing it via Rapido.Constants.FrameworkName
)
如何在不必创建它的新实例的情况下使其他类可以使用此常量?(即通过直接访问它Rapido.Constants.FrameworkName
)
采纳答案by Mitch Wheat
public static class Constants
{
public const string FrameworkName = "Rapido Framework";
}
回答by ggf31416
A const is already static as it cannot change between instances.
const 已经是静态的,因为它不能在实例之间改变。
回答by Andrew Kennan
You don't need to declare it as static - public const string is enough.
您不需要将其声明为静态 - 公共常量字符串就足够了。