Reputation: 313
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?
Upvotes: 3
Views: 135
Reputation: 7943
They both basicaly do the same thing. If you look at it, you always have to call the constructor regardless.
The second one calls the default constructor. It is then responsible for setting up the variables. The first, initializes the variables inside the constructor.
The first is your best and foremost option in any scenario. It is always best to let the object initialize the variables through the constructor. With the second example, the initialization is left to the implementation of the class that created this object, which may not yield the proper results you want from a class.
For instance, in the second example I do:
Customer Costomer1 = new Customer() { Name = "Mark", Salary = 5000, };
Now I do not have an ID. The constructor parameters will force the implementer to abide by it's rules. No matter what, you cannot omit the variables passed into the constructor, unless you put in empty variables (ie. "").
Your second example is best when I want to short hand initialize assign variables that are not accepted by the constructor. Basically, as far as I know (I could be terribly wrong), the second does this:
Customer Costomer1 = new Customer();
Customer1.ID = 100;
Customer1.Name = "Mark";
Customer1.Salary = 500;
think about the second example as pure short hand.
When constructing a class, you must think about what are the variables that make this class work. For instance, a customer CANNOT not have an ID, though they may not have a defined salary. If I do not include the ID in my customer by mistake, say it is 0, then you can have some very strange behavior as the object get's used.
Consider this scenerio.
Customer Costomer1 = new Customer();
Customer1.ID = 100;
Customer1.Name = "Mark";
Customer1.Salary = 500;
Customer Costomer2 = new Customer();
Customer1.Name = "John";
Customer1.Salary = 500;
Customer Costomer3 = new Customer();
Customer1.Name = "Eric";
Customer1.Salary = 500;
now, you have two customers with a 0 for the ID number. While you may by mistake set this to the same value through a constructor, setting the value in the constructor FORCES you to consider the ID number.
The other thing is, when for instance I take your code and I use it for my project. Say I am using your code to extend a program that you have already written. I may may or not be aware of the fact that customers must have ID numbers. Or, more likely, I FORGET to initialize the name and ID number. Your entire system now may exhibit strange behavior as a result.
But speaking purely, out of experience, forcing yourself or your implementer to set certain variables hides the logic into one location. I no longer have to have long strands of code initializing each and every variable that is absolutely necessary for this object. The rule is, normally, that you take any kind of logic that can be repeated and put it into one place.
Last, but not least, you don't usually want to expose writable variables like this. That is, important variables that shouldn't be tampered with. Using a constructor, you can initialize a class with an ID number and hide it inside your class as a private variable. This way, the implementer cannot, by any means, morph the ID number to something else. In my experience this helps to stop people "hot fixing" variables to give them some kind of outcome that they can't figure out how to do normally. This can lead to some very odd behavior in your program.
You can actually have the best of both worlds. Consider this:
class MyClass
{
private int someVariable;
public int someOtherVariable;
public MyClass(int myVar)
{
someVariable = myVar;
}
}
and to initialize:
MyClass c = new MyClass(5) { someOtherVariable = 10 };
Upvotes: 1
Reputation: 204
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;
Upvotes: 5
Reputation: 2771
The first one requires you to have parameters (or optional parameters) in the construction of your object. The second option allows you to initialize the properties of the object as needed. If you do not require the properties to be initialized then the second option is the better approach.
Upvotes: 1
Reputation: 180948
In the second option, unless you add validation, it is possible to leave out a parameter and have an incomplete instance, whereas in the first option, all parameters are required, so it's not possible to have a partially constructed object.
The second version can be more readable if there are several parameters, although you can use named parameters in the first version if you're using C# 4.
The second version is less work to code, but the first version is easier to get right.
Upvotes: 4