Read Summary (5) How to reasonably use the visibility of types and the accessibility of members to define classes

Source: Internet
Author: User
Tags types of functions
1. Class visibility

Class has two kinds of visibility.
1. Public, which is visible to all the code in the Assembly.
2, internal, is visible only to the Code in the Assembly that defines it, but not to the Code in other assembly.
Note: If the class visibility is not explicitly declared, the C # compiler is set to internal by default.

2. Accessibility of Class Members

Class Members have a total of six types of Accessability. Here we summarize the three most commonly used Accessability modifiers. Their restriction ranges from top to bottom, and the restriction ranges from maximum to minimum.
1. Private, which can only be accessed by methods that define its type.
2. protected, which is the same as private, except that it allows methods in its derived class to access.
3, public, no access restrictions.
Note: If the accessibility of the member is not explicitly declared, the C # compiler is set to private by default. In addition, any member who wants to be accessed by others must be defined in a visible type.

Iii. Guiding principles for defining classes

1. When defining a class, unless a class has been determined to be used as the base class, and the derived class is allowed to be specialized, it is always explicitly specified as a sealed class.Because sealed cannot be derived from it, this ensures the security of the class.
2. The data field is best defined as private within the class.In this way, only internal methods of the class can be accessed, and the object security is ensured.
3. In the class, it is best to define methods, attributes, and events as private and non-virtual.If you want to publish some types of functions, you can define methods, attributes, and events as public. Otherwise, it is best not to publish them.
Note: a motto of OOP: when things become too complex, more types will be created. When the implementation of an algorithm becomes complex, I will define some helper classes to close independent functions.

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.