Example of C # static method concept resolution

Source: Internet
Author: User
Tags class definition

A C # static method is a special member method that does not belong to a specific instance of a class. Non-static methods can access any member of a class, while static can only access static members in a class. So how do we handle this feature of the C # static method? Let's take a look at a simple example of a C # static method application:

C # static Method instance Demo:

Class A

{

Int x;

Static int y;

Static int F(){

x=1; //Error, no access allowed

y=2; //correct, allow access

}

C # static Method application Example Analysis:

1. In this class definition, C # static method F () can access static member Y in the class, but cannot access non-static member X.

2, X as a non-static member, each instance of the class occupies a storage (or a copy), and the static method is shared by the class, and it cannot tell what class the current x belongs to, so it does not know which address in memory to read the value of the current x.

3, Y is a static member, all instances of the class have a common copy, static method F use it does not have any problems.

The example parsing of the C # static method is introduced to you here, and it is hoped that this example will give you an idea of the C # static method.

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.