Improve C # code 21: Restrict type visibility

Source: Internet
Author: User


1. Not everyone needs to see everything, and neither of your types need to be public. To ensure that a type can complete its work, you should assign the minimum visibility to the type as much as possible. The visibility of types is generally not as high as you expected. Internal or similar classes can also implement public interfaces. All users can access the functions defined by the public interface in the private type.

 

2. Many independent classes should be internal (internal). You can further restrict visibility and embed protected or private classes into the original class. The lower the visibility, the less changes you need to upgrade later. The reason is simple.CodeThe smaller the number, the less modifications may occur in the future.

 

3. Creating an internal class is a method that is often ignored to limit the type scope.

 

4. Classes and interfaces exposed to the outside world in public form will be the contract of your components and must be taken seriously. The more complex the interface is, the more limited future modifications will be. The fewer public types are exposed, and the greater the scope for scaling later.

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.