static property in c# 6

前端 未结 3 1285
栀梦
栀梦 2021-01-01 15:19

I\'m writing a small code to more understand about property and static property. Like these:

class UserIdentity
{
    public static         


        
3条回答
  •  生来不讨喜
    2021-01-01 15:57

    You're trying to assign to a read only static property in an instance constructor. That would cause it to be assigned every time a new instance is created, which would mean it's not read only. You need to assign to it in the static constructor:

    public static IDictionary OnlineUsers { get; }
    
    static UserIdentity()
    {
        OnlineUsers = new Dictionary();
    }
    

    Or you can just do it inline:

    public static IDictionary OnlineUsers { get; } = new Dictionary();
    

提交回复
热议问题