Method Overloading - CSharp Custom Type

CSharp examples for Custom Type:overload

Description

Method Overloading

Demo Code

using System;//from  ww  w .ja  va  2  s .  co m
public class Circle
{
    public int x;
    public int y;
    public double radius;
    private const float PI = 3.14159F;
    public double Area()  // Uses values from data members
    {
        return Area(radius);
    }
    public double Area(double rad)
    {
        double theArea;
        theArea = PI * rad * rad;
        Console.WriteLine("  The area for radius ({0}) is {1}", rad, theArea);
        return theArea;
    }
    public double Area(int x1, int y1, double rad)
    {
        return Area(rad);
    }
    public double Area(int x1, int y1, int x2, int y2)
    {
        int x_diff;
        int y_diff;
        double rad;
        x_diff = x2 - x1;
        y_diff = y2 - y1;
        rad = (double)Math.Sqrt((x_diff * x_diff) + (y_diff * y_diff));
        return Area(rad);
    }
    public Circle()
    {
        x = 0;
        y = 0;
        radius = 0.0;
    }
}
class MainClass
{
    public static void Main()
    {
        Circle myCircle = new Circle();
        myCircle.Area();
        Console.WriteLine("\nPassing a radius of 3...");
        myCircle.Area(3);
        Console.WriteLine("\nPassing a center of (2, 4) and a radius of  3...");
        myCircle.Area(2, 4, 3);
        Console.WriteLine("\nPassing center of (2, 3) and a point of (4, 5)...");
        myCircle.Area(2, 3, 4, 5);
    }
}

Result


Related Tutorials