Here is our Box class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassExamples
{
public class Box
{
//fields
private double height;
private double width;
private double depth;
//default constructor
//a constructor has the same name
//as the class and no return type
public Box()
{
Height = 0;
Width = 0;
Depth = 0;
}
//overloaded constructor
public Box(double h, double w, double d)
{
Height = h;
Width = w;
Depth = d;
}
#region properties
public double Width
{
get { return width; }
set { width = value; }
}
public double Depth
{
get { return depth; }
set { depth = value; }
}
public double Height
{
get
{
return height;
}
set
{
height = value;
}
}
#endregion
//class method
public double CalculateVolume()
{
return height * width * depth;
}
}
}
Here is the main class where we called the Box class and assigned properties
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassExamples
{
class Program
{
static void Main(string[] args)
{
//Console.BackgroundColor = ConsoleColor.DarkMagenta;
//Console.WriteLine("Hello");
//initialize the new box class
//using the default constructor
Box b = new Box();
//set the property values
b.Height = 5.1; //all use the set of the property
b.Depth = 6;
b.Width = 4;
//call the method
double volume = b.CalculateVolume();
//these are using the get part of the properties
Console.WriteLine("The volume of a box with a height of {0} and a width of {1} and a depth of {2} has a volume of {3} cubic feet", b.Height, b.Width, b.Depth, volume);
//initialize a second Box instance
//useing the overloaded constructor
Box b2 = new Box(4, 5, 2);
Console.WriteLine("The volume of box 2 is {0} cubic feet", b2.CalculateVolume());
Console.ReadKey();
}
}
}