This article will focus on C # static methods and instance methods. For some ideas, such as: "static method is resident memory", and "static method is loaded first than instance method" for analysis. We also discuss when to use static methods and instance methods.
A few days ago, a friend from Microsoft sent me a link: Do you know the difference between static methods and instantiation methods? Welcome to the discussion !! -The final question is..., and some points here need to be clarified. I hope to write a blog. I read this blog at the time. The article is short. I have listed several cases of static methods and instance methods, and I have no arguments. Then I will discuss them. Later comments are much more lively than blogs. The words were fierce. Later, the author simply deleted the blog. This blog is no longer visible. Now I am writing this blog. One is to make an analysis of some points of view, and the other is to fulfill the trust of friends.
C # static methods and instance methods
See the following code: We should have used these three forms.
- Public ClassSomeclass
- {
- Private StringMyfield =Null;
- Public StaticInstance =NewSomeclass ();
- PublicInstrancemethod (){};
- Public StaticStaticmethod (){};
- }
- Public ClassAnotherclass
- {
- Public StaticMain ()
- {
- // The first method is to declare the instance and call the instance method.
- Someclass =NewSomeclass ();
- Someclass. instancemethod ();
- // Method 2: Call the instance method through a static instance
- Someclass. instance. instancemethod ();
- // Method 3: directly call the static method
- Someclass. staticmethod ();
- }
- }
These methods differ in call time, thread security, and object-oriented programming. It will be discussed later.
"The static method is resident memory"
This is the opinion of the blog author in his comment. I think the "static method is resident memory" statement is incorrect. You need to know that a. Net Type Static Method belongs to this. Net type. This. Net type is part of a. Net assembly. This. Net assembly is loaded into the memory by an appdomain. This appdomain can be detached from the memory. There can be more than one appdomain in a process with. Net CLR, And the appdomain after the first appdomain can be dynamically created and uninstalled. The. NET assembly in these AppDomains can have both static methods and instance methods. Both static and instance methods are created and uninstalled along with the appdomain of the Assembly. The first appdomain will be uninstalled at the end of the entire program. The. NET assembly is also uninstalled. See Figure 1 to better understand. So the static method does not have resident memory.
Figure 1
"Loading static methods first than instance methods"
This is also the point raised by some people in the blog comment. I don't know what their arguments are, but I have already done experiments and have written these two blogs about. net reflection and metadata loading -- to some of jeffray Zhao's and firelong and [continue to discuss] about Windows PE and. net Assembly load to prove.. Net assembly is loaded in the whole place, instead of loading the metadata using a metadata. A method is used to load the metadata. Because static methods and instance methods belong to the same. Net type, and A. Net type belongs to a. Net assembly. When a. Net assembly is loaded, both the static method and the instance method are loaded into the memory with the Assembly. Therefore, "loading the static method first than the instance method" is not true.
When to use the static method and when to use the instance method
First, let's talk about the instance method. When you write a method to a class, if the method needs to access the member variables of an instance, it is defined as an instance method. A class-one instance usually has some member variables, which contain the status information of the instance. This method needs to change these statuses. This method must be declared as an instance method.
The static method is the opposite. It does not need to access the member variables of an instance and does not need to change the status of an instance. We define this method as a static method.
The first method is to declare the instance and call the instance method.
When a class has multiple instances, such as student A, student B, and student C, we use the first method. In the case of multiple threads, as long as each thread creates its own instance, the first method is generally thread-safe.
The second method is to call the instance method through a static instance.
This situation is special. It is usually the only instance of this class in the whole program. We change the status of this instance by calling the instance method of this instance. This instance is thread-safe in the case of multiple threads. Unless we lock this instance. Prevent other threads from accessing the instance.
Method 3: directly call the static method
In this case, the static method does not need to change the status of an instance. You only need to obtain a few parameters to complete the tasks. For example, to determine whether a file exists, you only need to provide the file path and file name to know whether the file exists.