C # modifier in language,

Source: Internet
Author: User

C # modifier in language,

Public: public access. Not limited.

Private: private access. Only access by members of this class is allowed, and neither the subclass nor the instance can be accessed.

Protected: protects access. The instance cannot be accessed only by the class or subclass.

Internal: internal access. It is restricted to access within the project (assembly), and others cannot be accessed.

Protected internal: Internally protects access. Only access to this project or its classes and sub-classes is allowed.

Accessible

1. class)
Default modifier: private
Modifiable: public, private, internal, protected, and protected internal

2. enumeration (enum)
Default modifier: private
Modifiable: public, private, and internal

3. structure (struct)
Default modifier: public

4. interface)
Default modifier: public

 

It can only be used as a modifier of a method or variable.

(1) const:Specifies the value of a field or local variable that cannot be modified.

Modify the declaration of a field or local variable. It specifies that the value of a field or local variable is a constant and cannot be modified.

(2) new:Hides inherited members from base class members.

When used as a modifier, The new Keyword can explicitly hide members inherited from the base class. Hiding inherited members means that the derived version of the member replaces the base class version. Hidden members are allowed without the new modifier, but warnings are generated. Explicitly hiding a member with new removes this warning and records the fact that the member is replaced with a derived version.

To hide an inherited Member, declare the member in the derived class with the same name and use the new modifier to modify the member.

It is wrong to use both new and override for the same member because these two modifiers are mutually exclusive in meaning. Using new will create a new member with the same name and hide the original member, while override will extend the implementation of inherited members.

(3) virtual:Declare the methods or accessors that can be modified by the override member in the derived class.

It is used to modify methods, attributes, indexers, or event declarations, and allows you to override these objects in a derived class.

When a virtual method is called, The system checks the runtime type of the object for the override member. This override member in most Derived classes will be called. If no derived class is used to override this member, it may be the original member.

By default, the method is non-virtual. You cannot override non-virtual methods.

Virtual modifiers cannot be used with static, abstract, and override modifiers.

Except for the declaration and call syntax, the virtual attribute behavior is the same as that of the abstract method.

(4) override:Provides New implementations of virtual members inherited from the base class.

Override modifiers must be used to extend or modify the abstract or virtual Implementation of inherited methods, attributes, indexers, or events.

(5) readonly:Declare a field. This field can only be assigned as part of the Declaration or in the constructor of the same class.

Is a modifier that can be used on a field. When a field Declaration includes a readonly modifier, the value assignment introduced by this Declaration can only appear as part of the Declaration, or in constructors of the same type. In this example, the value of the year field cannot be changed in the ChangeYear method, even if it is assigned a value in the class constructor.


========================================================== ======================================

In C #, in addition to access modifiers, there are also many non-access modifiers:

Partial, abstract, sealed, static, virtual, and override.

Partial: partial classification. A class can be divided into several parts and written in different files. It will be merged during compilation.

Into a file, and each part cannot be scattered in different programming sets.

Abstract: abstract class. Instances of this class cannot be created. When a modifier is used, this method must be composed of sub-classes.

If the subclass does not implement this method, the subclass is also an abstract class. Contains the abstract party

The class must be an abstract class.

Sealed: sealed class, which cannot be inherited. When modifying a method, this method cannot be overwritten.

Static: static class, which cannot be instantiated. Since it cannot be instantiated, this class cannot contain

Object Member, that is, all the members of this class are static; when a member of the category class, only the "class name. member"

.

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

Method.

Override: indicates that the method of the parent class is overwritten.

None or internal
Class can only be accessed in the current project
Public
Class can access public access anywhere. Not limited.

Abstract or internal abstract
Class can only be accessed in the current project, cannot be instantiated, can only inherit private access
Public abstract
Class can be accessed anywhere. It cannot be instantiated and can only be inherited.
Sealed or internal sealed
Class can only be accessed in the current project, cannot be derived, can only be instantiated
Public sealed
Class can be accessed anywhere, cannot be derived, can only be instantiated

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.