The difference between override and overload in C #

Source: Internet
Author: User

Overloading should be called overload, override called Override, and overloading a method occurs in the same class! Overriding is to override a method in a subclass in a parent class.

1.override: Parent class: Public virtual string ToString () {return "a";}
Subclass: public override string ToString () {return ' B ';}
2.overload: Available in the same class or in a parent-child relationship class.
public string ToString () {return "a";}
public string ToString (int id) {return ID. ToString ();}
Overrides are virtual methods used to override the base class, which provides a new method in the derived class;

Overloading (overload) provides a mechanism by which the same function names are differentiated by different return value types and parameter tables.
=====================================================

The essential difference is to look at the function characteristics: Overwrite (override) of the two functions of the same function, overloaded (overload) of the two functions of the function name, although the same, but the function characteristics are different.
function features include the function name, the type and number of arguments.
Override is at the time of inheritance, if you write a function that is the same as the function function to inherit, then, with this keyword, when using this function of this subclass, the parent class (or super Class) function is not visible, it is overwritten.
For example: derived inherited the base,base inside has void a (int a), then if you derived inside think a is not good or suitable for this class, you want to re-write again A in the code, then write the override void a (int a) this way, The original function was overwritten by the new one you wrote.
Overload is overloaded, that is, function names are the same, function features are different, the system will adjust the corresponding functions according to the parameters you provide.
For example: void a (int a) and void a (int a,int b)
If you are using a (1) then the first one, if it is a (a) then the second one of the tune.
=====================================================
1. Overwrite (override)
------------------
Use the override modifier to modify a method, property, indexer, or event. The override method provides a new implementation of a member inherited from a base class. Methods overridden by rewrite declarations are called overriding base methods. The overriding base method must have the same signature as the overridden method.
You cannot override a non-virtual method or a static method. The overriding base method must be virtual, abstract, or overridden. That is, the method in the base class overridden with the override modifier must be a virtual, abstract, or override method.

2. Overloading (overload)
------------------
Method overloads occur when a class contains two methods with the same name but different signatures.

Guidelines for using overloaded methods:
A. Use method overloading to provide different ways of accomplishing the same functionality semantically.
B. Use method overloading instead of allowing default parameters. The default parameters have poor versioning performance, so default parameters are not allowed in the Common Language Specification (CLS).
C. Use default values correctly. In an overloaded method family, the complex method should use the parameter name to indicate the change from the default state assumed in the simple method.
D. Use a consistent sorting and naming pattern for method parameters. Provides a set of overloaded methods that have an incremented number of parameters, so that developers can specify the level of information they want, which is a common scenario. The more parameters you specify, the more detailed the developer can specify.
E. If you must provide the ability to override the method, make only the most complete overload virtual and define other actions based on it.

 Public classsampleclass{Private stringmyString;  PublicSampleClass (stringstr) {       This. myString =str; }    Public intIndexOf (strings) {returnIndexOf (s),0); }    Public intIndexOf (stringSintStartIndex) {      returnIndexOf (S, StartIndex, Mystring.length-StartIndex); }    Public Virtual intIndexOf (stringSintStartIndex,intcount) {      returnMystring.indexof (S, StartIndex, Count); }}view Code
View Code

The difference between override and overload 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.