1
Answer

why we use constructor with parameters to initialize values

Photo of Karthikeyan K

Karthikeyan K

9y
342
1
 I'm a bit confused about the use of getter/setters and constructors.why we use constructor with parameters to initialize values.

Answers (1)

0
Photo of Afzaal Ahmad Zeeshan
NA 36k 2m 9y
There is no benefit or difference of them, other than that they allow us to update the values of the fields before hand. For example, we may do this:
var person = new Person();
person.Name = "Afzaal Ahmad Zeeshan"; 
person.Age = 20;
But, using the parameters with constructors allows you to pass the values at the object construction,
var person = new Person("Afzaal Ahmad Zeeshan", 20); 
Because rest of the stuff would be handled by the constructor itself:
public Person(string name, int age) {
     Name = name;
     Age = age; 
Otherwise, there is no difference in them. Plus, there is a bit of stuff for "overloading" going on. You must understand that Overloading is a concept of Object-oriented programming. I have an article about overriding and overloading, that may help you a bit more about this. 
 
http://www.c-sharpcorner.com/UploadFile/201fc1/still-do-not-get-the-difference-of-overloading-and-overridin/