Method Overloading in C#

This article provides an explaination of one of the most important concepts of C# or any Other Object Oriented language: Method Oveloading and its application with an example. I'll also explain various methodologies of accessing the functionality of method overloading.

Method Overloading

Example

There can be several existing methods of declaring or accessing the functionality of method overloading but this article will make you familiar with two of those methodologies.

Here we go.

Methodology 1

In this methodology I am declaring 3 public classes and accessing their functionality below in the other class.

classes

When I move to the Program class, to access an overloaded property defined in the first class, I have 3 options available for accessing the overloading functionality.

These 3 options are:
 
accessing overloading functionality

Coding
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Hello_Word

{

    class Book

    {      

        public string Name;

        public string Language;

        public decimal Prize;    

 

        public Book(string s)

        {

            Name = s;

        }

 

        public Book(string s, string S)

        {

            Name = s;

            Language = S;

        }

 

        public Book(string s, string S, decimal d)

        {

            Name = s;

            Language = S;

            Prize = d;

        }

 

        class Program

        {

            private static string JAVA;

            private static string CSharp;

            static void Main(string[] args)

           {

                Book b1 = new Book("Black Book");

                Book b2 = new Book("Complete Reference",JAVA);

                Book b3 = new Book("Programming in C#", CSharp, 250.00m);

                Console.WriteLine("{0}", b1.Name);

                Console.WriteLine("{0} {1}", b2.Name, b2.Language );

               Console.WriteLine("{0} {1} {2}", b3.Name, b3.Language, b3.Prize);

               Console.ReadLine();

           }       

        }

    }

}

 
Method Overloading 

Methodology: 2

In this methodology I am declaring input and then getting output using method overloading.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Hello_Word

{

    class overloding

    {

        public static void Main()

        {

            Console.WriteLine(volume(10));

            Console.WriteLine(volume(2.5F, 8));

            Console.WriteLine(volume(100L, 75, 15));

            Console.ReadLine();

        }

 

        static int volume(int x)

        {

            return (x * x * x);

        }

 

        static double volume(float r, int h)

        {

            return (3.14 * r * r * h);

        }

 

        static long volume(long l, int b, int h)

        {

            return (l * b * h);

        }

    }

}

 
output