O'Reilly logo

WCF 4.0 Multi-tier Services Development with LINQ to Entities by Mike Liu

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Object initializer

In the past we couldn't initialize an object without using a constructor. For example, we could create and initialize a Product object like this if the Product class had a constructor with three parameters:

Product p = new product(1, "first candy", 100.0);

Or we could create the object and then initialize it later, like this:

            Product p = new Product();
            p.ProductID = 1;
            p.ProductName = "first candy";
            p.UnitPrice=(decimal)100.0;

Now with the new object initializer feature we can do it as follows:

Product product = new Product
{
    ProductID = 1,
    ProductName = "first candy",
    UnitPrice = (decimal)100.0
};

At compile time the compiler will automatically insert the necessary property setter code. So again this new feature is a Visual Studio ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required