In this paper, the static and Non-static methods of C # are analyzed, and their usage is analyzed comprehensively. Share to everyone for your reference. The specific analysis is as follows:
In general, C # classes can contain two methods: static methods and Non-static methods.
The static modifier is used as the method, and the opposite is a non-static method.
A static method is a special member method that is not part of a specific instance of a class, but belongs to the class itself. Therefore, it is not necessary to create an instance of a class first in a static method, but rather with the class name. The format of the static method.
1 The static method is a member method in a class that is part of the entire class, that is, it can be invoked without creating any objects.
Only static variables and other static methods can appear inside static, and the static method cannot use such keywords as this, because it belongs to the entire class.
2 static method efficiency is higher than the instantiation, the disadvantage of static method is not automatically destroyed, and the instantiation can do destruction.
3 static methods and static variables always use the same memory after the creation, and the way to use the instance creates multiple memory.
4 So where in the program can use static fields and static construction methods, usually applies to some not often change and frequently used data, such as connection string, configuration information, etc., when the above mentioned two points, a read, can be easily used later, but also save the managed resources, Because for static members, a static field identifies only one storage location. No matter how many instances are created for a class, its static fields will always have only one copy.
Static members exist in memory, and non-static members need to be instantiated to allocate memory, so static members cannot access non-static members. Because static members exist in memory, non-static members can directly access static members of a class. Common processing functions, the use of static methods should be no problem, involving data sharing, static variable functions to consider more, static variables to be used carefully.
static method:
the principle is to share code snippets;
Sharing code snippets does not cause any problems;
Because the code snippet is for the CPU as "read" use, unless you make malicious "modify" Run-time code snippet;
So the static method is safe to use;
Static variables:
the principle is to share data segments;
As long as there is no "write" operation does not cause problems, but the data is usually used for reading and writing, so static variables should be paid attention to use;
Here is a simple example of using a static method:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace ConsoleApplication1
{
class program
{
static void Main (string[] args)
{
int i = Myclass.add (7, 11); Call static method
Console.WriteLine (i);
Console.readkey ();
}
Class MyClass
{public
static int Add (int x, int y)
{return
x + y;
}
}}
I hope this article will help you with the learning of C # programming.