C# Overloading

  • C# overloading means the methods or constructors can be defined with the same name but different numbers or types of parameters.
  • It is good to define a virtual longest overload member with the maximum number of parameters and the other short overloads will call the longer one instead of creating from scratch.
  • Avoid using the similar type at the same location of parameters and ref out keyword in parameters for overloading.
  • Return type in methods is not part of overloading. You will get an error message if you try to define 2 methods with the same signature but different return types.

For example, many overloaded methods WriteLine() are defined in class Console in System namespace.

public static void WriteLine();
public static void WriteLine(bool value);
public static void WriteLine(char value);
public static void WriteLine(string value);
......

In the above overloaded methods, the numbers and types of the parameters will decide which method will be invoked. For details, please go to MSDN Console.WriteLine method page.

Example 01-64-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;

class Rectangle
{
    public virtual string getArea(string name, double length, double width)
    {
        if (name == null || name == "")
        {
            name = "Empty";
        }

        return "Name: " + name + "\tArea: " + (length * width).ToString();
    }

    public string getArea(double length, double width)
    {
        return getArea(null, length, width);
    }

    public string getArea(string name, double side)
    {
        return getArea(name, side, side);
    }

    public string getArea(double side)
    {
        return getArea(null, side);
    }

    static void Main()
    {
        Rectangle r = new Rectangle();
        Console.WriteLine(r.getArea("Rect A", 10, 5));
        Console.WriteLine(r.getArea(10, 5));
        Console.WriteLine(r.getArea("Square B", 7));
        Console.WriteLine(r.getArea(7));

        Console.Read();
    }
}

Output

Name: Rect A	Area: 50
Name: Empty	Area: 50
Name: Square B	Area: 49
Name: Empty	Area: 49

Explanation

  • Line 3-40: Define a class Rectangle.
  • Line 5-13: Define a public virtual method getArea with 3 parameters.
  • Line 12: Return a string with name and area.
  • Line 15-18: An overloading method without name.
  • Line 20-23: An overloading method returning the square area.
  • Line 25-28: Overload the method in line 20-23.
  • Line 30: Main method is defined to test the overloaded methods.
  • Line 32: Create an instance.
  • Line 33-36: Call each overloaded method for testing and the strings were printed as expected.

In the above example, only the first getArea method was defined as virtual and all the others were defined by calling this method. This is a good design. Only this virtual method can be overridden if the class is inherited and the other overloading methods don't need to be changed in the derived class.

We actually had the constructor overloading example. For details, you can check the Example 01-50-02 in C# Constructors section.