Sponsored Ad

Monday, May 24, 2010

C# Function Overloading

In C# we can have function with the same name, but we have to take different data type. We called the function by the same name as by passing different parameters and a different function gets called. That’s Process called function overloading .In the following code we have three function and all of having same name aaa.

 

C# Function Overloading Examples

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{

    class xxx
    {
        public static void Main()
        {
            zzz a = new zzz();
            a.function(10);
            a.function("Hello");
            a.function("Number", 100);
        }
    }
    class zzz
    {

        public void function(int i)
        {
            System.Console.WriteLine("Name" + i);
        }

        public void function(string i)
        {
            System.Console.WriteLine("Name" + i);
        }

        public void function(string i, int j)
        {
            System.Console.WriteLine("Name" + i + j);
            System.Console.ReadLine();
        }
    }
}

Output

 

C# Function Overloading

2 comments:

  1. how we camn perform calculation using function overloading.

    ReplyDelete
  2. Suppose you want two methods to sum values

    like
    int sum(int a, int b)
    and
    int sum(int a, int b, int c)

    ReplyDelete

Sponsored Ad

Development Updates