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.
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.
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.
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.
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.
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).
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.
A good rule of thumb is:
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.
I currently see the following problems with using them:
So my working theory is: Don't use object initializers they encourage stupid things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With