SIGN UP MEMBER LOGIN:    
ARTICLE

Object Initializer in C#

Posted by Hiren Visavadiya Articles | Learn .NET July 29, 2011
Here you will learn some new features in C# 3.0 ike Object initializer, implicitly typed variables, extension methods, anonymous types, object initializer, Collection initializer, and automatic properties.
Reader Level:
Download Files:
 


Let us play around with some new features of C# 3.0 or above and you can say .Net framework 3.0 and above, like Object initializer, implicitly typed variables, extension methods, anonymous types, object initializer, Collection initializer, and automatic properties.

Let's start with Object and collection initializes.

Just refer to the following code of style. Definitely all .Net coders are aware of these lines.

public class Customer
{
    private int _customerID;
    private string _companyName;
    private Phone _phone;
    public int CustomerID
    {
        get { return _customerID; }
        set { _customerID = value; }
    }
    public string CompanyName
    {
        get { return _companyName; }
        set { _companyName = value; }
    }
    public Phone Phone
    {
        get { return _phone; }
        set { _phone = value; }
    }
}

public class Phone
{
    private string _countryCode;
    public string CountryCode
    {
        get { return _countryCode; }
        set { _countryCode = value; }

    }
    private string _areacode;
    public string AreaCode
    {
        get { return _areacode; }
        set { _areacode = value; }
    }
    private string _phonenumber;
    public string AreaCode
    {
        get { return _phonenumber; }
        set { _phonenumber = value; }
    }

}

Regular method of initializing an instance of the Customer class.

 Phone oPhone = new Phone();
 oPhone.CountryCode = "+91";
 oPhone.AreaCode = "0999";
 oPhone.PhoneNumber = "999999";

 Customer oCustomer = new Customer();
 oCustomer.CustomerID = 101;
 oCustomer.CompanyName = "oTest Corporation";
 oCustomer.Phone = oPhone; 

By taking advantage of Object Initializers an instance of the Customer class

Customer nCustomer = new Customer()
 {
     CustomerID = 102,
     CompanyName = "nTest Corporation",
     Phone = new Phone()
     {
         CountryCode = "+91",
         AreaCode = "09999",
         PhoneNumber = "999999"
     }
 };

We do not need to take care of performance issues. Because in the execution time, both samples make sample IL code. So both samples give sample performance.

Next time we will look into automatic property.

Happy coding

Login to add your contents and source code to this article
share this article :
post comment
 

Useful article, Welcome to the Mindcracker Community.

Posted by Dinesh Beniwal Jul 29, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor