Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Local static variables in VB.NET

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the example analysis of local static variables in VB.NET. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

VB supports the declaration of local variables with the Static keyword, so that the value of the variable can be maintained at the end of the procedure:

Public Sub Test1 () Static I

As Integer i + = 1

'implement a procedure call counter End Sub

We have implemented a simple process counter. Each time Test is called, the value of the counter increases by 1. In fact, there are many cases where we want to keep the value of the variable. However, the static of C # cannot be used inside the process. So to implement the process counter, we must declare a class-level variable. This is obviously not as good as VB. Because there is no way to prevent other procedures from modifying counter variables. This is the same as object encapsulation, which is supposed to be a local variable of a method, but now I am forced to separate it, which is obviously a bad design. So how does VB generate local static variables? By reassembling the above code, we can clearly see that in the CIL generated by VB, I appears not as a local variable, but as the Field of the class:

.field private specialname

Int32 $STATIC$Test1 $2001$ I

That is, I is renamed as a field of a class, but preceded by specialname. It is impossible to try to access $STATIC$Test1 $2001$ I in the code because it is not a valid identifier. But in IL, the code that adds this variable to one is exactly the same as a normal class field, which is loaded through ldfld. I think this approach is very clever, turning a static variable into a lifecycle class field, but the compiler controls access and makes it a local variable. It also explains why VB uses two different keywords to declare static variables-- Static and Shared.

Because the essence of a VB.NET local static variable is a field of a class, it is different from the real local variable. For example, in the case of multithreading, the access to VB.NET local static variables is the same as the access field.

This is the end of this article on "sample analysis of VB.NET local static variables". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report