C # static and non-static

Source: Internet
Author: User

C # static member
1. Static members belong to the class and non-static members belong to the instance.
2. Each time a class instance is created, a new storage block is allocated to non-static members in the memory.
C # static method
1. C # static methods belong to all classes and can be used before class instantiation.
2. Non-static methods can be used to access any member of the primary class. Static methods can only be used to access static members of the primary class.
3. Static methods can be used before class instantiation. Non-static variables in the class must be instantiated before they can be used.
The static method in C # does not need to create a class instance first, but uses the class name. Static Method format.
1. The static method is a member method and belongs to the entire class, that is, it can be used without creating an object.
Only static variables and other static methods can appear in static, and keywords such as this... cannot be used in static.
2. The efficiency of static methods is higher than that of instances. The disadvantage of static methods is that they cannot be destroyed, while instantiation can be destroyed.
3. The static method and static variable always use one piece of memory after being created, and multiple memories can be created through instantiation.
4. There are two methods in C #: instance method and static method.
C # obtain the class name in the static method.
Static Method: String classname = system. reflection. methodbase. getcurrentmethod (). reflectedtypefullname;
Non-static method: String classname = This. GetType (). fullname;

Non-static is the lifecycle of variables automatically controlled by the program. Usually, variables are allocated when they enter the scope and are released when they leave the scope. Static means that variables are allocated during program initialization until the program exits. That is to say, static allocates and releases variables according to the life cycle of the program, rather than the life cycle of the variable itself.
Void func ()
{Int A; sataic int B ;}
Every call to this function variable A is new because it is allocated when it enters the function body and released when it exits the function body. So when multiple threads call this function, they will get their respective variable A, because it is always re-allocated. Regardless of whether you use this function or not, variable B is allocated during program initialization. Therefore, when multiple threads call this function, the same variable is always accessed.

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.