C# 3.0 introduced a new abbreviated syntax for initializing collections, similar to the object initializer syntax shown in the section titled "Object Initializers." If the collection type instance you are initializing implements IEnumerable or IEnumerable<T> and contains a public Add method that accepts one parameter of the contained type, you can utilize this new syntax. Alternatively, your type could just implement ICollection<T> from the System.Collections.Generic namespace because it also implements IEnumerable<T>. The collection initializer syntax is shown in the following:
<pre class="brush:csharp">
using System;
using System.Collections.Generic;
public class Employee
{
public string Name { get; set; }
}
public class CollInitializerExample
{
static void Main() {
var developmentTeam = new List<Employee> {
new Employee { Name = "Michael Bolton" },
new Employee { Name = "Samir Nagheenanajar" },
new Employee { Name = "Peter Gibbons" }\
};
Console.WriteLine( "Development Team:" );
foreach( var employee in developmentTeam ) {
Console.WriteLine( "\t" + employee.Name );
}
}
}
</pre>
Under the covers the compiler generates a fair amount of code to help you out here. For each item in the collection initialization list, the compiler generates a call to the collection’s Add method. Notice that I have also used the new object initializer syntax to initialize each of the instances in the initializer list.
As I’ve mentioned, the collection type must implement ICollection<T> or implement IEnumerable<T> and a public Add method. If it does not, you will receive compile-time errors. Additionally, the collection must implement only one specialization of ICollection<T>; that is, it can only implement ICollection<T> for one type T. And finally, each item in the collection initialization list must be implicitly convertible to the type T.
Source Of Information : Apress Accelerated C Sharp 2010
<pre class="brush:csharp">
using System;
using System.Collections.Generic;
public class Employee
{
public string Name { get; set; }
}
public class CollInitializerExample
{
static void Main() {
var developmentTeam = new List<Employee> {
new Employee { Name = "Michael Bolton" },
new Employee { Name = "Samir Nagheenanajar" },
new Employee { Name = "Peter Gibbons" }\
};
Console.WriteLine( "Development Team:" );
foreach( var employee in developmentTeam ) {
Console.WriteLine( "\t" + employee.Name );
}
}
}
</pre>
Under the covers the compiler generates a fair amount of code to help you out here. For each item in the collection initialization list, the compiler generates a call to the collection’s Add method. Notice that I have also used the new object initializer syntax to initialize each of the instances in the initializer list.
As I’ve mentioned, the collection type must implement ICollection<T> or implement IEnumerable<T> and a public Add method. If it does not, you will receive compile-time errors. Additionally, the collection must implement only one specialization of ICollection<T>; that is, it can only implement ICollection<T> for one type T. And finally, each item in the collection initialization list must be implicitly convertible to the type T.
Source Of Information : Apress Accelerated C Sharp 2010
|
0 comments
Post a Comment