The C # class can contain two methods: the C # static method and the non-static method. So what are their definitions? What are their differences in use? Let's take a look at the most intuitive difference: the static method uses the static modifier, and the non-static method is vice versa.
Let's take a look at the differences between the static and non-static methods in C # in four aspects:
C # comparison between static and non-static methods 1. C # static members:
① Static members belong to the class and non-static members belong to the class instance.
② Each time a class instance is created, a new storage block is allocated to non-static members in the memory;
Non-static members belong to the class and are shared by instances of each class. No matter how many instances are created for the class,
The static members of the class only occupy the same region in the memory.
C # comparison between static and non-static methods 2. C # static methods
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. Because static methods can be used before class instantiation, non-static variables in the class must be allocated after instantiation,
In this way, the memory address used by non-static variables cannot be determined when the C # static method is called. So it cannot be used. The static variable address is fixed for the class, so it can be used.
C # comparison between static methods and non-static methods 3. C # static methods are a special member Method
It does not belong to a specific instance of the class, but to the class itself. Therefore, you do not need to create a class instance for the static method, but use the class name. Static Method format.
1. The static method is a member method in the class and belongs to the whole class, that is, it can be called directly without creating any object!
Only static variables and other static methods can appear in static! In addition, the keyword such as this... cannot be used in the static method because it belongs to the entire class!
2. The efficiency of static methods is higher than that of Instantiation. The disadvantage of static methods is that they cannot be destroyed automatically, but instantiated methods can be destroyed.
3. The static method and static variable always use the same memory after being created, and multiple memories will be created using the instance method.
4. There are two methods in C #: instance method and static method.
C # comparison between static and non-static methods IV. C # obtain the class name in static methods
Static Methods:
- string className =
- System.Reflection.MethodBase.
- GetCurrentMethod().ReflectedType.FullName;
Non-static methods can also be used:
- string className = this.GetType().FullName;
The difference between the C # static method and the non-static method is resolved to interpret the meaning of the C # static method. It is helpful for you to understand and learn the C # static method and the non-static method.
Http://www.router.net.cn/Article/24353.html