Understanding Collection and Object Initializer

 

collection - we use it when we want to group objects of different types into one bucket. Remember that each item in a collection is an object. Example ArrayList adds all items in the form of objects so we were taking out values from the ArrayList instance then we must type from object type to its corresponding type



 

Here item is of object type, and we are type casting it to its corresponding type.

collection Initializer - The group of object initializers are nothing but collection Initializer

 



 

 

Object Initialzier - It is used to initialize the data members or properties of a class.To avoid creating multiple constructor of a class, we go for Object Initialzier. Object  initializers creates parameterless constructor internally. The curly braces opening and closing is called Object  initializers block.

 


Comments

Popular posts from this blog

How to execute Javascript in VS code