关于委托Lamda表达式等的一个小例子
2012-02-07 11:25
323 查看
using System;
using System.Drawing;
public class Example
{
public static void Main()
{
// Create an array of five Point structures.
Point[] points = { new Point(100, 200),
new Point(150, 250), new Point(250, 375),
new Point(275, 395), new Point(295, 450) };
// To find the first Point structure for which X times Y
// is greater than 100000, pass the array and a delegate
// that represents the ProductGT10 method to the Shared
// Find method of the Array class.
//第一种写法
Point first = Array.Find(points, new Predicate<Point>(Example.ProductGT10));
//第二种写法
Point first = Array.Find(points, p => { return p.X * p.Y > 100000; });
//第三种写法
Point first = Array.Find(points, p => p.X * p.Y > 100000);
//第四种写法
Point first = Array.Find(points, delegate(Point p) { return p.X * p.Y > 100000; });
//第五种写法
Point first = Array.Find(points, ProductGT10);
// Note that you do not need to create the delegate
// explicitly, or to specify the type parameter of the
// generic method, because the C# compiler has enough
// context to determine that information for you.
// Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
Console.Read();
}
// This method implements the test condition for the Find
// method.
private static bool ProductGT10(Point p)
{
if (p.X * p.Y > 100000)
{
return true;
}
else
{
return false;
}
}
}
using System.Drawing;
public class Example
{
public static void Main()
{
// Create an array of five Point structures.
Point[] points = { new Point(100, 200),
new Point(150, 250), new Point(250, 375),
new Point(275, 395), new Point(295, 450) };
// To find the first Point structure for which X times Y
// is greater than 100000, pass the array and a delegate
// that represents the ProductGT10 method to the Shared
// Find method of the Array class.
//第一种写法
Point first = Array.Find(points, new Predicate<Point>(Example.ProductGT10));
//第二种写法
Point first = Array.Find(points, p => { return p.X * p.Y > 100000; });
//第三种写法
Point first = Array.Find(points, p => p.X * p.Y > 100000);
//第四种写法
Point first = Array.Find(points, delegate(Point p) { return p.X * p.Y > 100000; });
//第五种写法
Point first = Array.Find(points, ProductGT10);
// Note that you do not need to create the delegate
// explicitly, or to specify the type parameter of the
// generic method, because the C# compiler has enough
// context to determine that information for you.
// Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
Console.Read();
}
// This method implements the test condition for the Find
// method.
private static bool ProductGT10(Point p)
{
if (p.X * p.Y > 100000)
{
return true;
}
else
{
return false;
}
}
}
相关文章推荐
- C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介
- C#中关于委托练习的一个例子
- C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介
- C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介
- C#中关于委托练习的一个例子
- 一个关于委托事件的例子(计时器)
- Lamda表达式的一个有意思的例子
- 关于委托与事件的一个小例子
- 一个关于委托的例子(排序)
- 用一个实例说说委托,匿名委托,Lamda表达式
- 关于类的静态变量和非静态变量区别的一个研究例子
- 委托、lamda表达式..委托概念-匿名函数-泛型委托-Lamda表达式-多播委托
- 一个委托的简单例子
- 关于ByteArrayOutputStream(字节数组输出流)的一个例子,与ByteArrayInputStream作一比较
- 关于如何计算子网掩码和广播地址的问题的一个例子
- 一段关于正则表达式的例子
- 关于Logstash中grok插件的正则表达式例子
- JDK中的Timer和TimerTask详解 目录结构: Timer和TimerTask 一个Timer调度的例子 如何终止Timer线程 关于cancle方式终止线程 反复执行一个任务 sche
- 给大家做了一个关于日程的例子,可以参考一下,对进一步学习有所帮助
- 关于那个SMTP类及一个例子