O'Reilly logo

WCF 4.5 Multi-Layer Services Development with Entity Framework 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 could not initialize an object without using a constructor. For example, we could create and initialize a Product object as follows, if the Product class had a constructor with three parameters:

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

Or we could create the object and then initialize it later, as follows:

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

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

var product = new Product
{
    ProductID = 1,
    ProductName = "first candy",
    UnitPrice = 100.0m
};

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

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