Several modifiers of the C # class

Source: Internet
Author: User
Tags class definition modifiers

None or the internal class can only be accessed in the current project

The public class can be accessed from anywhere

Abstract or internal abstract classes can be accessed in the current project, cannot be instantiated, only inherit

The public abstract class can be accessed anywhere, cannot be instantiated, and can inherit only

The sealed or internal sealed class can only be accessed in the current project, can only be instantiated, cannot inherit

The public sealed class can be accessed anywhere, only instantiated, not inherited

1. Modifier for keyword class

1) Internal default modifier, specifies that the class is internal, only the code in the current project can access it

2) public specifies that the class is common, and that code in other projects can access it

3) abstract specifies that the class is abstracted, cannot be instantiated, can inherit only, and may have abstract members

4) Sealed sealed, cannot inherit

Where internal and public are a pair of mutually exclusive relationships, abstract and sealed are mutually exclusive, and the two pairs can be used in conjunction with each other.

The default class definition is as follows:

Class a{} is the equivalent of internal class a{},internal, which is usually omitted.

2. Modifier for keyword interface

1) Public and private This is the same as the definition of the access modifier and the keyword class, because common features of the interface are usually used

2) abstract and sealed the pair modifiers cannot be used in the interface because they are meaningless to the interface

When defining a derived class, if there is both a base class and an interface, then write the base class and then write the interface, separated by commas, can inherit only one base class but can have multiple interfaces, such as:

Class Derivedclass:baseclass,iinterface1,iinterface2

{}

Interface definitions and inheritance are as follows:

public interface Imyinterface:iinterface1,iinterface2

{}

3. Modifiers for class members (including fields, properties, methods)

1) public--members can be accessed by any code

2) private--members can only be accessed by code in this class (default value)

3) internal--members can only be accessed by code within the project (assembly) that defines it

4) protected--members can only be accessed by code in a class or derived class

The last two keywords can be combined, that is, protected internal, which can only be accessed by the code of the class and derived classes in the project (assembly). There is also static in parallel with the above keywords, which represents the static members of the class, not the instance members of the class. A field can also be defined as realonly and cannot be changed until it is assigned and assigned to a definition or constructor.

4, the class method also has another keyword definition

1) The virtual--method can be overridden

2) The abstract--method must be overridden (for use in abstract classes only, and if the class method defines an abstract then class must also append abstract)

3) The override--method overloads a base class method (the keyword must be used if the method is overloaded)

4) extern--method definition in other places

Another: A class method of a derived class can also use new to hide the base class method, through this point to the current class instance reference, to the base class instance reference through the base keyword.

Several modifiers of the C # class

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.