Skip to main content
Post Locked by Jamal
Notice added Historical significance by Jamal
Rollback to Revision 6
Source Link
Ocelot20
  • 1.9k
  • 11
  • 18

Question: What are peoples preferences between the two methods of creating an object (in C# in particular) and setting other fields (that may not be given values during construction)? Are either of them considered best practice? or is it down to the programmer and any styling conventions in place at the company?

My preference is absolutely for the object initializer method. It looks much cleaner and more concise to me.

Object initializer

Type T = new Type(Param1, Param2)
{
    Field1 = Value1,
    Field2 = Value2,
    Field3 = ValueFromFunction()    
};

Constructor

Type T = new Type(Param1, Param2);
T.Field1 = Value1;
T.Field2 = Value2;
T.Field3 = ValueFromFunction();

Question: What are peoples preferences between the two methods of creating an object (in C# in particular)? Are either of them considered best practice? or is it down to the programmer and any styling conventions in place at the company?

My preference is absolutely for the object initializer method. It looks much cleaner and more concise to me.

Object initializer

Type T = new Type()
{
    Field1 = Value1,
    Field2 = Value2,
    Field3 = ValueFromFunction()    
};

Constructor

Type T = new Type();
T.Field1 = Value1;
T.Field2 = Value2;
T.Field3 = ValueFromFunction();

Question: What are peoples preferences between the two methods of creating an object (in C# in particular) and setting other fields (that may not be given values during construction)? Are either of them considered best practice? or is it down to the programmer and any styling conventions in place at the company?

My preference is absolutely for the object initializer method. It looks much cleaner and more concise to me.

Object initializer

Type T = new Type(Param1, Param2)
{
    Field1 = Value1,
    Field2 = Value2,
    Field3 = ValueFromFunction()    
};

Constructor

Type T = new Type(Param1, Param2);
T.Field1 = Value1;
T.Field2 = Value2;
T.Field3 = ValueFromFunction();
Rollback to Revision 4
Source Link
Ocelot20
  • 1.9k
  • 11
  • 18

Question: What are peoples preferences between the two methods of creating an object (in C# in particular) and setting other fields (that may not be given values during construction)? Are either of them considered best practice? or is it down to the programmer and any styling conventions in place at the company?

My preference is absolutely for the object initializer method. It looks much cleaner and more concise to me.

Object initializer

Type T = new Type(Param1, Param2)
{
    Field1 = Value1,
    Field2 = Value2,
    Field3 = ValueFromFunction()    
};

Constructor

Type T = new Type(Param1, Param2);
T.Field1 = Value1;
T.Field2 = Value2;
T.Field3 = ValueFromFunction();

Question: What are peoples preferences between the two methods of creating an object (in C# in particular) and setting other fields (that may not be given values during construction)? Are either of them considered best practice? or is it down to the programmer and any styling conventions in place at the company?

My preference is absolutely for the object initializer method. It looks much cleaner and more concise to me.

Object initializer

Type T = new Type(Param1, Param2)
{
    Field1 = Value1,
    Field2 = Value2,
    Field3 = ValueFromFunction()    
};

Constructor

Type T = new Type(Param1, Param2);
T.Field1 = Value1;
T.Field2 = Value2;
T.Field3 = ValueFromFunction();

Question: What are peoples preferences between the two methods of creating an object (in C# in particular)? Are either of them considered best practice? or is it down to the programmer and any styling conventions in place at the company?

My preference is absolutely for the object initializer method. It looks much cleaner and more concise to me.

Object initializer

Type T = new Type()
{
    Field1 = Value1,
    Field2 = Value2,
    Field3 = ValueFromFunction()    
};

Constructor

Type T = new Type();
T.Field1 = Value1;
T.Field2 = Value2;
T.Field3 = ValueFromFunction();
added 14 characters in body
Source Link
Andy Hunt
  • 447
  • 1
  • 4
  • 12

Question: What are peoples preferences between the two methods of creating an object (in C# in particular) and setting other fields (that may not be given values during construction)? Are either of them considered best practice? or is it down to the programmer and any styling conventions in place at the company?

My preference is absolutely for the object initializer method. It looks much cleaner and more concise to me.

Object initializer

Type T = new Type(Param1, Param2)
{
    Field1 = Value1,
    Field2 = Value2,
    Field3 = ValueFromFunction()    
};

Constructor

Type T = new Type(Param1, Param2);
T.Field1 = Value1;
T.Field2 = Value2;
T.Field3 = ValueFromFunction();

Question: What are peoples preferences between the two methods of creating an object (in C# in particular) and setting other fields (that may not be given values during construction)? Are either of them considered best practice? or is it down to the programmer and any styling conventions in place at the company?

My preference is absolutely for the object initializer method. It looks much cleaner and more concise to me.

Object initializer

Type T = new Type()
{
    Field1 = Value1,
    Field2 = Value2,
    Field3 = ValueFromFunction()    
};

Constructor

Type T = new Type(Param1, Param2);
T.Field1 = Value1;
T.Field2 = Value2;
T.Field3 = ValueFromFunction();

Question: What are peoples preferences between the two methods of creating an object (in C# in particular) and setting other fields (that may not be given values during construction)? Are either of them considered best practice? or is it down to the programmer and any styling conventions in place at the company?

My preference is absolutely for the object initializer method. It looks much cleaner and more concise to me.

Object initializer

Type T = new Type(Param1, Param2)
{
    Field1 = Value1,
    Field2 = Value2,
    Field3 = ValueFromFunction()    
};

Constructor

Type T = new Type(Param1, Param2);
T.Field1 = Value1;
T.Field2 = Value2;
T.Field3 = ValueFromFunction();
Tweeted twitter.com/#!/StackCodeReview/status/105982772383985665
added 90 characters in body
Source Link
Andy Hunt
  • 447
  • 1
  • 4
  • 12
Loading
added 4 characters in body
Source Link
Andy Hunt
  • 447
  • 1
  • 4
  • 12
Loading
Source Link
Andy Hunt
  • 447
  • 1
  • 4
  • 12
Loading