C# 具有暴露属性的单态
我有一个类似这样的类:C# 具有暴露属性的单态,c#,c#-4.0,singleton,C#,C# 4.0,Singleton,我有一个类似这样的类: public class Constants { private static readonly Lazy<Constants> lazy = new Lazy<Constants>(() => new Constants()); public static Constants Instance { get { return lazy.Value; } } Dictionary<string,
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
}
}
private string message= "SomeMessage";
public string Message
{
get { return message; }
}
Constants.Message
Constants.Instance.Message
向该类添加一组字符串常量的最佳方法是什么?我是否应该像这样添加它们:
private static readonly string message= "SomeMessage";
public static string Message
{
get { return message; }
}
并像这样访问它们:Constants.Message
或者我应该这样添加它们:
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
}
}
private string message= "SomeMessage";
public string Message
{
get { return message; }
}
Constants.Message
Constants.Instance.Message
并像这样访问它们:Constants.Instance.Message
这两种在单例中创建它们和从外部访问它们的方法有什么区别吗?嗯,为什么没有
public const string Message = "SomeMessage";
?
这两种在内部创建它们的方法有什么区别吗
单身汉和从外部访问他们
对
前一个实现将在常量的任何实例之前创建,并且可以如下访问:
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
}
}
private string message= "SomeMessage";
public string Message
{
get { return message; }
}
Constants.Message
Constants.Instance.Message
只有初始化了实例
后,才会创建后者,并且可以如下访问:
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
}
}
private string message= "SomeMessage";
public string Message
{
get { return message; }
}
Constants.Message
Constants.Instance.Message
将readonly
添加到该字段不会影响从“外部”访问该字段的方式,但会从内部访问该字段。它只能在初始化期间或在所属类的构造函数中设置
private static readonly string message= "SomeMessage";
或
如果应用了readonly
,则不会编译:
private void SetMessage()
{
message = "SomeMessage";
}
错误:
无法将静态只读字段分配给(静态只读字段除外)
构造函数或变量初始值设定项)
仅仅因为它是静态的,并不能使它成为常数。它们可以是常量
还是静态只读
?你说得对,我错过了只读。Fixed nowUseconst
或static readonly
我认为没有理由让它们成为实例成员。原因有二:我来自Java背景,public static readonly
与public static final
非常相似。第二个答案是:这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论。@briandfoy,问题是“向该类添加一组字符串常量的最佳方法是什么?”,我的回答提供了一种最好的方法(当然,在我看来)。谢谢,现在知道了。使用一种方式访问常量字符串比另一种方式有什么优势吗?我个人倾向于将其设置为非静态的,因为我希望将消息视为实例的一部分,而不是类的静态部分。否则我想不出一个。第一个方法允许您访问常量,而无需实例化类。这是因为第二种方式,它们本身不是常量,只是类上的普通字段/属性,碰巧总是由相同的字符串文本设置。