Whats the difference between using constructor when instantiating with passing arguments:
Customer Costomer1 = new Customer(100, Mark, 5000);
And doing the same but without passing anything into constructor but simply instantiating members?
Customer Costomer1 = new Customer() { ID = 100, Name = "Mark", Salary = 5000, };
Which one is better and for what situations are they are good?
Will I be correct if I will say that constructor is when more work needs to be done when instantiating an object and member initializing is for a signing values for fields and properties only?
And if Im understanding this correctly why would you use second case with members if you can use constructor?
this is a syntax shortcut(will call default constructor)
Customer Costomer1 = new Customer() { ID = 100, Name = "Mark", Salary = 5000, };
equals to the follwing
Customer Costomer1 = new Customer()
Costomer1.ID = 100;
Costomer1.Name = "Mark";
Costomer1.Salary = 5000;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With