C # static methods static properties call static methods

Source: Internet
Author: User

C # classes can contain two methods: static and non-static methods.

Methods that use the static modifier are static and, conversely, are non-static methods.

A static method is a special member method that is not part of a specific instance of a class, but rather belongs to the class itself. Therefore, the static method does not need to create an instance of a class first, but instead takes the class name. The format of the static method.

The 1.static method is a member method in a class that belongs to the entire class, that is, you can call it directly without creating any objects!

Static variables and other static methods can only appear inside static! And this is not available in the static method .... and other keywords. Because it belongs to the whole class!

2. Static methods are more efficient than instancing, and the disadvantage of static methods is that they are not destroyed automatically, while instantiated can be destroyed.

3. Static and static variables always use the same piece of memory after they are created, and the way the instance is used creates multiple memory.

There are two methods in 4.c#: Instance method, static method.

There is only one copy of the method code for the class, and their life cycle and class are consistent. The instance method is called through the object name, and the static method is associated with the class instead of the object name.

5. Where in the program can use static field and static construction method, usually apply to some do not change frequently and frequently use of data, such as connection string, configuration information, when the above mentioned two points, a read, and then can be convenient to use, 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 field will always have only one copy (copy I understand as only a piece of memory

Static members exist in memory, 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 access static members of the class directly. The common handler function, using static methods, should have no problem: Data sharing is involved, the function of static variables should be considered more ... Static variables should be used with caution. Static methods

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", unless you make a malicious "modify" the code snippet of the runtime

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 will not cause problems but the data is usually used for reading and writing, so the static variables should be careful to use

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;

}

}

C # static methods static properties call static methods

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.