您的位置:首页 > 编程语言 > C#

C#编程:Predicate<T> 委托

2015-08-16 22:19 615 查看
表示定义一组条件并确定指定对象是否符合这些条件的方法。

命名空间: 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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: