(original) C # learning note 08--Introduction to Object-oriented programming 01--the meaning of object-oriented programming 03--static and instance class members

Source: Internet
Author: User

8.1.4 static and instance class members

Members, such as properties, methods, and fields, are unique to object instances, and there are static members (also known as shared members, especially for visual Basic users), such as static methods, static properties, or static fields. static members can be shared between instances of a class, so they can be thought of as global objects of a class . static properties and static fields can access data that is independent of any object instance, and static methods can perform commands that are related to the object type but are independent of the object instance . When you use static members, you do not even need to instantiate the object.

For example, the Console.WriteLine () and Convert.ToString () methods used earlier are static and do not need to instantiate the console or Convert class at all (if you try to do this instantiation, the operation will fail, Because the constructors for these classes are not publicly accessible, as described earlier.

In many cases, static properties and methods have a good effect. For example, you can use static properties to track how many instances are created for a class . In UML syntax, the static members of a class are underlined , as shown in 8-5.

1. Static constructors

When you use static members in a class, you need to initialize those members beforehand.

You can perform this type of initialization task using a static constructor. A class can have only one static constructor, which cannot have an access modifier or take any parameters. Static constructors cannot be called directly and can only be performed in the following cases:

When you create a class instance that contains a static constructor

When accessing a static member of a class that contains a static constructor

  in both cases, the static constructor is called first, and then the class is instantiated or the static member is accessed . No matter how many instances of a class are created, their static constructors are called only once (static variables are common to all instances, so you only need to initialize them once.) ). To differentiate between static constructors and the constructors described earlier in this chapter, all non-static constructors are also referred to as instance constructors.

 

2. Static Class

We often want the class to contain only static members, and cannot be used to instantiate objects such as the console. A simple way to do this is to use a static class instead of setting the class's constructor to private. A static class can contain only static members and does not require an instance constructor because, by definition, it cannot be instantiated at all. However, a static class can have a static constructor , as described in the previous section.

(original) C # learning note 08--Introduction to Object-oriented programming 01--the meaning of object-oriented programming 03--static and instance class members

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.