Nested & access Modifiers

Source: Internet
Author: User

Nested types

You can nest any number of types (including class) in class and structure, but not in Enum.

 
Class{EnumParserstate {}} class person {class room {}EnumGuntype {}}

View code

Access Modifiers


Internal: Only packages are used in this assemble (classlibrary is DLL after compilation, consoleapplication16 is EXE after compilation, which is two independent assembles.

Other assemblies are invisible.

The above classlibrary1 is a class assembly DLL, class1 is internal, so another assembly applies this DLL, it cannot be new

Private:Non nestType (when class is defined), private cannot be added, and nest is not necessarily. It can only be explicitly public or internal, or the default value is iternal.

 
ClassPersonparser//Default Value: internal.{Int_ Privatedata;//Default Value: PrivatePrivate EnumParserstate//Explicit: Private{}}

View code

Protect: the inherited class can use protect methord, and the external class cannot be used. In usage, create base class creates several methords. I only want the child class to use these methords. The external classes are not available.

Summary of Public vs private vs protect:

    • The picture on the left is private, so it cannot be used by child class or external class. Protect on the right can only be used by child class, and internal can be used by third-digit assembly. Public all visible

    • After nest, write type. Type:

    • Protected internal: internal to yourself is internal, external is protect (use first inheritance ).

 

    • Both filed and methord in the interface are public.

    • Summary: Generally, we recommend that you use the private method whenever possible. If you need multiple Assembly sharing methods to use internal (for example, the reader and writer methods must be used by multiple projects), you need to use other external assemblies before using public.

ImplicitAnd explicitInterface implementations

Implicity:

 

 

Explicity: The Use method fullfill in the class tree can be used separately.

 

 

 

When writing an interface, we can use a type to inherit the interface, CTRL +. that is, implicaity creates a public method to associate with the method of the same name as the interface. If you write the explicity yourself, you need to write the interface. in this way, the method is private.

The same object in the interface has different results due to different vaiable types.

 

 

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.