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

Source: Internet
Author: User
Document directory
  • 1. Public:
  • 2. Private:
  • 3. protected:
  • 4. Internal:
  • 5. Protected internal:

To control the access permissions of objects in C #, you can add modifiers before defining objects.

There are five modifiers: private, protected, internal, public, and protectde.
Internal (only the types in this program or inherited from this class can be accessed ).

Modifier objects: namespace (namespace), Class (class), struct (structure), Enum (enumeration), interface (Interface), delegate (delegate ), function, variable

1. Public:

Access is unrestricted. It is a modifier with the most permissions. namespace and enum members. Interface members implicitly have public modifiers. They cannot be explicitly added.

2. Private:

Only members in. Class or truct that can be accessed in the same class or struct are private by default if no modifier is added.

3. protected:

Access is limited to the current class or the inherited class. for example, to declare a function protected int add () {} in a class student, you can only use add in student or base in the subclass inherited from student. add () to use num. however, the object obtained by instantiating the class cannot be added. for example, student s = new student (); S. add (); this is incorrect. (Note: class declaration, which cannot be modified by protected before struct. public or internal only)

4. Internal:

Only the current Assembly. That is, in the same project.

Class or struct. If no modifier is added to the interface, the default value is internal. but it can also be declared as internal or public (Note: if the class, struct is declared in a class, it can also be protected, private modification. that is to say, a class can also be embedded to declare a class)

5. Protected internal:

The same assembly or inherited subclass can be accessed. in fact, it is equivalent to adding the protected and internal permissions together. (Note: Because struct cannot be inherited, its members cannot be modified by protected or protected internal.

Note: The Accessability of the derived class cannot be higher than that of the base class. that is to say, if the base class is modified by internal, the subclass can only be modified by internal. if both the base classes are public, the subclass can be internal or public.

In addition to permission modifiers, there are also four modifiers sealed, abstract, virtual, and partial (these four modifiers can be used together with the preceding five permission modifiers. write it before or after the permission modifier)

Sealed can only be used to modify the class, indicating that the class cannot be inherited.

Abstract can be used to modify the members of the class and class. however, only after the class is abstract modified can the member be abstract modified. it indicates that this class is an abstract class and cannot be instantiated. It must be inherited and overwritten.

Virtual functions can only be used to modify the functions in the class, indicating that they are virtual functions. virtual functions are the same as general functions except those that can be overwritten. It is optional and not necessary to re-Override virtual functions.

 

Partial

You can use the keyword partial to separate a class and write it in different files in a project. This function is particularly useful when a class is very large and written by several people.

In winform development, the system automatically uses partial to separate a class for writing. for example, if form1. is added, two files are displayed: form1.cs and form1.designer. CS is actually a class, which is divided into two files by the keyword partial. we mainly Add code in form1.cs. form1.designer. CS is automatically generated page-related code. in fact, we can also choose form1.designer by ourselves instead of dragging controls. add code to CS. however, this is too troublesome.

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.