How best to implement publicly accessible constants in C#

Posted by DanM on Stack Overflow See other posts from Stack Overflow or by DanM
Published on 2010-05-22T17:56:54Z Indexed on 2010/05/22 18:00 UTC
Read the original article Hit count: 274

Filed under:
|

There seem to be three choices for implementing publicly accessible constants in C#. I'm curious if there are any good reason to choose one over the other or if it's just a matter of personal preference.

Choice 1 - private field plus property getter

private const string _someConstant = "string that will never change";

public string SomeConstant
{
    get { return _someConstant; }
}

Choice 2 - property getter only

public string SomeConstant
{
    get { return "string that will never change"; }
}

Choice 3 - public field only

public const string SomeConstant = "string that will never change";

Which do you recommend and why?

© Stack Overflow or respective owner

Related posts about c#

Related posts about constants