Mega Code Archive

 
Categories / C# / Class Interface
 

Illustrates the use of a struct

/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /*   Example5_15.cs illustrates the use of a struct */ // declare the Rectangle struct 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;   } } public class Example5_15 {   public static void Main()   {     // create an instance of a Rectangle     System.Console.WriteLine("Creating a Rectangle instance");     Rectangle myRectangle = new Rectangle(2, 3);     // display the values for the Rectangle instance     System.Console.WriteLine("myRectangle.Width = " + myRectangle.Width);     System.Console.WriteLine("myRectangle.Height = " + myRectangle.Height);     // call the Area() method of the Rectangle instance     System.Console.WriteLine("myRectangle.Area() = " + myRectangle.Area());   } }