How can a readonly static field be null?

后端 未结 5 815
误落风尘
误落风尘 2020-12-30 21:37

So here\'s an excerpt from one of my classes:

    [ThreadStatic]
    readonly static private AccountManager _instance = new AccountManager();

    private Ac         


        
5条回答
  •  生来不讨喜
    2020-12-30 22:12

    Quoting MSDN ThreadStaticAttribute:

    Do not specify initial values for fields marked with ThreadStaticAttribute, because such initialization occurs only once, when the class constructor executes, and therefore affects only one thread. If you do not specify an initial value, you can rely on the field being initialized to its default value if it is a value type, or to a null reference (Nothing in Visual Basic) if it is a reference type.

提交回复
热议问题