Abstract and virtual in C #

Source: Internet
Author: User
Tags abstract modifier

Abstract

The abstract modifier can be used with classes, methods, properties, indexers, and events.

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

An abstract class has the following attributes:

• Abstract classes cannot be instantiated.

• Abstract classes can contain abstract methods and abstract accessors.

• An abstract class cannot be modified with the sealed modifier, which means that the class cannot be inherited.

• A non-abstract class derived from an abstract class must include the actual implementation of all inherited abstract methods and abstract accessors.

• Use the abstract modifier in a method or property declaration to indicate that this method or property does not contain an implementation.

Abstract methods have the following characteristics:

• Abstract methods are implicit virtual methods.

• Only abstract method declarations are allowed in abstract classes.

• There is no method body because the abstract method declaration does not provide a real implementation, and the method declaration ends with a semicolon and does not have braces ({}) after signing. For example: Public abstract void MyMethod ();

• Implementation is provided by the overriding method, which is a member of a Non-abstract class.

• It is an error to use a static or virtual modifier in an abstract method declaration.

Abstract properties behave like abstract methods, except for differences in declaration and invocation syntax.

• It is an error to use the abstract modifier on a static property.

• In a derived class, you can override an abstract inherited property by including a property declaration that uses the override modifier.

Virtual

The virtual keyword is used to modify the declaration of a method or property, in which case the method or property is called a virtual member. The implementation of a virtual member can be changed by an overriding member in a derived class.

When a virtual method is invoked, the Run-time type of the object is checked for the overriding member. The overridden member in most derived classes is called, and it may be the original member if no derived class overrides the member.

By default, the method is non-virtual. Non-virtual methods cannot be overridden.

You cannot use the virtual modifier with the following modifiers:

Static abstract override

The behavior of a virtual property is the same as an abstract method, except that the declaration and invocation syntax are different.

• It is an error to use the virtual modifier on a static property.

• You can override a virtual inheritance property in a derived class by including a property declaration that uses the override modifier.

Override (Overrides the above two keyword modification methods)

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.