I read a lot about why constructors are useful, and all the resources found indicate that constructors are used to initialize instances of your classes. A key advantage of using a constructor is that it ensures that the object goes through the correct initialization before use, often taking parameters. This helps ensure the integrity of the object and helps make applications written with object-oriented languages more reliable.
By default, C # creates an empty default constructor if no constructors are specified in the class.
Most of the examples I find point to something like this:
public Car(int speedCurrent, int gearCurrent) { speed = speedCurrent; gear= startGear; } Car myCar = new Car(0, 0);
Now, what is the practical point of creating a constructor, when you can specify properties,
public int speed { get; set; } public int gear { get; set; }
And initialize it like this:
Car myCar = new Car(); myCar.speed = 0; myCar.gear = 0;
I cannot bow my head to having to explicitly create a constructor. I would appreciate if someone would give me a good practical example.
source share