C # static method and non-static method instance Analysis _php skill

Source: Internet
Author: User

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

 static int Add (int x, int y)
  x + y;

Output Result: 18

I hope this article will help you with the learning of C # programming.

Related Article

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.