1

What is more efficent between this:

MyClass foo = new MyClass()
{
    name = "foo",
    color = "blue",
    number = 3
};

and this:

MyClass foo = new MyClass();
foo.name = "foo";
foo.color = "blue";
foo.number = 3;
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
Migio B
  • 405
  • 1
  • 6
  • 22

1 Answers1

1

Difference between first and second is, that the second way won't create a temporary object (as explained here: CA2000 - "out-of-school-junior-programmers"-mistakes or false positive?) - so no problems, warning or errors when it comes to disposing My Class.

Community
  • 1
  • 1
Matthias R.
  • 441
  • 2
  • 15
  • Thanks - this leads to http://stackoverflow.com/a/1679841/314291 - interesting. – StuartLC Feb 26 '14 at 13:10
  • yeah, indeed haha, had this (plus its resulting CA2000) problem a few weeks ago and could not explain why this might happen - now i know. hope this solved this question and your problem :) – Matthias R. Feb 26 '14 at 13:14
  • 1
    Also, this means the linked duplicate is actually wrong, even in release mode. There seems to be an extra assignment with the initializer, to keep the observable state of `foo` consistent. – StuartLC Feb 26 '14 at 13:16