I am following c# complete reference
. Following is the example that demonstrates Constructor and inheritance
,
class TwoDShape {
double pri_width;
double pri_height;
// Properties for Width and Height.
public double Width {
get { return pri_width; }
set { pri_width = value < 0 ? -value : value; }
}
public double Height {
get { return pri_height; }
set { pri_height = value < 0 ? -value : value; }
}
class Triangle : TwoDShape {
string Style;
// Constructor.
public Triangle(string s, double w, double h) {
Width = w;
Height = h;
Style = s;
}
Now in Program.cs
inside Main() i have
static void Main() {
Triangle t1 = new Triangle("isosceles", 4.0, 4.0);
Triangle t2 = new Triangle("right", 8.0, 12.0);
}
My question is that when we are using properties and can initialize our fields using them why we use constructor and then inside this Constructor fields are initialized.
Which is better approach use construct / properties or this approach. Please also explain this approach as i am unable to get this logic (both constructor and properties for initialization)