static variables in asp.net/C#

后端 未结 5 915
夕颜
夕颜 2021-01-02 03:24

I\'m using extensively static variables in my web application project. Now I have read from some articles that it is a global variable for the whole project and the data tha

5条回答
  •  旧巷少年郎
    2021-01-02 04:04

    I believe your interpretation of static is wrong.

    Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object.

    In other words, there is only one instance of this member for all the specific instances of the class.

    There isn't anything wrong with static variables as long as you use them correctly. I believe you are mixing static with global variables. Global variables can be accessed from everywhere. This isn't desireable since knowing when and where the state of that variable is set is complex. Furthermore this makes unit testing more difficult.


    This Programmers.SE question probably interests you.

提交回复
热议问题