Best Practices of Initialization Objects and Collections in C#

No.of Views807
Bookmarked0 times
Downloads 
Votes0
By  pranay rana   On  25 Feb 2011 08:02:53
Tag : CSharp , Miscellaneous
In this article, i will share with you, Best Practices of Initialization Objects and Collections in C# for different versions of .NET
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

What is object Initialization ?

Object initialization is new feature in C#3.0 which allow to create object without writing too much lengthy code and without invoking constructor.

Example :

class Employee
{
  public string Name { get; set; }
  public string Address { get; set; }
 
 public Employee(){}
 public Employee (string name) { Name= name;  }
}

To define object without object Inialization than your code is like

Employee emp = new Employee();
emp.Name="pranay";
emp.Address = "Ahmedabad";
 
or
 
Employee emp = new Employee("Krunal");
emp.Address = "Ahmedabad";

IL Code for this in ILDASM

// Code size       32 (0x20)
  .maxstack  2
  .locals init ([0] class anonymoustype.Employee emp)
  IL_0000:  nop
  IL_0001:  newobj     instance void anonymoustype.Employee::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  ldstr      "pranay"
  IL_000d:  callvirt   instance void anonymoustype.Employee::set_Name(string)
  IL_0012:  nop
  IL_0013:  ldloc.0
  IL_0014:  ldstr      "Ahmedabad"
  IL_0019:  callvirt   instance void anonymoustype.Employee::set_Address(string)
  IL_001e:  nop
  IL_001f:  ret

But with the object Initialization its like

Employee emp = new Employee{ Name="Pranay", Address="Ahmedabad"  };
or
Employee emp = new Employee() { Name="Pranay", Address="Ahmedabad"  };
or
Employee emp = new Employee("Hemang") { Address="Ahmedabad"  };

The thing in that all above line of code create object of Employee. But the first one create Object without calling constructor explicitly but it calls parameterless constructor where other two calls respective constructor explicitly.

IL Code for this in ILDASM for the first statment is

.entrypoint
  // Code size       34 (0x22)
  .maxstack  2
  .locals init ([0] class anonymoustype.Employee emp,
           [1] class anonymoustype.Employee '<>g__initLocal0')
  IL_0000:  nop
  IL_0001:  newobj     instance void anonymoustype.Employee::.ctor()
  IL_0006:  stloc.1
  IL_0007:  ldloc.1
  IL_0008:  ldstr      "Pranay"
  IL_000d:  callvirt   instance void anonymoustype.Employee::set_Name(string)
  IL_0012:  nop
  IL_0013:  ldloc.1
  IL_0014:  ldstr      "Ahmedabad"
  IL_0019:  callvirt   instance void anonymoustype.Employee::set_Address(string)
  IL_001e:  nop
  IL_001f:  ldloc.1
  IL_0020:  stloc

as you can see the Object Initialization code is similar to the above IL code. But the thing is parameterless constructor get generated by compiler which I have not written in first statement.

Why object Initialization is part of C#3.0?

Again this new feature is part of C#3.0 to support LINQ. You can see the this feature with Anonymous type here.

Collection Initialization

Collection Initialization allows to create collection of object using Object Initialization feature.
Example :

List<Employee> emp = new List<Employee>
      {
        new Employee { Name="Pranay", Address="Ahmedabad"  },
        new Employee { Name="Krunal", Address="Mevada"  },
        null
     };

Summary

Object Initialization allow to create objects in one line i.e just one expression. So we can create large collection of object without writing to lengthy code which we can see in Collection Initialization.

 

 
Sign Up to vote for this article
 
About Author
 
pranay rana
Occupation-CEO
Company-GMind Solusion
Member Type-Senior
Location-India
Joined date-08 Jan 2011
Home Page-http://pranayamr.blogspot.com
Blog Page-http://pranayamr.blogspot.com
Hey, I am Pranay Rana, working as a Senior Software engineer in mid-size company located in ahmedabad. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 4.3 years now. For me def. of programming is : Programming is something that you do once and that get used by multiple for many years You can visit me on my blog - http://pranayamr.blogspot.com/ StackOverFlow - http://stackoverflow.com/users/314488/pranay My CV :- http://careers.stackoverflow.com/pranayamr
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top