When is a static variable created?

前端 未结 3 1113
耶瑟儿~
耶瑟儿~ 2020-12-19 11:02

Let\'s say I have a static variable in a function:

Private Sub SomeFunction()
    Static staticVar As String = _myField.Value
End Sub

When, e

相关标签:
3条回答
  • 2020-12-19 11:38

    The CLR has no support for this construct so the VB.NET compiler emulates it.

    Creation first. The variable is lifted to a private field of the class with an unspeakable name that ensures no name collisions can occur. It will be an instance field if the variable is inside an instance method of the class. So will be created when an object of the class is created with the new operator. It will be a Shared field if the method is Shared or is part of a Module. So will be created in the loader heap by the jitter.

    Assignment next, the much more involved operation. The language rule is that assignment occurs when code execution lands on the Dim statement for the first time. The term first time is a loaded one. There's an enormous amount of code generated by the compiler inside the method to ensure this is guaranteed. The kind of problems that need to be addressed are threading, recursion and exceptions.

    The compiler creates another hidden helper field of type StaticLocalInitFlag at the same scope as the hidden variable field to keep track of initialization state for the variable. The first part of the injected code is a call to Monitor.Enter() to deal with threading. Same thing as SyncLock. The StaticLocalInitFlag serves as the locking object, note how it is a class and not just a Boolean.

    Next, Try and Finally statements guard against exceptions. Inside the Try statement, the value of StaticLocalInitFlag.State is checked for 0. This protects against recursion on the same thread. State is then set to 2 to indicate that initialization code is running. Followed by the assignment. Next, State is checked again to see if it is still 2. If it is not then something went drastically wrong and an IncompleteInitialization exception is thrown.

    The Finally block then sets the State to 1 to indicate that the variable is initialized. Followed by a call to Monitor.Leave().

    Lots of code, 96 bytes of IL for just a simple variable. Use Static only if you don't worry about the cost.

    0 讨论(0)
  • 2020-12-19 11:43

    A Static variable is instantiated when it is assigned, obviously.

    When that line is run, not before, not after.

    Put a breakpoint on it and you'll see.

    Static just means that the value will persist between calls.

    Shared class/module members are instantiated the first time the class is accessed, before any Shared constructor and before the call, whether that is to the class constructor or some Shared method/property. I think this may be the origin of your confusion. Static local variables, like local statics in c# are not instantiated in this way.


    Interesting information from Tim Schmelter, it appears the value is maintained internally using a hidden Shared class level variable that is instantiated like other Shared class level variables but always with the default value.

    The effect observed by you the developer is unchanged, apart from some instantiation delay when the class is accessed. This delay should be undetectable in practice.

    0 讨论(0)
  • 2020-12-19 11:56

    The VB.NET compiler creates a static (shared in VB.NET) class-level variable to maintain the value of "staticVar". So it's initialized like any other static/shared variable, on the first use of a static field of that class (or when you call this method).

    http://weblogs.asp.net/psteele/pages/7717.aspx

    0 讨论(0)
提交回复
热议问题