Define Rectangle class with method to compute the area : Member Method « Class « C# / CSharp Tutorial






using System; 
 
class Rect { 
  public int width; 
  public int height; 
 
  public Rect(int w, int h) { 
    width = w; 
    height = h; 
  } 
 
  public int area() { 
    return width * height; 
  } 
} 
  
class UseRect { 
  public static void Main() {   
    Rect r1 = new Rect(4, 5); 
    Rect r2 = new Rect(7, 9); 
 
    Console.WriteLine("Area of r1: " + r1.area()); 
 
    Console.WriteLine("Area of r2: " + r2.area()); 
 
  } 
}
Area of r1: 20
Area of r2: 63








7.4.Member Method
7.4.1.Define methods
7.4.2.Member Functions
7.4.3.Nested methods
7.4.4.Return a value from a member method
7.4.5.Define Rectangle class with method to compute the area
7.4.6.Object Class Methods