Static variables in ASP. NET

Source: Internet
Author: User
Today, I saw the document prohibiting static variables on the page and the discussion about static variables in ASP. NET in my comments. I don't think it involves the essence of static-What is static.

Static in ASP. NET and C ++ is not much different. In essence, they are all existing address spaces during compilation. In other words, after you build the project, the DLL file of the project already has the space for this static variable (or class, that is to say, after the system loads the DLL during your runtime, the so-called instance of this variable/class already exists and is an applicationProgramPublic, so you can use it directly,At the same time, all pages use the same space..

You need a new instance to use a common class. The reason is that only declarations are not defined in the application. When new is used, a space is allocated to one of its instances in the heap.

In a simple experiment, you can easily understand the differences between them: create a simple project, add a class, add a variable to the class, compile and record the DLL file size; then convert the variable to static (of course, your class must also be static, think about why), compile and record the DLL file size, and compare the two DLL sizes, is it bigger than the previous one? :)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.