Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use an object initializer or a constructor? [duplicate]

I have just learned about object initializers and was wondering what the best practices for when to use them are.

This is what I read about them: http://msdn.microsoft.com/en-us/library/vstudio/bb384062.aspx It makes it clear that they are necessary for creating anonymous types but I would like to know if I should try to prefer them to normal constructors in all the other cases.

like image 225
Sarien Avatar asked Mar 21 '13 18:03

Sarien


People also ask

Is a constructor and initializer the same?

A constructor is a defined method on a type which takes a specified number of parameters and is used to create and initialize an object. An object initializer is code that runs on an object after a constructor and can be used to succinctly set any number of fields on the object to specified values.

Should I initialize constructor class?

A class object with a constructor must be explicitly initialized or have a default constructor. Except for aggregate initialization, explicit initialization using a constructor is the only way to initialize non-static constant and reference class members.

What is object initializer?

An object initializer is an expression that describes the initialization of an Object . Objects consist of properties, which are used to describe an object. The values of object properties can either contain primitive data types or other objects.

Why do we initialize in constructor?

A constructor is typically used to initialize instance variables representing the main properties of the created object. If we don't supply a constructor explicitly, the compiler will create a default constructor which has no arguments and just allocates memory for the object.


4 Answers

I would like to know if I should try to prefer them to normal constructors in all the other cases.

I would say no.

Constructors have a huge number of advantages. Using a constructor, the compiler will enforce that all required data is provided to your type. This means you can make it impossible to create an instance of your type that is in an invalid state, which allows you to proactively prevent many bugs.

Object initializers, on the other hand, create many disadvantages. You must provide publically settable properties for any data you need to initialize. They are not required at construction time, so users of your type can accidentally leave out some data.

In general, anything required for your class to function should be required in the constructor. Object initializers can still be used, even if you have a custom constructor, but should only be used for data that's optional in setting on your class. Mixing both in an initialization is fine, which means you can do:

var yourInst = new YourClass(req1, req2) { OptionalProperty = opt1 }

This can help reduce the number of constructor overloads required (similar to using optional arguments, but without some of the disadvantages of versioning in optional arguments).

like image 85
Reed Copsey Avatar answered Oct 17 '22 06:10

Reed Copsey


I believe you are confusing things.

Object initializers will call the default (or a specified) constructor of the class! So you can't really use object initializers instead of the normal constructor. So when using an object initializer you are calling the constructor anyway.

If you are wondering about Object Initializers for a class that you are designing, than the answer still applies. Make sure to provide the necessary constructors that make sense. You don't need to do anything special to enable/allow object initializers. They are syntactic sugar provided by the C# compiler since version 3.0 and allow users of your class to initialize public members of your class, immediately after construction.

like image 30
Mike Dinescu Avatar answered Oct 17 '22 06:10

Mike Dinescu


A good rule of thumb is:

  • If it's required to make the class work right, it should be a constructor parameter
  • If changing it would break the class, it should be a constructor parameter
  • If it's optional, has a sane default, and/or simply and safely changes how the class behaves, it should be an initializer.

The main advantage to initializers is that you don't have to set them when the object is created - you can set them later, based on other logic.

like image 39
Bobson Avatar answered Oct 17 '22 05:10

Bobson


I currently see the following problems with using them:

  1. The properties that are assigned have to be changeable. This seems undesirable because if the data is passed to a constructor I can check it there and make object creation fail if the data provided is insufficient or wrong. If data can be assigned with a property I suddenly have to figure out what the state of my object is, whether everything is properly created yet or which other state I might be in.
  2. The properties that are assigned have to be public. This means that access to things that might otherwise be private will have to be exposed and then later restricted by using an interface or something like that.

So my working theory is: Don't use object initializers they encourage stupid things.

like image 38
Sarien Avatar answered Oct 17 '22 07:10

Sarien