Static methods and variables in. Net)

Source: Internet
Author: User

C # static member and method learning Summary


data member:
data members can be divided into static variables and instance variables.
static member: static member variables are associated with classes, it can be used as a "common" variable (a common expression) in a class. It does not depend on the existence of a specific object. During access, it is accessed by adding a class name operator and a variable name.
instance Member: The instance member variable is associated with the object, and the access instance member variable depends on the existence of the instance.

function Member:
methods can be divided into static methods. For example,
static methods: static methods are methods that do not belong to specific objects, static methods can access static member variables. Static methods cannot directly access instance variables. In the case of instance function calls, instance variables can be passed as parameters to static methods. Static methods cannot directly call instance methods. They can be called indirectly. You must first create an instance of the class and then call static methods through this specific object.
instance method: the execution of an instance method is associated with a specific object. An object is required for execution. The instance method can directly access static variables and instance variables. The instance method can directly access the instance method and static method. The static method can be accessed by adding a variable name to the class name operator. When multiple instance objects exist, the memory does not contain copies of a specific instance method. Instead, all objects of the same class share a copy of each instance method (the instance method only occupies one set of space ).

static things are generated in the heap during construction and will not be released or changed before the end.
because a large amount of data access has been generated and is not released, instances are not created repeatedly in the stack, saving time.
repeated non-static call of methods may cause the used variables or methods to be recycled by the system and then created.

for access to large data volumes, use static methods to improve performance.

If a method runs well without an instance, it should have been static unless you intentionally write it as non-static, so this efficiency is nonsense.


usage of static methods:
1. This method is irrelevant to any instance.
2. This method is independent of a specific instance.
3. This method is an operator.
4. This method does not modify the instance.

however, except that the first and third are unchanged, the second and fourth are wise.

Static Methods

1. Static declared variables and methods will be placed in the heap area of the memory, and space will be allocated even if not called, rather than static space will be allocated only when used, release after use.
2. Static can be called without creating an instance.

 

When a class member is declared as static, it can be accessed before the class object is created and no object reference exists. Therefore, most static members are used for global purposes. You can declare both methods and variables as static. When loading a class, one static block after another is executed only once. That is to say, some common functions that are widely used in the system generally adopt static methods. Non-general functions can use non-static methods, because not many of them are used in the system, which is not as extensive as general functions. Instantiation is more suitable than static methods. It consumes less resources than static methods. Therefore, the static method is usually determined based on actual needs.

================================================== ====================

Written in C #ProgramOr write a program in an OO language. First, you should identify when static functions are used and when member functions are required. 

If the difference is not made, the meaning of OO is abandoned, just to slightly improve the performance, then I suggest you do not use C #, write in C, you will get more performance improvement, it is not an order of magnitude.

second, it improves the memory consumption and performance of static functions and static members, I will give you a general idea about the dangers.

For memory consumption, it generally seems that only static members will consume, but static functions will not. 
However, to call a static function of a class, if the static members of the class are not initialized, they need to be initialized. That is to say, the static function is called to initialize the static members, that is, the memory loss is caused by a subtle influence. 

For performance improvement, 
First, let's talk about the calling of static functions in the following form: 
Class Name. Method Name 
The Calling procedure is roughly as follows: 
1. Find the type table by Class Name 
2. Find the function pointer to be called through the type table. 

For member functions, the formula is as follows: 
Object Name. Method Name 
The Calling procedure is roughly as follows: 
1. Find the type table address through the Object Name 
2. Find the type table through the type table address 
3. Find the function pointer to be called through the type table. 

That is to say, calling a member function requires one more step than a static function. Here it can be said that the performance of the member function is inferior to that of the static function.

in the end, we talk about the dangers of misuse of static functions for performance purposes.

As I did in the previous example, instead of using static functions for static functions, the following forms are generated in large numbers: 
Public class uglyclass 

Public static void uglymethod (uglyclass data ); 

Before that, let's talk about the data that can be accessed by static functions. There are roughly two types of data: 
1. Static members; 
2. parameters; 

If you use static members to transmit data, you need to be careful. Especially in multi-threaded operations, operations that do not work well may interfere with each other, and this problem is hard to be found. 

If you use parameters as the method to pass data, 
First, the parameter list is bloated and should not have been passed in; 
Second, the read and write operations of parameters are the same as those of static members, and mutual interference occurs during multi-threaded operations. 

The potential harm of these static functions can be avoided to the member functions of the class. 

Okay, I'm not talking about it anymore, 
It is recommended that you determine which functions should be implemented using static functions and which functions should not be implemented using static functions.

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.