C # type member styles

Source: Internet
Author: User

Static Member

The type member modified with the keyword "static" is static. The type fields, attributes, methods, events, and constructors can all be static. For example, in the PeopleClass above, the following code defines a static method.
Publicstaticint CalculateAge (DateTime birthday) {returnDateTime. Now. Subtract (birthday). Days/365 ;}
This static method can be calculated based on the birthday. Because the computing age is a common algorithm and is not limited to a specific object, it can be defined as a static class.
The static member type can be called by "type name. member name" without the need to create an object instance. For example, the following code calls the CalculateAge method.
DateTime dtm = newDateTime (1980, 2, 14); int age = leleclass. CalculateAge (dtm );
For static types, you cannot use the "instance variable. member name" method. For example, the following code is incorrect.
PeopleClass instance = newPeopleClass (); DateTime dtm = newDateTime (1980, 2, 14); int age = instance. CalculateAge (dtm );
Note that calling static member C # differs from VB. NET syntax. In VB. NET, you can use "derived type name. member name" to call static members, but C # does not. For example, if the static member M is defined in Type A and B is derived from type A, the static member M is defined in VB. "A.M" or "B .M" can be used to call static members in. NET, but "B .M" cannot be used in C.
For example, the following code defines the static method "Sum" and static field "Value ",
PublicclassMyClass {public static int Value = 100; publicstaticint Sum (int a, int B) {return a + B ;}}
In this way, we can use "MyClass. Sum" to call this method without creating an object instance. If a new type is derived from this type, the Code is as follows:
PublicclassOtherClass: MyClass {}
In C #, only "MyClass. sum "to call this static method, while in VB. NET can contain "MyClass. sum and OtherClass. sum.
 
Static Field
Static fields can be assigned values, and the scope of static fields is the whole program, which is equivalent to global variables. For example, after modifying the Value of the static variable "Value" here, the field value obtained elsewhere is the modified value.
 
Static Constructor
The non-parameter constructor modified by "static" is a type static constructor. For example, for the PeopleClass Type above, the following code defines a static constructor.
Static PeopleClass () {System. Console. WriteLine ("Start ");}
When a program is loaded, if the program does not call a member in PeopleClass, the static constructor will not be called, or even the typeof operation will not be called; however, before the program references a member of the type or creates an object instance for the first time, the system automatically calls a static constructor of the type once, and during the entire software running, A type of static constructor can be called only once and will not be called repeatedly.
Static constructor is suitable for delayed system initialization, which can speed up the system startup.
 
Note: The static constructor must have no parameters and must be private. You cannot set the accessible level.
 
Instance Member
A type member not modified by the "static" keyword is an instance Member. For example, a ToString method is defined in PeopleClass. The Code is as follows.
Publicoverridestring ToString () {return _ Code + "" + _ Name ;}
You cannot use PeopleClass. ToString to call this method. You must first create an object instance and then call the method of this object instance. The Demo code is as follows:
Leleclass instance = newPeopleClass (); instance. ToString ();
Virtual Member
The member type modified by the keyword "virtual" is a virtual member. For example, the following code contains a virtual function named "Sum.
PublicclassMyClass {public virtual int Sum (int a, int B) {return a + B ;}}
In fact, virtual members are not virtual. They can contain substantive functional code and complete certain functions. However, virtual members can be easily reloaded. Successors can also not reload these virtual methods as needed.
 
Abstract Member
The type member modified by the keyword "abstract" is an abstract member. Abstract members can only be attributes, methods, and indexers. The following code defines an abstract method.
Publiceffecactint Sum (int a, int B );
In this Code, "public" indicates that the method is public; "abstract" indicates that this is an abstract member; "int" indicates the type of Return Value of the method; "Sum" indicates the method name, and "int a, int B" indicates the parameter list of the method.
Defining abstract types is similar to defining interfaces. You only need to write the declaration of members, but you also need to write the accessibility of abstract members.
Abstract members must appear in abstract classes. When a new type is derived from an abstract class, all abstract methods must be overwritten to fill the method body. [Yuan Yongfu copyright]
 
Abstract members are different from virtual members. Abstract members cannot define any substantive functions and must be overloaded. Virtual members must contain the complete code structure and substantive functions, it can not be overloaded.
Constant Member
The member field modified by the keyword "const" is a constant field. You can use "type name. Field name" to reference constants. For example, the following code contains a constant.
PublicclassMyClass {publicconstdouble PI = 3.1415926 ;}
For this, we can use "MyClass. PI" to obtain this constant value. Constant values cannot be modified. For example, for the value assignment code "MyClass. PI = 3.14; "indicates an error. In contrast, static fields can be modified. For example, the code" publicstaticdouble PI = 3.1415926; "is used to define this field, then you can assign a value to this field.

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.