C# 3.0 Enhancements: Object Initializers

C# 3.0 Enhancements: Object Initializers

  • Comments 3

C# 3.0 allows us to initialize object in far better way in one single line. Let’s talk with an example here. Back again to my favorite Customer object.


public class Customer


    public Customer() { }


    private int _ID;

    public int ID


        get { return _ID; }

        set { _ID = value; }



    private string _Name;

    public string Name


        get { return _Name; }

        set { _Name = value; }




If you want to initialize the properties with the object initialization current style won’t allow you to do so. What you can do is that you can create another public parameterized constructor, like


public Customer(int intCustID, string sCustName)


    _ID = intCustID;

    _Name = sCustName;



Now internally this will initialize the property. But you may not require to set values to property every time. C# 3.0 rescues us where we can initialize the Customer class with property even though there is just a default public constructor.



Customer objCust = new Customer() {ID = 1,Name = "Wriju"};


Now if you do not want to set all the properties you can freely chose one or any number. It does not force you to initialize all the properties every time.


Customer objCust = new Customer { Name = "Wriju" };


Amazing feature.



Leave a Comment
  • Please add 8 and 6 and type the answer here:
  • Post
  • C# 3.0 allows us to initialize object in far better way in one single line. Let’s talk with an example

  • C# 3.0 (“C# Orcas”) introduces several language extensions that build on C# 2.0 to support the creation and use of higher order, functional style class libraries. The extensions enable construction of compositional APIs that have equal expressive power of query languages in domains such as relational databases and XML. The extensions include:

    • Implicitly typed local variables, which permit the type of local variables to be inferred from the expressions used to initialize them.

    • Extension methods, which make it possible to extend existing types and constructed types with additional methods.

    • Lambda expressions, an evolution of anonymous methods that provides improved type inference and conversions to both delegate types and expression trees.

    • Object initializers, which ease construction and initialization of objects.

    • Anonymous types, which are tuple types automatically inferred and created from object initializers.

    • Implicitly typed arrays, a form of array creation and initialization that infers the element type of the array from an array initializer.

    • Query expressions, which provide a language integrated syntax for queries that is similar to relational and hierarchical query languages such as SQL and XQuery.

    • Expression trees, which permit lambda expressions to be represented as data (expression trees) instead of as code (delegates).

  • While using these object initializers, I came to a point where I had two values that had side-effects. I was wondering whether initializers are strongly ordered (according to the source code or some other order). The C# 3.0 Spec doesn't seem to say explicitly.

Page 1 of 1 (3 items)