C # selecd, new, virtual, abstract and override,

Source: Internet
Author: User

C # selecd, new, virtual, abstract and override,

Most of the content in this article is taken from. NET development expert Liang jian. NET:. NET in-depth experience and practical needs.

The blogger is only a porter.

Abstract classes are not very clear about virtual methods and cannot be used in their current work.

I downloaded a book a few days ago and found that it was very vivid and made me suddenly enlightened.

Sort it out and try again to help more beginners understand this knowledge.

 

1. sealed -- "peerless"

 

The sealed class cannot be inherited.

The sealing method can override the methods in the base class. However, it cannot be further rewritten in any derived class.

When applied to methods or attributes, the scaled modifier must be used together with the override.

 

2. new -- "I am yours, you are mine"

 

The new keyword is used to explicitly hide the member inherited from the base class. That is to say, the method called when using the derived class is the newly defined method of the new Keyword, rather than the method of the base class.

It is also possible to hide members without the new Keyword, but a warning is generated. Explicit member hiding with new will cancel the alarm.

 

3. virtual -- "for future generations and future generations"

 

The virtual keyword is used to modify the declaration of a method or attribute. In this case, a method or attribute is called a virtual member. The implementation of the Virtual Member can be modified by the override member in the 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, private, or override modifiers. Except for the declaration and call syntax, the virtual attribute behavior is the same as that of the abstract method.

The override modifier is used to override the virtual inheritance attribute in the derived class.

 

4. abstract -- "I am God"

 

Abstract modifiers can be used with classes, methods, attributes, indexers, and events.

Use the abstract modifier in the class declaration to indicate that the class can only be the base class of other classes.

Abstract class features:

  • Abstract classes cannot be instantiated.
  • Abstract classes can contain abstract methods and abstract accessors.
  • The abstract class cannot be modified with the sealed modifier, which means the class cannot be inherited.
  • A non-abstract class derived from an abstract class must include all the inherited abstract methods and the implementation of the abstract accessors.
  • The abstract modifier is used from the method or attribute declaration to indicate that this method or attribute does not contain an implementation.

Features of abstract methods

  • The abstract method is an implicit virtual method.
  • Only abstract methods can be declared in abstract classes.
  • Abstract method declaration does not provide actual implementation, so there is no method body. The method declaration ends with; and there is no {} symbol after the signature. For example, public abstract void MyMethod ();
  • The implementation method is provided by the overriding method, which is a non-abstract class member.
  • It is incorrect to use the static or virtual modifier in the abstract method declaration.
  • Except for the declaration and call method, the behavior of the selected attribute is the same as that of the abstract method.

 

5. override -- "cover the sky with one hand"

 

Use the override modifier to modify methods, attributes, indexers, or events. It mainly provides a new implementation of the base class method of the derived class, covering the abstract and virtual modified members.

The base class method to be rewritten must have the same signature as the rewrite method.

The override base class methods must be virtual, abstract, or overwritten. You cannot override non-virtual or static methods.

You cannot use the new, static, ubuntual, and abstract keywords to modify override method members.

The override attribute declaration must have the same access modifier, type, and name as the inherited attribute. And the rewrite attribute must be virtual, abstract, or rewritten.

Similarities between new and override

  • You can hide base class members. You can use the base keyword to call base class members.

Difference between new and override

The methods of base classes using override must use virtual instead of new.

When the same base class object calls the virtual method of the base class, the methods of the override derived class will be accessed, while the methods of the new override derived class will not be accessed.

 

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.