Learn C # Modifiers: class modifiers and member modifiers

Source: Internet
Author: User

Modifiers such as C # Modifiers: public, internal, partial, abstract, sealed, static

C # modifier member modifiers: public, protected, private, internal, sealed, abstract, virtual, override, ReadOnly, const

Public: Most open, all of this assembly and other objects inside the assembly can access

Protected: More open, self-member and subclass member accessible

Private: Only its members can access

Internal: Members within this assembly can access

Partial: Some classes can be divided into parts of a class in different files, the final compilation will be merged into one file, and the parts can not be scattered in different assemblies

Abstract: When a class is decorated, it indicates that the class is an abstract class and cannot create an instance of the class. When the method is modified, the method needs to be implemented by subclasses, and if the subclass does not implement the method then the subclass is also an abstract class, and the class containing the abstract method must be an abstract class.

Sealed: When a class is decorated, it means that the class cannot be inherited, and the method is decorated to indicate that it cannot be overwritten.

Static: When a class is decorated to represent the class when it is not able to instantiate an object of that class, and since the class cannot be instantiated, then the class cannot contain an object member, that is, all members of the class are static, and when a class member is decorated, the member is a class member and can only be accessed through the class. Member name method.

When the static modifier constructor, the constructor cannot contain any arguments, cannot contain modifiers, and the constructor cannot initialize an object member. However, it is possible to initialize or invoke static members. There is no guarantee of when he will execute, but it is guaranteed to be executed before the first use of the type. The static member that is initialized in the static constructor is the final initialization result.

Virtual: Modifies a method member to represent a virtual method. The parent class can contain the implementation of the class, and subclasses can override the function.

Override: Indicates that the method is a method that override the parent class.

Readonly: A decorated field that represents a read-only field.

Note: readonly modifies a reference type when the read-only object state may be modified due to improper operation. For example:

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

We may have modified the persons in some places: Persons.add (new Person ());

Const: A decorated field that represents a read-only field. And at compile time must be able to clearly know the value of the field, its value is hard-coded into the program, modify the type of member after the need to recompile for the changes to take effect.

While ReadOnly is run-time read-only and content is determined at run time, the ReadOnly type member is modified without recompiling to take effect.

ReadOnly cannot modify local variables, const can.

Note: When a class or method is not decorated with any modifiers, the default is internal:

C # uses a variety of modifiers to express the different properties of a class. There are five different restriction modifiers for classes based on their protection level C #:

Public can be arbitrarily accessed;

Protected can only be accessed by this class and its inheriting subclasses;

Internal can only be accessed by all classes within the group (Assembly), which is the logical and physical unit of a class being combined in the C # language, and its compiled file extension is often ". DLL "or". EXE ".

Protected internal is the only combination restriction modifier that can be accessed only by all classes in the group and by the inherited subclasses of those classes.

Private can only be accessed by this class.

If the class is not nested, the namespace or the class within the compilation unit has only public and internal two adornments.

The new modifier can only be used for nested classes, which represent shadowing of a type with the same name inherited from the parent class.

Abstract is used to decorate a class, indicating that the class can be used only as a parent class for inheritance, not object instantiation. An abstract class can contain abstract members, but this is not required. Abstract cannot be used in conjunction with new.

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, implements the class abstract method F (), and can then instantiate the object.

Sealed is used to decorate classes as sealed classes, preventing the class from being inherited. It is meaningless and forbidden to make an abstract and sealed modification to a class at the same time.

Learn C # Modifiers: class modifiers and member modifiers

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.