C # modifier

Source: Internet
Author: User

Modifiers are used to qualify types and declarations of type members from the definition you can see that there are two types of functions: access modifiers and declaration modifiers 1. Access modifiers (5) access modifiers describe public publicly accessible. Not subject to any restrictions. Private access. This class is restricted to access by members, subclasses, and instances. protected protected Access. This class and subclass access is limited, and instances cannot be accessed. Internal internal access. Access is limited to this item and cannot be accessed by others. protected internal internal protection access. Limited to this item or subclass access, other modifiers and default modifiers that do not have access to C # member types are as follows: Member type default modifier can be modifier enumpublicnoneclassprivatepublic, protected, internal, Private, protected internalinterfacepublicnonestructprivatepublic, internal, private2. Declaration modifiers (10) Partial: Defines partial classes and struct static throughout the same assembly: the declaration belongs to the type itself and not to a member of a particular object. Abstract: A class that can only be a base class of other classes. A method in a class declares only that it is not implemented, and the implementation of the method is done in his derived class. Sealed: The specified class cannot be inherited. Virtual: Used to decorate a method, property, indexer, or event declaration, and to allow overriding of these objects in a derived class override: Provides a new implementation of a member inherited from a base class, a modifier that hides a member inherited from a base class member, and is allowed to hide a member without using the new modifier. However, a warning is generated. As an operator for creating objects and calling constructors. Extern: Used to declare methods that are implemented externally. The common use of the extern modifier is to use the DllImport attribute with the Interop service when it is being transferred into unmanaged code. In this case, the method must also be declared as a staticreadonly: Decorated field, which indicates that the field is read-only. 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 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. and readOnly the runtime is read-only and the content is determined at run time, so the ReadOnly type member is modified without recompiling to take effect. ReadOnly cannot modify local variables, const can. Important: The difference between virtual,override and new Virtual is used in the parent class, declaring that a method can be overridden, that the method cannot be static;override (overwrite inheritance) used in subclasses, overriding the parent class with virtual/override/ The method that the abstract keyword declares; new (hidden inheritance) is used in subclasses to decorate a method with or without a virtual declaration in the parent class, and if there is a method in the parent class with the same name as the parent class, C # implicitly adds a new keyword before the child class. "Difference" override and new when a subclass instance is assigned to the parent class instance, when the method is called separately in the subclass instance and in the parent class instance: The subclass method is called when the override is used, and the subclass method and the parent class method are called by using the new modifier. The two keywords, new and override, are mutually exclusive. Can not be used at the same time.  The "difference" between virtual and abstract virtual: used in the parent class to declare that the method can be overridden.  Abstract: It can only be declared in an abstract class, and cannot itself implement any function (so it must be override in a subclass) and cannot be instantiated. In addition to overriding this same point with override in subclasses, there is no other relationship.

C # modifier

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.