Click below for Step by Step and Interview Q & A (dotnetinter) wrote,
Click below for Step by Step and Interview Q & A

Online guide to learn .NET for Object initializer in C#?

Object initializer let us create an instance of a class and assign values to its properties in a single line. Normally we write following things while working with classes and objects.

      1. Create Object of class

        code 1

       2. Assign values to its properties

          code 2

But with object initializer we can do it in single line as follows

        code 3

What does collection initializer do?

Just like Object initializer it will reduce the work of a developer. It will let us create an instance of any collection class and assign one or more elements to it in the same line.

This is how we normally work,

      1. Create instance of collection class.

          code 4

      2. Add elements to it

          code 5

But with collection initializer we can do it in single line.

        code 6

For technical trainings on various topics like WCF, MVC, Business Intelligence, Design Patterns, WPF, TFS and Basic fundamentals feel free to contact or visit

For more stuff like this, click here. Subscribe to article updates or follow at twitter @SukeshMarla

Also do not forget to visit for step by step, technical video & trainings.
Tags: .net, .net interview questions, c#, csharp, csharp interview questions, it interview questions with answers, object initializer, online guide to learn .net, programming interview questions

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded