Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press
Buy directly from Paradoxal Press at $33.99 (Save 43%)
Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA
|
Listing 15-23 extracted from chapter
Collections
Listing 15-22< > Listing 15-24
This listing can be compiled with the command line: csc.exe /target:exe Example_15_23.cs Errors: 0 Warnings: 0
Example_15_23.cs
using System.Collections.Generic;
class Program {
class Article {
public Article(decimal price,string name){Price=price;Name=name;}
public readonly decimal Price;
public readonly string Name;
}
static void Main(){
// Seek out every odd integers.
// Implicitly uses a 'Predicate<T>' delegate object.
List<int> integers = new List<int>();
for(int i=1; i<=10; i++)
integers.Add(i);
List<int> even =integers.FindAll( delegate(int i){ return i%2==0; });
// Sum up items of the list.
// Implicitly uses an 'Action<T>' delegate object.
int sum = 0;
integers.ForEach(delegate(int i) { sum += i; });
// Sort items of type 'Article'.
// Implicitly uses a 'Comparison<T>' delegate object.
List<Article> articles = new List<Article>();
articles.Add( new Article(5,"Shoes") );
articles.Add( new Article(3,"Shirt") );
articles.Sort(delegate(Article x, Article y) {
return Comparer<decimal>.Default.Compare(x.Price,y.Price); } );
// Cast items of type 'Article' into 'decimal'.
// Implicitly uses a 'Converter<T,U>' delegate object.
List<decimal> artPrice = articles.ConvertAll<decimal> (
delegate(Article article) { return (decimal)article.Price; } );
}
}
Copyright Patrick Smacchia 2006 2007
|