C # use a variety of modifiers to express the different properties of a class

Source: Internet
Author: User
C # use a variety of modifiers to express the different properties of the class. Class C # of its protection level has five different restrictions and modifiers:

Public can be accessed at will;
Protected can only be accessed by this class and its inherited sub-classes;
Internal can only be accessed by all classes in the Assembly. The combination is the logical unit and physical unit after the classes in C # language are combined, the compiled file extension is often ". DLL or. EXE ".
Protected internal is a unique composite modifier that can only be accessed by all classes in the current composite and the inherited sub-classes of these classes.
Private can only be accessed by this class.
If it is not a nested class, only the public and internal classes in the namespace or compiling unit are modified.

The new modifier can only be used for nested classes, indicating hiding the type that inherits the same name of the parent class.

Abstract is used to modify abstract classes, indicating that the class can only be used as the parent class for inheritance, but cannot be instantiated. Abstract classes can contain abstract members, but they are not required. Abstract cannot be used together with new. The following is the pseudo code used in the abstract class:

Abstract Class
{
Public abstract void F ();
}
Abstract class B:
{
Public void g (){}
}
Class C: B
{
Public override void F ()
{
// Implementation of method F
}
}

Abstract class A contains an abstract method F (), which cannot be instantiated. Class B inherits from Class A, which contains an instance method g (), but does not implement the abstract method F (), so it must still be declared as an abstract class. Class C inherits from Class B and implements class abstraction method F (). Therefore, objects can be instantiated.

Sealed is used to modify a class as a sealed class to prevent the class from being inherited. At the same time, abstract and sealed modifications to a class are meaningless and prohibited.

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.