Polymorphism, overloading, overriding differential analysis in C #

Source: Internet
Author: User

This paper summarizes the concepts and differences of polymorphism, overloading and rewriting in C #. For beginners in C # friends have a good reference value. Share to everyone for your reference. The specific analysis is as follows:


Rewrite



Is the method that overrides the base class, the method in the base class must have the modifier virtual, and the override must be indicated in the method of the subclass

The format is as follows:

1. In the base class:

public virtual void MyMethod () {}

2. In the subclass:

public override void MyMethod () {}

After rewriting, the MyMethod () method is accessed with base class objects and subclass objects, and the result is access to methods redefined in the subclass, and the method of the base class is equivalent to being overwritten.


Overload:


Used to select an optimal function member to implement the invocation given the argument list and a set of candidate function members


public void Test (int x,int y) {}public void test (int x,ref int y) {}public void test (int x,int y,string a) {}


Features of overloading:

I.

Method names must be the same

Ii.

The argument list must be different, regardless of the order of the parameter list

Iii.

Return value types can be different


But if there is a generic type, you should pay attention!


Polymorphic:


C # polymorphism is mainly embodied in the inheritance of class

:

When a subclass inherits from a parent class, there may be cases with the same name but different method definitions.

Therefore, the original method will be overridden in the subclass to achieve its own requirements.


Here are two points to note:

①. A method that can be overridden in a subclass must be marked as virtual, abstract, and override (override) The function marked as virtual and abstract is created for rewriting, The function marked as override is itself overridden by the first two functions, so it is also logical that it can be rewritten;

②. The overridden function must appear in the subclass, and any function of the parent class can be overridden only once in one of its subclasses. (This is a good understanding that when you want to rewrite two times, the subclass will define two return types, the method name and the parameter list are the same function, which is certainly not possible).

It is believed that this article has some reference value for the study of C # program design.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Polymorphism, overloading, overriding differential analysis in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23494.html






Related content. NET WinForm Implementation method for adding ProgressBar in ListView C # Rx's main interface in-depth understanding of C # Dynamic types, and dynamic object creation, merging 2 objects, map instance C # application Npoi get pictures in Excel, Save to local algorithm
C # datetime.tostring generates appropriate time formats for different languages understanding lambda expressions in C # Remove substrings from a specified position in a string C # methods for generating code128 barcodes

Polymorphism, overloading, overriding differential analysis in C #

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.