C#: Method overloading

This entry is part 33 of 71 in the series C#

Method overloading is very common in object-oriented programming.  This technique is commonly used in C++, Java and C#.

We can overload a method based on the number of parameters or the type of parameters.

As only methods, constructors, and indexed properties can have parameters, only those members can be overloaded.

We take a simple WriteLine example from C#.  As WriteLine is overloaded, we are able to print out the different types of values passed into the method.

using System;

namespace Method
    class Program
        static void Main()
            Console.WriteLine("This is a string");  //This is a string
            Console.WriteLine(16);  //This is an integer
            Console.WriteLine(80.81);  //This is a double
            Console.WriteLine(true);  //This is a boolean value

The WriteLine method declaration is likely to be similar to below:

public static class Console {
    public void WriteLine();
    public void WriteLine(string value);
    public void WriteLine(bool value);

Overloading is one of the most important techniques for improving usability, productivity, and readability of reusable libraries.

Series Navigation<< C#: Constructor overloading
C#: Main method and its arguments args >>

Leave a comment

Leave a Reply