Illustrates the use of a struct : struct : Class Interface C# Source Code


Custom Search

C# Source Code » Class Interface » struct »

 

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());

  }

}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Class Interface
» struct