C# class modifier insights

巴扎黑
Release: 2016-12-20 09:42:42
Original
1208 people have browsed it

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

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


Public: the most open, all Objects in this assembly and other assemblies can be accessed


Protected: relatively open, its own members and subclass members can access


Private: only its own members can access


Internal: this assembly Members within can access


Partial: Partial class, a class can be divided into several parts and written in different files. They will be merged into one file during final compilation, and the various parts cannot be scattered in different assemblies


Abstract: When modifying a class, it means that the class is an abstract class and instances of this class cannot be created. When modifying a method, it means that the method needs to be implemented by a subclass. 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 modifying the class, it means that the class It cannot be inherited. When modifying a method, it means that the method cannot be overridden.


Static: When modifying a class, it means that the class is a static class and cannot instantiate objects of this class. Since this class cannot be instantiated, this class cannot contain object members, that is, all members of this class are static; When modifying a class member, the member is a class member and can only be accessed through [class. member name]


When static modifies the constructor, the constructor cannot contain any parameters or modifiers, and the constructor Functions cannot initialize object members. But static members can be initialized or called. There is no guarantee when it will be executed, but it can be guaranteed to be executed before the class

is used for the first time. Static members initialized in the static constructor are the final initialization results. For example:

view plain

public static int test = 0;

static Person()

{

test = 3;

}

static void Main(string[] args)

{

  Console.WriteLine(Person.test);//The running result is 3

}

Note: When a class or method is not modified by any modifier, the default is internal:

C# uses a variety of modifiers to Express different properties of classes. C# classes have five different restriction modifiers according to their protection level:


public can be accessed arbitrarily;

protected can only be accessed by this class and its inherited subclasses;


internal can only be accessed by this assembly ( All class access within Assembly). Assembly is the logical and physical unit of combined classes in the C# language. The compiled file extension is often ".DLL" or ".EXE".

protected internal is the only combination restriction modifier. It can only be accessed by all classes in this combination and the inherited subclasses of these classes.

Private can only be accessed by this class.

If it is not a nested class, the class in the namespace or compilation unit only has two modifications: public and internal.

The new modifier can only be used for nested classes, indicating that the type with the same name inherited from the parent class is hidden.

abstract is used to modify an abstract class, indicating that the class can only be used for inheritance as a parent class and cannot be instantiated as an object. An abstract class can contain abstract members, but this is not required. abstract cannot be used together with new.

Related labels:
c#
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!