C# 3.0 - Object Initializers, Collection Intializers, Anonymous Classes

In this article we will discuss some of the very useful features introduced in C# 3.0

What is Object Initializers?

Object Initializers let us create an object of a class and assign values to any accessible fields or properties of it at the same time; it's not necessary to explicitly invoke a constructor.

Let's see an Example; see:

public class Car

{

          public string Name

          {

                   get;

                   set;

          }

          public Color Color

          {

                   get;

                   set;

          }

}

In C# 2.0 we could have done that like this:

public static void Main()

{

          Car myCar = new Car();

          myCar.Name = "Chevrolet Corvette";

          myCar.Color = Color.Red;

}

But from 3.0 there is one more way to do the same thing:
 

public static void Main()

{

          Car myCar = new Car()

          {

                   Name = "Chevrolet Corvette",

                   Color = Color.Red

          };

}


What is Collection Initializers?

Just like Object Intializers, Collection Inializers let us create an instance of a collection and at the same time we can provide values with it.

Example:

List<int> MyInts=new List<int>(){1,2,3,5,6,7};

We can use object intializers here if we want to initialize a collection of objects; see:

List<Customer> MyCustomers =new List<Customer>()
{
new Customer(){CustomerName="Mr.A",Address="Mumbai"},
new Customer(){CustomerName="Mr.B",Address="Pune"},
new Customer(){CustomerName="Mr.C",Address="Banglore"}
};

Both Object Intializers and Collection Initializers are easy to use, simple and compact.

Anonymous Types

Anonymous types are types which behave just like normal class,

the only difference is:

  • They are not predefined. Unlike normal C# classes where we create first and then create again and again, Here we will directly create a object
  • They don't have a particular name.

Let's see and an example of how it looks.

To create an Anonymous type we need Object Initializers, as in:

var ExampleObject=new{Name="Sukesh Marla",Designation="Software Developer"};

Console.WriteLine(ExampleObject. Name);

Anonymous Types with LINQ

Assume we have a list of customers with 100 different properties (like Name, Point, Address…..) and we are interested only in their Name and points.

One thing we can do here is fire a LINQ query and extract the necessary data.

Normally in LINQ when we want only part of the information from each object in a sequence we use a Select clause and store temporary results into Anonymous types.

Example:

Var CustomerObject=Customers.Select(x=>
new {CustomerName=x.CustName,Points=x.Point});


So that's it. I hope everyone begins using this features if they haven't already.

Hope you enjoyed. I hope to see some good comments.
 


Similar Articles
Just Compile LLP
Just Compile is an IT based firm based out of Mumbai. Application Development, Staffing, Training