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