2

I have a class that looks something like this:

public class Constants
{
    private static readonly Lazy<Constants> lazy =
        new Lazy<Constants>(() => new Constants());

    public static Constants Instance { get { return lazy.Value; } }
    Dictionary<string, List<string>> validApplicationTypes;
    public Dictionary<string, List<string>> ValidApplicationTypes
    {
        get { return validApplicationTypes; }

    }
    private Constants()
    {
       // validApplicationTypes is populated from a DB table
    }
}

Now outside I access the valid application types like this:

Constants.Instance.ValidApplicationTypes

What would be the best way to add a bunch of string constants to this class? should I add them like:

private static readonly string message= "SomeMessage";
public static string Message
        {
            get { return message; }
        }

and access them like: Constants.Message

or should I add them like this:

   private string message= "SomeMessage";
    public string Message
            {
                get { return message; }
            }

and access them like: Constants.Instance.Message

Is there any difference between these 2 ways of creating them inside the singleton and accessing them from the outside?

DVM
  • 1,229
  • 3
  • 16
  • 22

2 Answers2

1

Is there any difference between these 2 ways of creating them inside the singleton and accessing them from the outside?

Yes.

The former implementation will created before any instance of Constants and will be accessible like this:

Constants.Message

The latter will not be created until Instance is initialized and will be accessible like this:

Constants.Instance.Message

Adding readonly to the field will not affect how it is accessed from "outside", but it will from the inside. It will only be able to set during initialization or in the constructor of the class in belongs to.

private static readonly string message= "SomeMessage";

or

private Constants()
{
   message = "SomeMessage";
}

This will not compile if readonly is applied:

private void SetMessage()
{
    message = "SomeMessage";
}

Error:

A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)

Khan
  • 17,904
  • 5
  • 47
  • 59
  • thanks, got that now. Is there any advantage in using one way over the other of accessing the constant strings? – DVM Oct 09 '13 at 13:25
  • 1
    It would be my personal preference to make it non-static because I would expect to see Message as part of the instance and not a static part of the class. Otherwise I can't think of one. – Khan Oct 09 '13 at 13:35
  • 1
    The first method allows you to access the constants without having to instantiate the class. This is because the second way they are not constants as such, just normal fields/properties on a class that happen to be always set by the same string literal. – Ben Robinson Oct 09 '13 at 13:36
0

Hmm, why not

public const string Message = "SomeMessage";

?

Pavel Tupitsyn
  • 8,393
  • 3
  • 22
  • 44
  • For 2 reasons: I'm come from a Java background and `public static readonly` seemed very similar to `public static final`. And the second one is this answer: http://stackoverflow.com/questions/755685/c-static-readonly-vs-const – DVM Oct 09 '13 at 13:19
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – brian d foy Oct 09 '13 at 13:38
  • 1
    @briandfoy, the question is "What would be the best way to add a bunch of string constants to this class?", and my answer provides a way that is the best (in my opinion, of course). – Pavel Tupitsyn Oct 09 '13 at 14:34