C # class modifiers)

Source: Internet
Author: User

Class modifiers: public, internal, partial, abstract, sealed, static

Member modifiers: public, protected, Private, internal, sealed, abstract, virtual, override, readonly, const


Public: the most open, allProgramSet and other program set objects can be accessed


Protected: relatively open, accessible to its own members and sub-class members


PRIVATE: Only Members can access


Internal: Members in this Assembly can access


Partial

Points cannot be dispersed in different programming Sets


Abstract: when modifying a class, the class is an abstract class and cannot be created. Indicates that the method needs to be modified.

Implemented by sub-classes. If the sub-classes do not implement this method, the sub-classes are also abstract classes. Classes containing abstract methods must be abstract classes.


Sealed: When a class is modified, the class cannot be inherited. When a method is modified, the method cannot be overwritten.


Static: indicates the static class when the class is modified. Objects of this class cannot be instantiated. Since the class cannot be instantiated

Object members cannot be contained, that is, all the members of this class are static. When modifying a class member, this member is a class member and can only be indicated by the "class. member name"

Type access

When the static modifier constructor is used, the constructor cannot contain any parameters or modifiers, And the constructor cannot

Row initialization. However, static members can be initialized or called. It cannot guarantee when the class is executed, but it can guarantee that the class is used for the first time.

Before the instance type. The static member initialized in the static constructor is the final initialization result. For example:

Public static int test = 0;

Static person ()

{

Test = 3;

}


Static void main (string [] ARGs)

{

Console. writeline (person. Test); // The running result is 3.

}

That is, equivalent:

Public static int test;

Static person ()

{

Test = 3;

}

Virtual: modifier Member, indicating the virtual method. The parent class can contain the implementation of this class, and the Child class can override this function.


Override: indicates that this method overwrites the parent class method.


Readonly: indicates that the field is a read-only field.


Note: When modifying the reference type in readonly, the read-only object status may be modified due to improper operations. For example:

Readonly list <person> Persons = ....;

We may have modified persons in some places: persons. Add (new person ());


Const: modifier field, indicating that this field is a read-only field. The value of this field must be clearly known during compilation, and its value is hard-coded.

To the program, after modifying the type of members, you need to re-compile to make the modification take effect.

Readonly is read-only during running, and the content is determined during running. Therefore, after the readonly type member is modified, it takes effect without re-compiling.

.

Readonly cannot modify local variables. const can.

Note: When a class or method is not modified by any modifier, the default value is internal: 
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

Personnel, but this is not necessary. 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 Abstraction

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, make a class abst

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.