You all now the cool syntax sugar for object initialization in C#:
Also there are sugar for collection initialization:
And, of course, we could mix these two cases:
But there is one rarely used case that is even cooler. If ItemContainer.Items is ICollection and it is created in constructor:
This code will be ILed as:
IL_0000: newobj ItemContainer..ctor
IL_0005: stloc.1
IL_0006: ldloc.1
IL_0007: callvirt ItemContainer.get_Items
IL_000C: newobj Item..ctor
IL_0011: stloc.2
IL_0012: ldloc.2
IL_0013: ldc.i4.1
IL_0014: callvirt Item.set_Age
IL_0019: ldloc.2
IL_001A: callvirt System.Collections.Generic.List<Item>.Add
So there is no constructions of Items, just single method 'Add'.
The same is applicable for Expressions as well.
var item = new Item { Age = 5 };
Also there are sugar for collection initialization:
var items = new List<Item> { new Item { Age = 5 } };
And, of course, we could mix these two cases:
class ItemContainer { public List<Item> Items{get;set;} } // ... var itemContainer = new ItemContainer { Items = new List<Item> {new Item { Age = 5 }} };
But there is one rarely used case that is even cooler. If ItemContainer.Items is ICollection and it is created in constructor:
class ItemContainer { public ItemContainer() { Items = new List<Item>(); } public List<Item> Items{get;set;} }Than you can add elements to the Items collection without 'new' construction:
var itemContainer = new ItemContainer { Items = { new Item { Age = 5 } } };
This code will be ILed as:
IL_0000: newobj ItemContainer..ctor
IL_0005: stloc.1
IL_0006: ldloc.1
IL_0007: callvirt ItemContainer.get_Items
IL_000C: newobj Item..ctor
IL_0011: stloc.2
IL_0012: ldloc.2
IL_0013: ldc.i4.1
IL_0014: callvirt Item.set_Age
IL_0019: ldloc.2
IL_001A: callvirt System.Collections.Generic.List<Item>.Add
So there is no constructions of Items, just single method 'Add'.
The same is applicable for Expressions as well.
No comments:
Post a Comment