Programming, Investing and FI

Visual Basic/C# – What Is Method Overloading?

Method overloading is a simple and very useful concept in computer programming. We’re in luck because VB.Net and C# can do method overloading!

What is method overloading?

Basically, it’s writing two or more methods with the same name but with a different signature. This means that if two or more methods have pretty much the same behavior, it’s possible to simply give them the same name but different return value and parameters. Let me show you an example.

Example Please!

I created a simple class named Calculator in a new Console Application project.

C#

using System.Collections.Generic;

namespace MethodOverloading
{
    public class Calculator
    {
        public Queue LastResults { get; set; }

        public Calculator()
        {
            LastResults = new Queue();
        }

        public int AddNumbers(int NumberOne, int NumberTwo)
        {
            int Result = NumberOne + NumberTwo;

            return Result;
        }

        public double AddNumbers(double NumberOne, double NumberTwo)
        {
            double Result = NumberOne + NumberTwo;

            return Result;
        }
    }
}

Visual Basic

Public Class Calculator
    Public Property LastResults As Queue(Of Double)

    Public Sub New()
        LastResults = New Queue(Of Double)
    End Sub

    Public Function AddNumbers(ByVal NumberOne As Integer, ByVal NumberTwo As Integer) As Integer
        Dim Result As Integer
        Result = NumberOne + NumberTwo

        Return Result
    End Function

    Public Function AddNumbers(ByVal NumberOne As Double, ByVal NumberTwo As Double) As Double
        Dim Result As Double
        Result = NumberOne + NumberTwo

        Return Result
    End Function
End Class

In this class you can see that I’ve added a method with the same name twice. This is what overloading is about. The name is the same but the signature is not. The first one returns int and takes (int,int) as parameters while the second one returns double and takes (double,double) as parameters. LastResults is just a class variable I added to make it sound a bit more like it’s going to be an actual calculator.

How to Use

My Program.cs/MethodOverloading.vb class looks like the code below.

C#

Overloads Add Number

Visual Basic

Add Numbers Overload VB.Net

What I want to show you is that when you instantiate a new Calculator and call the AddNumbers method, you actually see two overloads (the current one +1 as written in the completion)! That’s exactly what we want. I then have the choice of both methods to use.

C#

using System;

namespace MethodOverloading
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator MyCalculator = new Calculator();

            double DoubleResult = MyCalculator.AddNumbers(15.32, 64.21);
            int IntResult = MyCalculator.AddNumbers(67, 98);

            Console.WriteLine("The Double Result: {0}", DoubleResult);
            Console.WriteLine("The Int Result: {0}", IntResult);
            Console.ReadKey();
        }        
    }
}

Visual Basic

Module MethodOverloading

    Sub Main()
        Dim MyCalculator As New Calculator()

        Dim DoubleResult As Double
        DoubleResult = MyCalculator.AddNumbers(15.32, 64.21)

        Dim IntResult As Integer
        IntResult = MyCalculator.AddNumbers(67, 98)

        Console.WriteLine("The Double Result: {0}", DoubleResult)
        Console.WriteLine("The Int Result: {0}", IntResult)
        Console.ReadKey()
    End Sub

End Module

In the code above, I use both methods and display the result in the console.

Method Overloading Result

It Works!

As you can see, one method was called with double values and returned a double and the other one with int values and returned an int. Two methods, same name, different signatures. You will see this on multiple occasions in the .Net Framework and should take advantage of it in your own code as well!

When you see two method that do very similar behavior enough to give them the same name, that’s a potential candidate for overloading.

Youtube

C#

Visual Basic

Next article Cash Versus Investing Experiment
Previous article Why lottery Is Not An Investment

Related posts