I want to know about function overloading in C# & how to do it?

Can anyone tell me with a simple example.

Since as i read it from a book i came to this conclusion.
I thought it happens only when you use 1 function with same name & tried with an program.

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

namespace Area_program
{
    class Program
    {
        static void Main(string[] args)
        {
            double h, b;
            double area;
            Console.Write("Enter height of triangle : ");
            h = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter base of triangle : ");
            b = Convert.ToDouble(Console.ReadLine());
            if (h != 0 && b != 0)
            {
                area = Area(h, b);
                Console.WriteLine("Area is : " + area + " sq. units");
            }
            else
                Console.WriteLine("either base or height you have entered is zero");
            Console.WriteLine("              Made by Vinod Bhosale");
            Console.ReadKey();
        }
        static int Area(int n1, int n2)
        {
            Console.WriteLine("Inside Area(int n1, int n2)");
            int area;
            area = (n1 * n2) / 2;
            return area;
        }
        static double Area(double n1, int n2)
        {
            Console.WriteLine("Inside Area(double n1, int n2)");
            double area;
            area = (n1 * n2) / 2;
            return area;
        }
        static double Area(int n1, double n2)
        {
            Console.WriteLine("Inside Area(int n1, double n2)");
            double area;
            area = (n1 * n2) / 2;
            return area;
        }
        static double Area(double n1, double n2)
        {
            Console.WriteLine("Inside Area(double n1, double n2)");
            double area;
            area = (n1 * n2) / 2;
            return area;
        }
    }
}

I know as i am sending any value it is already in double format.
Then how i can send it thorugh other methods such Area(int n1, int n2) or Area(int n1, double n2) or Area(double n1, int n2).
Since i have tried it for hours but not finding the answer.

Can anyone tell me how operator overloading works

The main purpose for method overloading is doing essentially the same thing with different types. Each method must have a unique "signature". The signature is made up of the number, order and type of the parameters (the return value doesn't matter).

In your sample program, you could call another one of the methods by casting one or both of the variables to an int:

area = Area((int)h, b);

The compiler will try to match the given parameters (based on their types) with the best method (closest signature to your input). It generally has to match exactly, but will perform implicit casts if necessary. In your program, the compiler sees that both input parameters are doubles, so it calls Area(double,double). If you took out that method, it would not call any of the other ones, since you cannot implicitly cast an int to a double.

Comments
simple and clear explanation

but what if i want to use any of the four methods with different parameter

if it is area = Area((int)h, b); then it will only call Area(int n1, double n2) method

What if i want to use other methods how can I use it

Do you mean you want to call multiple methods with a signle method call? That is not the intention of method overloading. It is mainly a convenience. It would be equivalent to something like this:

        static int AreaII(int n1, int n2)
        {
            Console.WriteLine("Inside Area(int n1, int n2)");
            int area;
            area = (n1 * n2) / 2;
            return area;
        }
        static double AreaDI(double n1, int n2)
        {
            Console.WriteLine("Inside Area(double n1, int n2)");
            double area;
            area = (n1 * n2) / 2;
            return area;
        }
        static double AreaID(int n1, double n2)
        {
            Console.WriteLine("Inside Area(int n1, double n2)");
            double area;
            area = (n1 * n2) / 2;
            return area;
        }
        static double AreaDD(double n1, double n2)
        {
            Console.WriteLine("Inside Area(double n1, double n2)");
            double area;
            area = (n1 * n2) / 2;
            return area;
        }

Here you would explicitly call the method you want. With method overloading, the compiler does that for you; it implicitly calls one of the methods.

Hope this clears things up a bit.

You would use one of the other methods by passing it the parameter types that it expects. If you want to use the method that takes two integers, then pass it two integers. If you want to use the method that takes two doubles, then pass it two doubles. The types of the parameters will determine which of the methods gets called.

As question was only to do function overloading by finding area of triangle

I used Area(2,4) Area(2.1,4) Area(2,3.9) Area(2.1,3.9)

It used all functions but thanx to everyone who helped me.

This question has already been answered. Start a new discussion instead.