The difference between C # static and non-static variables

Source: Internet
Author: User

The type descriptor for a static variable is static. Static variables, of course, are static storage, but the amount of static storage is not necessarily static variables, such as external variables are static storage, but not necessarily static variables, must be defined by static to be static external variables, or static global variables.

Detailed

A static resource is called by the static constructor to create the class type object (statically belonging to the class, dynamically to the object, statically by the static constructor, dynamically created by the normal construct, so the instance object is statically not reconstructed),

Second, the life cycle of the type object is the lifetime of the entire application domain, it is said that the static resources are accessed, only the application domain where it is unloaded will be unloaded, static variables always exist, that is, its lifetime is the entire source program.

Third, when the page is accessed for the first time, the equivalent of the page is constructed, static variables are initialized by the static constructor, and then access the page again, although the non-static resources are re-constructed, but at this time the static resources are not reconstructed and persisted (can also be used to do page state retention, but not recommended, Because the resource is always occupied, not as the session is periodically recycled) until the application domain is recycled. (The constructor class is automatically assigned a 0 value by the system if it is not assigned a local value.) )

Four, if it is application level, with static variable instead of application object is possible, the advantage is fast (storage area is different, and do not have to box unboxing), The disadvantage is that no application objects are common (application objects can store any object arbitrarily)

Five, is the page static variable page is initialized the first time, in the application domain has been saved, dynamic variables only in one HTTP request function (each HTTP request equivalent to the instance of the page this class)

Static variables allocate storage units within the static storage area. is not released during the entire run of the program. Variables belong to the dynamic storage category, which is stored in dynamic storage space (not static storage space), and is released after the function call ends.

Seven, problems associated with static constructs:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacestaticdemo{classProgram {Static voidMain (string[] args) {            //When you create an instance of a class that contains a static constructor, and when you access a static member that contains a static constructor, the static constructor is called first, regardless of how many instances of the class are created and how many times static members are called, and its static constructors are called only once. Statictest s =Newstatictest (); Statictest S2=Newstatictest (); //S.say ();//cannot use instance to reference to access static memberStatictest.say ();            Statictest.say ();            Console.WriteLine (Statictest.constdemo); //Static methods that call static classes, where static constructs are called only onceStatictest2.say ();            Statictest2.say ();                    Console.readkey (); }    }     Public classStatictest {//static construction methods cannot be modified by modifiers such as public, static constructs must have no parameters//Public Static statictest ()        StaticStatictest () {Console.WriteLine ("static construction methods for non-static classes are called only once"); }        //parameterless constructors can coexist with static constructors. Although the parameter list is the same, one belongs to the class and one belongs to the instance, so there is no conflict.          PublicStatictest () {Console.WriteLine ("Non- static construction methods for non-static classes can coexist"); }        //Although a field cannot be declared as static const, the behavior of a const field is inherently static. //such a field belongs to a class and does not belong to an instance of the class. Therefore, you can use classname.membername notation to access the Const field as you would with a static field         Public Const intConstdemo =1;  Public Static voidSay () {Console.WriteLine ("static methods for non-static classes"); }    }     Public Static classStaticTest2 {StaticStaticTest2 () {Console.WriteLine ("Static construction methods for static classes are called only once"); }          Public Static voidSay () {Console.WriteLine ("Static methods for static classes"); }    }}

1 Although a field cannot be declared as static const, the behavior of a const field is inherently static.

2 "Static construction method can not be modified by the modifier public, such as static construction must have no parameters

3 When you create a class instance that contains a static constructor, and when you access a static member that contains a static constructor, the static constructor is called first, regardless of how many instances of the class are created and how many times static members are called, and the static constructors are called only once.

4 "parameterless constructors can coexist with static constructors. Although the parameter list is the same, one belongs to the class and one belongs to the instance, so there is no conflict.

The difference between C # static and non-static variables

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.