A Class with Member Methods - CSharp Custom Type

CSharp examples for Custom Type:Method

Description

A Class with Member Methods

Demo Code

class Circle/*  ww w .  j a  va  2 s.c om*/
{
   public int x;
   public int y;
   public double radius;
   public double area()
   {
      double theArea;
      theArea = 3.14159 * radius * radius;
      return theArea;
   }
   public double circumference()
   {
      double theCirc;
      theCirc = 2 * 3.14159 * radius;
      return theCirc;
   }
}
class CircleApp
{
   public static void Main()
   {
      Circle first  = new Circle();
      Circle second = new Circle();
      double area, circ;
      first.x = 10;
      first.y = 14;
      first.radius = 3;
      second.x = 10;
      second.y = 11;
      second.radius = 4;
      System.Console.WriteLine("Circle 1: Center = ({0},{1})", first.x, first.y);
      System.Console.WriteLine("          Radius = {0}", first.radius);
      System.Console.WriteLine("          Area   = {0}", first.area());
      System.Console.WriteLine("          Circum = {0}", first.circumference());
      area = second.area();
      circ = second.circumference();
      System.Console.WriteLine("\nCircle 2: Center = ({0},{1})", second.x, second.y);
      System.Console.WriteLine("          Radius = {0}", second.radius);
      System.Console.WriteLine("          Area   = {0}", area);
      System.Console.WriteLine("          Circum = {0}", circ);
   }
}

Result


Related Tutorials