0

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.

Shachaf.Gortler
  • 5,655
  • 14
  • 43
  • 71
  • 2
    On your second code snippet, you probably meant `value = ...` instead of `response.value = ...`. Also, the fact that you don't have to repeat `response.` over and over again for each initialized property, increases readability, IMO. – dcastro Jul 09 '14 at 10:26

1 Answers1

3

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.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • I had upvoted you until I read what you said about multithreading. There's nothing to guarantee that all the initializations will occur atomically. – John Saunders Jul 09 '14 at 11:00
  • @JohnSaunders Although his explanation was vague, he's right. Section 7.6.10.2 of the spec says that the OP's second code snippet will be translated to: 1) Instantiate a new instance of type `Response` and assign it to a temporary variable named `__response`, 2) set the properties on `__response`, 3) copy the reference to `response`, i.e., `response = __response`. What this means is that the object won't be *published* until all initializations have occurred, which might have an impact on multithreading scenarios. This also means the OP's first and second code snippets aren't equivalent. – dcastro Jul 09 '14 at 11:10
  • @dcastro: interesting. I didn't know that. So `response` will be either `null` or else fully initialized. Is that only for local variables? Any way that different threads running the same code could initialize to different values? – John Saunders Jul 09 '14 at 11:14
  • @JohnSaunders This goes for fields as well. There's no benefit in not publishing the object until all the properties are set if it's a local variable - this will only make a difference if `response` is shared state. I didn't understand your second question, can you please rephrase it? – dcastro Jul 09 '14 at 11:21
  • @JohnSaunders: if you mean that there is no gurantee that the line code will complited, you are right. Inside initiliztion of the instance I can run while(true) and block the thread. My point was about "sane" coding were you know what you are doing. If I write the code which is not blocking, .NET itself guarantees that after that line your object is actually initilized. If you think you can change my post to better manifest thoughts on this, please feel free to edit. – Tigran Jul 09 '14 at 11:45