Differences between override and overload

Source: Internet
Author: User

1.
Parent class: public virtual string ToString () {return "";}
Subclass: public override string ToString () {return "B ";}
2.
In the same class or in the parent-child relationship class:
Public string ToString () {return "";}
Public string ToString (int id) {return id. ToString ();}

Override is a virtual method used to override the base class. In this way, a new method is provided in the derived class.

Overload provides a mechanism for table differentiation using different return value types and parameters of the same function name.

1. override
-----------
Use the override modifier to modify methods, attributes, indexers, or events. The override method provides a new implementation of Members inherited from the base class. The method overridden by the rewrite statement is called the override base method. The override base method must have the same signature as the override method.

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

That is to say, the methods in the base class that are rewritten with the override modifier must be virtual, abstract, or override methods.

 

2. Overload
-------
Method overload occurs when the class contains two methods with the same name but different signatures.

Guidelines for using overload methods:
A. Method Overloading is used to provide different methods to accomplish the same function in semantics.
B. Use method overload instead of allowing default parameters. The default parameter version control performance is poor, so the default parameter is not allowed in the public language specification (CLS.
C. Use the default value correctly. In a series of overloaded methods, a complex method should use a parameter name to indicate changes to the default state assumed in a simple method.
D. Use consistent sorting and naming modes for method parameters. Provides a set of reload methods with incremental parameters so that developers can specify the desired level of information. This is common. The more parameters you specify, the more detailed the developer can specify.
E. If you must provide the ability to override methods, just make the most complete overload virtual and define other operations based on it.
// The following describes the mode. Only the last method (the most complete method of the parameter) is a virtual method. In the subclass that inherits this class, you only need to override (override) this method is enough.

public class SampleClass{   private string myString;   public SampleClass(string str)   {      this.myString = str;   }   public int IndexOf(string s)   {      return IndexOf (s, 0);   }   public int IndexOf(string s, int startIndex)   {      return IndexOf(s, startIndex, myString.Length - startIndex );   }   public virtual int IndexOf(string s, int startIndex, int count)   {      return myString.IndexOf(s, startIndex, count);   }}

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.