C#编程:Predicate<T> 委托
2015-08-16 22:19
615 查看
表示定义一组条件并确定指定对象是否符合这些条件的方法。
命名空间: System
程序集: mscorlib(在
mscorlib.dll 中)
in T
要比较的对象的类型。
该类型参数是逆变的。即可以使用指定的类型或派生程度更低的类型。有关协变和逆变的更多信息,请参见泛型中的协变和逆变。
obj类型:T
要按照由此委托表示的方法中定义的条件进行比较的对象。
类型:System.Boolean
如果 obj 符合由此委托表示的方法中定义的条件,则为 true;否则为 false。
再举一个C#入门经典第6版的例子:
Vector.cs:
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch12Ex03
{
public class Vector
{
public double? R = null;
public double? Theta = null;
public double? ThetaRadians
{
get
{
// Convert degrees to radians.
return (Theta * Math.PI / 180.0);
}
}
public Vector(double? r, double? theta)
{
// Normalize.
if (r < 0)
{
r = -r;
theta += 180;
}
theta = theta % 360;
// Assign fields.
R = r;
Theta = theta;
}
public static Vector operator +(Vector op1, Vector op2)
{
try
{
// Get (x, y) coordinates for new vector.
double newX = op1.R.Value * Math.Sin(op1.ThetaRadians.Value)
+ op2.R.Value * Math.Sin(op2.ThetaRadians.Value);
double newY = op1.R.Value * Math.Cos(op1.ThetaRadians.Value)
+ op2.R.Value * Math.Cos(op2.ThetaRadians.Value);
// Convert to (r, theta).
double newR = Math.Sqrt(newX * newX + newY * newY);
double newTheta = Math.Atan2(newX, newY) * 180.0 / Math.PI;
// Return result.
return new Vector(newR, newTheta);
}
catch
{
// Return "null" vector.
return new Vector(null, null);
}
}
public static Vector operator -(Vector op1)
{
return new Vector(-op1.R, op1.Theta);
}
public static Vector operator -(Vector op1, Vector op2)
{
return op1 + (-op2);
}
public override string ToString()
{
// Get string representation of coordinates.
string rString = R.HasValue ? R.ToString() : "null";
string thetaString = Theta.HasValue ? Theta.ToString() : "null";
// Return (r, theta) string.
return string.Format("({0}, {1})", rString, thetaString);
}
}
}
Vectors.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch12Ex03
{
public class Vectors : List<Vector>
{
public Vectors()
{
}
public Vectors(IEnumerable<Vector> initialItems)
{
foreach (Vector vector in initialItems)
{
Add(vector);
}
}
public string Sum()
{
StringBuilder sb = new StringBuilder();
Vector currentPoint = new Vector(0.0, 0.0);
sb.Append("origin");
foreach (Vector vector in this)
{
sb.AppendFormat(" + {0}", vector);
currentPoint += vector;
}
sb.AppendFormat(" = {0}", currentPoint);
return sb.ToString();
}
}
}
VectorDelegates.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch12Ex03
{
public static class VectorDelegates
{
public static int Compare(Vector x, Vector y)
{
if (x.R > y.R)
{
return 1;
}
else if (x.R < y.R)
{
return -1;
}
return 0;
}
public static bool TopRightQuadrant(Vector target)
{
if (target.Theta >= 0.0 && target.Theta <= 90.0)
{
return true;
}
else
{
return false;
}
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch12Ex03
{
class Program
{
static void Main(string[] args)
{
Vectors route = new Vectors();
route.Add(new Vector(2.0, 90.0));
route.Add(new Vector(1.0, 180.0));
route.Add(new Vector(0.5, 45.0));
route.Add(new Vector(2.5, 315.0));
Predicate<Vector> searcher =
new Predicate<Vector>(VectorDelegates.TopRightQuadrant);
Vectors topRightQuadrantRoute = new Vectors(route.FindAll(searcher));
Console.WriteLine(topRightQuadrantRoute.Sum());
Console.ReadKey();
}
}
}
输出结果:
origin + (0.5, 45)
+ (2, 90) = (2.37996083210903, 81.4568451851077)
命名空间: System
程序集: mscorlib(在
mscorlib.dll 中)
public delegate bool Predicate<in T>( T obj )
类型参数
in T要比较的对象的类型。
该类型参数是逆变的。即可以使用指定的类型或派生程度更低的类型。有关协变和逆变的更多信息,请参见泛型中的协变和逆变。
参数
obj类型:T要按照由此委托表示的方法中定义的条件进行比较的对象。
返回值
类型:System.Boolean如果 obj 符合由此委托表示的方法中定义的条件,则为 true;否则为 false。
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 static // Find method of the Array class. 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); }
// 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; } } } /* This code example produces the following output: Found: X = 275, Y = 395 */
再举一个C#入门经典第6版的例子:
Vector.cs:
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch12Ex03
{
public class Vector
{
public double? R = null;
public double? Theta = null;
public double? ThetaRadians
{
get
{
// Convert degrees to radians.
return (Theta * Math.PI / 180.0);
}
}
public Vector(double? r, double? theta)
{
// Normalize.
if (r < 0)
{
r = -r;
theta += 180;
}
theta = theta % 360;
// Assign fields.
R = r;
Theta = theta;
}
public static Vector operator +(Vector op1, Vector op2)
{
try
{
// Get (x, y) coordinates for new vector.
double newX = op1.R.Value * Math.Sin(op1.ThetaRadians.Value)
+ op2.R.Value * Math.Sin(op2.ThetaRadians.Value);
double newY = op1.R.Value * Math.Cos(op1.ThetaRadians.Value)
+ op2.R.Value * Math.Cos(op2.ThetaRadians.Value);
// Convert to (r, theta).
double newR = Math.Sqrt(newX * newX + newY * newY);
double newTheta = Math.Atan2(newX, newY) * 180.0 / Math.PI;
// Return result.
return new Vector(newR, newTheta);
}
catch
{
// Return "null" vector.
return new Vector(null, null);
}
}
public static Vector operator -(Vector op1)
{
return new Vector(-op1.R, op1.Theta);
}
public static Vector operator -(Vector op1, Vector op2)
{
return op1 + (-op2);
}
public override string ToString()
{
// Get string representation of coordinates.
string rString = R.HasValue ? R.ToString() : "null";
string thetaString = Theta.HasValue ? Theta.ToString() : "null";
// Return (r, theta) string.
return string.Format("({0}, {1})", rString, thetaString);
}
}
}
Vectors.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch12Ex03
{
public class Vectors : List<Vector>
{
public Vectors()
{
}
public Vectors(IEnumerable<Vector> initialItems)
{
foreach (Vector vector in initialItems)
{
Add(vector);
}
}
public string Sum()
{
StringBuilder sb = new StringBuilder();
Vector currentPoint = new Vector(0.0, 0.0);
sb.Append("origin");
foreach (Vector vector in this)
{
sb.AppendFormat(" + {0}", vector);
currentPoint += vector;
}
sb.AppendFormat(" = {0}", currentPoint);
return sb.ToString();
}
}
}
VectorDelegates.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch12Ex03
{
public static class VectorDelegates
{
public static int Compare(Vector x, Vector y)
{
if (x.R > y.R)
{
return 1;
}
else if (x.R < y.R)
{
return -1;
}
return 0;
}
public static bool TopRightQuadrant(Vector target)
{
if (target.Theta >= 0.0 && target.Theta <= 90.0)
{
return true;
}
else
{
return false;
}
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch12Ex03
{
class Program
{
static void Main(string[] args)
{
Vectors route = new Vectors();
route.Add(new Vector(2.0, 90.0));
route.Add(new Vector(1.0, 180.0));
route.Add(new Vector(0.5, 45.0));
route.Add(new Vector(2.5, 315.0));
Predicate<Vector> searcher =
new Predicate<Vector>(VectorDelegates.TopRightQuadrant);
Vectors topRightQuadrantRoute = new Vectors(route.FindAll(searcher));
Console.WriteLine(topRightQuadrantRoute.Sum());
Console.ReadKey();
}
}
}
输出结果:
origin + (0.5, 45)
+ (2, 90) = (2.37996083210903, 81.4568451851077)
相关文章推荐
- 【C#】程序集中资源文件的使用
- C#基础--之数据类型
- c# 自定义控件
- C#中图片.BYTE[]和base64string的转换
- C# 抽象类与抽象方法的作用与实例
- C#中的数学类,Math,浮点数(中)
- 写出高性价比代码之C#建议第52-及时释放资源
- 说说C#的数学类,Math,浮点数(上)
- C#实现远程桌面自动登录
- 编写高质量代码改善C#程序的157个建议——建议52:及时释放资源
- 编写高质量代码改善C#程序的157个建议——建议51:具有可释放字段的类型或拥有本机资源的类型应该是可释放的
- 编写高质量代码改善C#程序的157个建议——建议50:在Dispose模式中应区别对待托管资源和非托管资源
- 编写高质量代码改善C#程序的157个建议——建议49:在Dispose模式中应提取一个受保护的虚方法
- C#基础系列——异步编程初探:async和await
- 编写高质量代码改善C#程序的157个建议——建议48:Dispose方法应允许被多次调用
- 编写高质量代码改善C#程序的157个建议——建议47:即使提供了显式释放方法,也应该在终结器中提供隐式清理
- AutoCAD.Net/C#.Net QQ群:193522571 将坐标系置为WCS
- 编写高质量代码改善C#程序的157个建议——建议46:显式释放资源需继承接口IDisposable
- C#枚举器接口IEnumerator的实现
- C#调Exe