Prev | Current Page 22 | Next

Satheesh, N Kumar

"LINQ Quickly"


LINQ query expressions make use of these object initializers for initializing
anonymous types. In the Anonymous Types section, as discussed previously, we have
a select query which creates an anonymous type with two properties. The values
are also assigned using object initializers.
var IcecreamsWithLessPrice =
from ice in icecreamsList
where ice.Price < 10
select new
{
Name = ice.Name,
Price = ice.Price
};
Overview
[ 12 ]
Collection Initializers
Collection initializers use object initializers to initialize their object collection. By
using a collection initializer, we do not have to initialize objects by having multiple
calls. For example, in the Anonymous Types section a little earlier, we created a list
named icecreamsList which is a collection of Icecreams. All Icecream objects
added to the collection are initialized using the collection initializer, as follows:
List icecreamsList = new List
{
new Icecream
{
Name="Chocolate Fudge Icecream",
Ingredients="cream, milk, mono and diglycerides.


Pages:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
pustaki szklane House Extension Kąty Rybackie noclegi kasyno Kołobrzeg