c static and non static methods are described in detail

  • 2020-06-12 10:31:53
  • OfStack

The classes of C# can contain two methods: static and non-static.
Methods that use the static modifier are static methods and non-static methods.
A static method is a special member method that belongs not to a specific instance of a class, but to the class itself.
So for static methods, it is not necessary to first create an instance of a class, but to use the class name.
1.static method is a member method in the class, which belongs to the whole class. It can be called directly without creating any objects!
Only the static variable and other static methods can appear inside static! And you can't use this in the static method yet... Etc keywords.. Because it belongs to the whole class!
2. The static method is more efficient than the instantiation method. The disadvantage of the static method is that it does not automatically destroy, while the instantiated method can destroy.
3. Static methods and static variables are always created using the same block of memory, while using an instance creates multiple memory.
There are two types of methods in C# : instance methods and static methods. The method code of a class is only 1 copy, and their life cycle and class are 1. Instance methods are called by the object name, and static methods are associated with the class rather than the object name.
5. So where in the program can use a static field and static constructor, and are usually applies to 1 will not change frequently and frequently used data, such as the connection string, configuration information and so on, when meet the above said two, read 1 time, later can be convenient to use, also saved the managed resource with, because for a static member of a static field only identifies a storage location.
No matter how many instances are created for a class, there will always be only one copy of its static fields. Because static members exist in memory, non-static members can directly access static members of a class.
Common handler functions, using static methods should be no problem.. When it comes to data sharing, static variable functions need to be considered more... Static variables should be used with care..
The principle of static methods is that sharing code fragments doesn't cause any problems because the code fragments are for CPU to "read," unless you're making malicious "changes" to the run-time code fragments so you can safely use static methods
The principle of static variables is that Shared data segments are the same as above and as long as there is no "write" there is no problem but data is usually used for read and write so be careful with static variables
Here is an example of using a static method

class Class1 { 
[STAThread]
static void Main(string[] args) 
{ 
int i = MyClass.Add(3,5); // Calling a static method  
Console.WriteLine(i); 
} 
} 
class MyClass 
{
public static int Add(int x,int y ) 
{ return x + y ;
} 
} 

Related articles: