开发者

An explanation of Object Initialisers in C#?

C开发者_StackOverflowould someone please provide a explanation of Object Initialisers in C# and examples if possible.

So far I understand that they allow you to condense instances of a class, is this correct?


Object initializers was added to C# in version 3, and provide condensed object initialization syntax. They are nothing but syntactic sugar, meaning they allow you to express the same code shorter than before.

Here is an example:

Foo foo = new Foo { Name = "John", Age = 42 };

Which is the equivalent of:

Foo foo = new Foo();
foo.Name = "John";
foo.Age = 42;

Have a look on the official documentation here.

A related feature is collection initializers, which lets you initialize collections easily:

var names = new List<string> { "John", "Michael", "Joe" }

The above code is short hand for constructing a new List, and then adding each element to it. For collection initializers to work for a type, nothing more is required than a public Add method taking the correct type as the parameter.


Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.

Cat cat = new Cat { Age = 10, Name = "Fluffy" };

Refer to Object and Collection Initializers (C# Programming Guide).


Object Initialisers allow you to set the properties of an instance when you declare it. For example:

class House
{
    public int Rooms { get; set; }
    public int PeopleLiving { get; set; }
}

Then using this:

House h = new House { Rooms = 4, PeopleLiving = 5 }

Simply sets these values when you create the object, a bit like a constructor where you can choose which properties to set. You can also use a constructor as well:

House h = new House("10 Some Road", "Some City") { Rooms = 4, PeopleLiving = 5 }

This is actually just syntactic sugar, the compiler converts it to this equivalent C# code:

House h = new House();
h.Rooms = 4;
h.PeopleLiving = 5;


From the MSDN

Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.

All this means is that it's a convenient shortcut you can use to (hopefully) make your code more readable. The example they give:

Cat cat = new Cat { Age = 10, Name = "Fluffy" };

would be more traditionally written as:

Cat cat = new Cat();
cat.Age = 10;
cat.Name = "Fluffy";

or

Cat = new Cat(10, "Fluffy");

The former can be more long winded, and there is a difference between the compiled code (thanks to @Kirk Woll for the link), explained in this post. The initialiser creates a temporary variable, assigns the properties and then copies that to the real variable. Something like this:

Cat temporaryCat = new Cat();
temporaryCat.Age = 10;
temporaryCat.Name = "Fluffy";
Cat cat = temporaryCat;

The latter would (before C# 4.0 with optional arguments) require you to have a many overloaded versions of your constructor for each possible parameter combination.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜