Reputation: 5735
What is the added value of using an object initializer? Is there any difference using it on value types compared to reference types?
I have installed ReSharper recently, and for the following example:
var response = new Response();
response.Value = "My value";
My code is transformed to this:
var response = new Response()
{
Value = "My value",
};
Personally I find it harder to follow the code when the initializer is too big.
Upvotes: 0
Views: 46
Reputation: 62248
No there is no any difference in this cases between reference
or value
types.
Object initializer is a fancy way of init objects in one code line, when initilization is short.
More convenient it becomes during multithreading when you need to be sure that one time that line executed, your object is initilized, or in valid state for your program.
But basically these all melts down to coding style and personal convenience.
Upvotes: 3