我有一个看起来像这样的类:
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
}
}
现在在外面,我可以像这样访问有效的应用程序类型:
Constants.Instance.ValidApplicationTypes
将一堆字符串常量添加到此类的最佳方法是什么?我应该像这样添加它们:
private static readonly string message= "SomeMessage";
public static string Message
{
get { return message; }
}
并像这样访问它们:Constants.Message
或者我应该像这样添加它们:
private string message= "SomeMessage";
public string Message
{
get { return message; }
}
并像这样访问它们:Constants.Instance.Message
这两种在单例内部创建它们并从外部访问它们的方法之间有什么区别吗?