C# Methods, part 1 - Methods Overloading
(Page 6 of 6 )
Method overloading is an Object oriented Design Principle. Sometimes you will have a behavior that you need to encapsulate inside a method that takes two parameters of type integer, and at another point in your application you need another method to concatenate two strings, and it's nice to name it. Ahh, but how?
In Object Oriented Programming, you can overload the method with another variation, which means that you will provide another method (normally with another implementation) with the same name, but it MUST differ in the number of the parameters, or it can differ in the parameter's data type, or maybe the parameter's order, so it's a way for the C# Compiler to differentiate between the method variations.
You can't use the method return type to make a new variation of the method, and you also can't overload a method with a property, because they are two different class members. When you use Visual Studio.NET, you can see how its intelliSense feature displays the method overloads; it will display the current method variation's parameter list with a number to indicate how many variations of this method exist.
The following screen shot illustrates the Visual Studio.NET intelliSense feature of the method Console.WriteLine()

There are 19 variations of the method WriteLine and, as you can see, there are arrows so that you can navigate through the variations and learn more about them. Also, there is a description for each parameter along with its data type. So let's overload the Add method.
using System;
namespace Methods
{
class Class1
{
static void Main(string[] args)
{
Console.WriteLine(Number.Add(4,5));
Console.WriteLine(Number.Add(5,6,7));
Console.WriteLine(Number.Add(1d,3d));
Console.WriteLine(Number.Add(2d,3d,6d));
Console.WriteLine(Number.Add("Hello","Overloading"));
Console.ReadLine();
}
}
public class Number
{
public static int Add(int x, int y)
{
return x + y;
}
public static int Add(int x, int y,int z)
{
return x + y + z;
}
public static double Add(double x, double y)
{
return x + y;
}
public static double Add(double x, double y, double z)
{
return x + y + z;
}
public static string Add(string x, string y)
{
return x +" "+ y;
}
}
}
The result is:

As you can see, the code has been compiled, and we have five different versions of the Add method that differ in the parameter's data type, and some of them in the parameter's number. Note the use of the d letter after the value to denote a double literal value, so it can be differentiated from integer values, and so on.
In the second part, after we discuss passing parameters by value and by reference, we will see that ref and out keyword can't overload a method.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |