Method overloading in C#

In method overloading, we can have different number of function arguments with different data types, however, the function name must be same. Moreover, the return type of function does not count in method overloading

The following example demonstrates how we can perform method overloading in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{   
    class Program
    {
   
        static void Main(string[] args)
        {
            DisplayResult(20);
            DisplayResult("pakistan");

        }

        static void DisplayResult(string result)
        {
            Console.WriteLine(result);
        }

        static void DisplayResult(int result)
        {
            Console.WriteLine(result);
        }

        /*
         * below call is incorrect as return type does not
         * matter in method overloading.
        static int DisplayResult(int result)
        {
            return result * 100;
        }
        */    
   }
}

 

Add a Comment

Your email address will not be published. Required fields are marked *