Declare the Rectangle struct : struct definition « struct « C# / CSharp Tutorial






public struct Rectangle
{
  // declare the fields
  public int Width;
  public int Height;

  // define a constructor
  public Rectangle(int Width, int Height)
  {
    this.Width = Width;
    this.Height = Height;
  }

  // define the Area() method
  public int Area()
  {
    return Width * Height;
  }

}


class MainClass
{

  public static void Main()
  {
    System.Console.WriteLine("Creating a Rectangle instance");
    Rectangle myRectangle = new Rectangle(2, 3);

    System.Console.WriteLine("myRectangle.Width = " + myRectangle.Width);
    System.Console.WriteLine("myRectangle.Height = " + myRectangle.Height);
    System.Console.WriteLine("myRectangle.Area() = " + myRectangle.Area());
  }
}
Creating a Rectangle instance
myRectangle.Width = 2
myRectangle.Height = 3
myRectangle.Area() = 6








6.2.struct definition
6.2.1.Declare the Rectangle struct
6.2.2.Declaration of a struct and use it
6.2.3.Declare a struct
6.2.4.Use enum data type in a struct
6.2.5.Composite Struct
6.2.6.Create A Struct Using Default Constructor
6.2.7.Defining struct