Access Modifiers (public,private,protected,internal,sealed,abstract)

Source: Internet
Author: User

In order to control access to objects in C #, you can add modifiers before defining an object.

There are five modifiers: private (privately), protected (protected), internal (internal to the Assembly), public (publicly), and protectde internal (only those types that are within or inherited from this program can be accessed).

Objects that can use modifiers: namespace (namespace), Class (Class), struct (struct), enum (enum), Interface (interface), delegate (delegate), function (functions), variable

1.public:

Unrestricted access. Is the most privileged modifier. namespace and enum members, members of interface implicitly have the public modifier. Its modifiers cannot be added explicitly.

2.private:

Only the same class or struct can be accessed. A member in class or truct if no modifier is the default private

3.protected:

Access is limited to the current class or inherited class. For example, declare a function protected int Add () {} In a class student, or only use Add in student, or in a subclass that inherits from student Base.add  () to use Num. But the object instantiated by class cannot be used with Add. For example Student s = new Student (); S.add (); This is wrong. (Note: It is not possible to add protected adornments before declaring class,struct. Only public or internal)

4.internal:

Limited to the current assembly. That's the same project.

Class or struct,interface the default is internal if you do not add modifiers. However, you can also display the declaration as internal or public (note: If Class,struct is declared in a class, it can be protected, Private decoration. That is, a class can also be nested in the declaration of a Class)

5.protected internal:

The same assembly or inherited subclass can be accessed. In fact, the equivalent of protected and internal two of the permissions are added together. (Note that because a struct cannot be inherited, its members cannot be decorated by protected or protected internal.)

Note: Derived class accessibility cannot be higher than the base class. That is, if the base class is internal decorated, the subclass can only be decorated with internal. If the base class has a public adornment subclass available, the internal is also available for public.

There are four modifiers in addition to the permission modifier sealed,abstract,virtual,partial (these four modifiers can be used with the above five permission modifiers.) write before or after a permission modifier

Sealed can only be used to modify class, which means class cannot be inherited.

Abstract can be used to modify members in class and class. But the class must be modified by the abstract, and the member can be modified by the abstract. It indicates that this class is an abstraction class, cannot be instantiated, must be inherited and overrides abstract functions

Virtual can only be used to modify a function in class, indicating that it is a virtual function, except that a virtual function can be rewritten in the same way as a normal function. virtual function re-override is optional, not required

Partial

You can write a class separately by using the keyword partial, which can be used in a different file in project. This feature is particularly useful when a class is particularly large and is written in collaboration with several people.

REFERENCE from:http://blog.csdn.net/weiwenhp/article/details/6968868

Access Modifiers (public,private,protected,internal,sealed,abstract)

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.