C# 6 中的静态 属性
static property in c# 6
我正在编写一个小代码来更好地理解 property
和 static property
。喜欢这些:
class UserIdentity
{
public static IDictionary<string, DateTime> OnlineUsers { get; set; }
public UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}
或
class UserIdentity
{
public IDictionary<string, DateTime> OnlineUsers { get; }
public UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}
因为我把它改成了:
class UserIdentity
{
public static IDictionary<string, DateTime> OnlineUsers { get; }
public UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}
它给了我错误信息:
Property or indexer 'UserIdentity.OnlineUsers' cannot be assigned to -- it is read only
我知道 属性 OnlineUsers
是 read only
,但在 C# 6 中,我可以通过构造函数分配它。那么,我错过了什么?
您正在尝试在实例构造函数中分配给只读静态 属性。这将导致每次创建新实例时都会分配它,这意味着它不是只读的。您需要在静态构造函数中分配给它:
public static IDictionary<string, DateTime> OnlineUsers { get; }
static UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
或者您可以内联:
public static IDictionary<string, DateTime> OnlineUsers { get; } = new Dictionary<string, DateTime>();
首先,您的构造函数缺少括号 ()
。正确的构造函数如下所示:
public class UserIdentity {
public UserIdentity() {
...
}
}
针对您的问题:
只读属性只能在特定上下文的构造函数中赋值。 static
属性 未绑定到特定实例,而是绑定到 class。
在你的第二个代码片段中,OnlineUsers
是非静态的,因此它可以在新实例的构造函数中分配给它,而且只能在那里分配。
在您的第三个代码段中,OnlineUsers
是静态的。因此,它只能在静态初始化器中赋值。
class UserIdentity
{
public static IDictionary<string, DateTime> OnlineUsers { get; }
//This is a static initializer, which is called when the first reference to this class is made and can be used to initialize the statics of the class
static UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}
静态只读 属性 必须像这样在静态构造函数中分配:
public static class UserIdentity
{
public static IDictionary<string, DateTime> OnlineUsers { get; }
static UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}
我正在编写一个小代码来更好地理解 property
和 static property
。喜欢这些:
class UserIdentity
{
public static IDictionary<string, DateTime> OnlineUsers { get; set; }
public UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}
或
class UserIdentity
{
public IDictionary<string, DateTime> OnlineUsers { get; }
public UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}
因为我把它改成了:
class UserIdentity
{
public static IDictionary<string, DateTime> OnlineUsers { get; }
public UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}
它给了我错误信息:
Property or indexer 'UserIdentity.OnlineUsers' cannot be assigned to -- it is read only
我知道 属性 OnlineUsers
是 read only
,但在 C# 6 中,我可以通过构造函数分配它。那么,我错过了什么?
您正在尝试在实例构造函数中分配给只读静态 属性。这将导致每次创建新实例时都会分配它,这意味着它不是只读的。您需要在静态构造函数中分配给它:
public static IDictionary<string, DateTime> OnlineUsers { get; }
static UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
或者您可以内联:
public static IDictionary<string, DateTime> OnlineUsers { get; } = new Dictionary<string, DateTime>();
首先,您的构造函数缺少括号 ()
。正确的构造函数如下所示:
public class UserIdentity {
public UserIdentity() {
...
}
}
针对您的问题:
只读属性只能在特定上下文的构造函数中赋值。 static
属性 未绑定到特定实例,而是绑定到 class。
在你的第二个代码片段中,OnlineUsers
是非静态的,因此它可以在新实例的构造函数中分配给它,而且只能在那里分配。
在您的第三个代码段中,OnlineUsers
是静态的。因此,它只能在静态初始化器中赋值。
class UserIdentity
{
public static IDictionary<string, DateTime> OnlineUsers { get; }
//This is a static initializer, which is called when the first reference to this class is made and can be used to initialize the statics of the class
static UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}
静态只读 属性 必须像这样在静态构造函数中分配:
public static class UserIdentity
{
public static IDictionary<string, DateTime> OnlineUsers { get; }
static UserIdentity()
{
OnlineUsers = new Dictionary<string, DateTime>();
}
}