开发者

How can I declare a constant in a public static class?

I have this class:

public static class LinkExtensions
{

Within this class I have a lot of methods that use a constant. Can someone explain to me is it possible for me to开发者_Python百科 declare a constant at the class level that I can use in all these methods?


If you want it to be avaible only inside the class, make it private:

public static class LinkExtensions {

  private const string _linkName = "asdf";

  ...
}

If you want it to be available outside the class, make it public:

public static class LinkExtensions {

  public const string _linkName = "asdf";

  ...
}

(Note that a constant that is used from a different project will use the value of the constant, not read it from your project. If you change the constant but doesn't recompile the other project that uses it, it will still use the old value. You might consider making it a read-only property instead in that case.)


Does this not work?

public static class LinkExtensions
{
    const int foo = 5;
}


Just in there, static int MY_CONSTANT = 5;


public static class LinkExtensions
{
    const int your_const = 1
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜