C # class Modifier insights

Source: Internet
Author: User
Tags modifiers
Class modifiers: public, internal, partial, abstract, sealed, static

Member modifier: 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, the constructor cannot enter the object member

The line initialization operation. However, it is possible to initialize or invoke static members. There is no guarantee of when he will execute, but to ensure that the class is used for the first time

Type before execution. The static member that is initialized in the static constructor is the final initialization result. For example:

View Plain

public static int test = 0;

Static person ()

{

Test = 3;

}

static void Main (string[] args)

{

Console.WriteLine (person.test);//Run result is 3

}

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 are logical units and physical units of the class being combined in the C # language.

The compiled file name 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. Abstract classes can contain abstract

But this is not a must. Abstract cannot be used in conjunction with new.

  • Related Article

    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.