Classes and Objects
Constructors
A constructor initializes an object when it is created.
constructor
A constructor runs when `new` creates an object.
Constructors
Constructors.cs
Replay: real traced execution (multi-file project)
using System;
class Rectangle
{
public int Width;
public int Height;
public Rectangle(int width, int height)
{
Width = width;
Height = height;
}
public int Area()
{
return Width * Height;
}
}
class Program
{
static void Main()
{
int width = 5;
Rectangle box = new Rectangle(width, 4);
int area = box.Area();
Console.WriteLine($"width={box.Width}");
Console.WriteLine($"area={area}");
}
}
using System;
class Rectangle
{
public int Width;
public int Height;
public Rectangle(int width, int height)
{
Width = width;
Height = height;
}
public int Area()
{
return Width * Height;
}
}
class Program
{
static void Main()
{
int width = 3;
Rectangle box = new Rectangle(width, 4);
int area = box.Area();
Console.WriteLine($"width={box.Width}");
Console.WriteLine($"area={area}");
}
}
using System;
class Rectangle
{
public int Width;
public int Height;
public Rectangle(int width, int height)
{
Width = width;
Height = height;
}
public int Area()
{
return Width * Height;
}
}
class Program
{
static void Main()
{
int width = 7;
Rectangle box = new Rectangle(width, 4);
int area = box.Area();
Console.WriteLine($"width={box.Width}");
Console.WriteLine($"area={area}");
}
}
width ← 5
21{22 static void Main()23 {24 int width→ 5 = 5; //@width=3, 725 Rectangle box = new Rectangle(width, 4);26 int area = box.Area();public Rectangle(int width, int height)
8public Rectangle(int width5, int height4)9{10 Width = width5;11 Height = height4;12}box ← Rectangle
24int width = 5; //@width=3, 725Rectangle box→ Rectangle = new Rectangle(width, 4);26int area = boxRectangle.Area();area ← 20
25 Rectangle box = new Rectangle(width, 4);26 int area→ 20 = boxRectangle.Area();2728 Console.WriteLine($"width={box.Width5}");29 Console.WriteLine($"area={area20}");30}outputwidth=5 area=20
width ← 3
21{22 static void Main()23 {24 int width→ 3 = 3;25 Rectangle box = new Rectangle(width, 4);26 int area = box.Area();public Rectangle(int width, int height)
8public Rectangle(int width3, int height4)9{10 Width = width3;11 Height = height4;12}box ← Rectangle
24int width = 3;25Rectangle box→ Rectangle = new Rectangle(width, 4);26int area = boxRectangle.Area();area ← 12
25 Rectangle box = new Rectangle(width, 4);26 int area→ 12 = boxRectangle.Area();2728 Console.WriteLine($"width={box.Width3}");29 Console.WriteLine($"area={area12}");30}outputwidth=3 area=12
width ← 7
21{22 static void Main()23 {24 int width→ 7 = 7;25 Rectangle box = new Rectangle(width, 4);26 int area = box.Area();public Rectangle(int width, int height)
8public Rectangle(int width7, int height4)9{10 Width = width7;11 Height = height4;12}box ← Rectangle
24int width = 7;25Rectangle box→ Rectangle = new Rectangle(width, 4);26int area = boxRectangle.Area();area ← 28
25 Rectangle box = new Rectangle(width, 4);26 int area→ 28 = boxRectangle.Area();2728 Console.WriteLine($"width={box.Width7}");29 Console.WriteLine($"area={area28}");30}outputwidth=7 area=28
Follow the Constructor
widthstarts as5.new Rectangle(width, 4)calls the constructor.- The constructor stores
Width = 5. - It also stores
Height = 4. box.Area()returns5 * 4, soareais20. | property | value | | --- | --- | |box.Width| 5 | |box.Height| 4 | |area| 20 |
Exercise: Constructors.cs
Reproduce width=5 and area=20, then use the pinned width variants 3 and 7 to predict area=12 and area=28.