Overloading

What Does Overloading Mean?

Overloading refers to the ability to use a single identifier to define multiple methods of a class that differ in their input and output parameters. Overloaded methods are generally used when they conceptually execute the same task but with a slightly different set of parameters.

Advertisements

Overloading is a concept used to avoid redundant code where the same method name is used multiple times but with a different set of parameters. The actual method that gets called during runtime is resolved at compile time, thus avoiding runtime errors. Overloading provides code clarity, eliminates complexity, and enhances runtime performance.

Techopedia Explains Overloading

Overloading is used in programming languages that enforce type-checking in function calls during compilation. When a method is overloaded, the method chosen will be selected at compile time. This is not the same as virtual functions where the method is defined at runtime.

Unlike Java, C# allows operators to be overloaded, in addition to methods, by defining static members using the operator keyword. This feature helps to extend and customize the semantics of operators relevant to user-defined types so that they can be used to manipulate object instances with operators.

The overload resolution in C# is the method by which the right function is selected on the basis of arguments passed and the list of candidate function members that have the same name. The different contexts in which the overload resolution is used include:

  • Invocation of a method in an expression
  • Constructor during object creation
  • Indexer accessor through an element access and predefined or user-defined operator expression

It is recommended to avoid overloading across inheritance boundaries because it can cause confusion. Overloading can become cumbersome to developers if it is used excessively and with user-defined types as parameters because it can reduce the readability and maintainability of code.

Advertisements

Related Terms

Margaret Rouse
Technology Expert

Margaret is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical business audience. Over the past twenty years, her IT definitions have been published by Que in an encyclopedia of technology terms and cited in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine, and Discovery Magazine. She joined Techopedia in 2011. Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages.