What can I do with a protected/private static variable?

前端 未结 7 2283
悲&欢浪女
悲&欢浪女 2021-01-01 18:00

I see I can write :

protected static

in my C# class (in my case, an aspx.cs). As well as :

private static

7条回答
  •  情歌与酒
    2021-01-01 18:25

    The definition of static isn't "available everywhere". It is a variable shared across the type it is declared within in the scope of an AppDomain.

    Access Modifiers do not alter this definition, but obviously affect the scope of access.

    You are confusing the static modifier with access modifiers. A static variable still needs accessibility defined. In your example, private static variables are only accessible within the type it is defined in, protected would be accessible within the type and any derived types.

    Just a note, be aware that IIS (hosting ASP.NET applications) recycles worker processes, which will flush any static variable values that are alive at the time.

提交回复
热议问题