谓语 [英] Predicate

查看:66
本文介绍了谓语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有人可以解释一下:


<这是来自使用泛型 - 作者:Ludwig Stuyck>


string nameToFind =" Ludwig Stuyck" ;;

PersonNameFilter personNameFilter = new PersonNameFilter(nameToFind);


//创建一个新谓词,使用FilterByName方法确定

是否找到了该人


谓词< PersonfilterByName = new

Predicate< ; Person>(personNameFilter.FilterByName);


//查找集合中的人物


Person foundPerson = persons.Find(filterByName) ;


??我们是将方法传递给方法还是什么


??这类似于匿名方法。


??无法看到完全理解它或看到全部功率,但我知道/听说它是非常强大的


谢谢


Lit.

Hi,

Can someone explain this:

<this is from Using generic- By Ludwig Stuyck>

string nameToFind = "Ludwig Stuyck";
PersonNameFilter personNameFilter = new PersonNameFilter(nameToFind);

// Create a new predicate, that uses the FilterByName method to determine
whether the person has been found

Predicate<PersonfilterByName = new
Predicate<Person>(personNameFilter.FilterByName);

// Find the person in the collection

Person foundPerson = persons.Find(filterByName);

?? Are we passing a method to a method or what

?? is this similar to anonymous methods.

?? can not see the fully understand it or see the full power yet, but I
know/heard it is very powerful

Thanks

Lit.

推荐答案

查看MSDN中的Predicate Generic Delegate,它提供基本样本。


从样本中提取

//要查找X次Y

//大于100000的第一个Point结构,请传递数组和委托

//表示共享的ProductGT10方法
// Array类的Find方法。

Point first = Array.Find(points ,ProductGT10);


//请注意,您不需要显式创建委托

//,或指定
//泛型方法,因为C#编译器有足够的

//上下文来为你确定这些信息。


//显示找到第一个结构。

Console.WriteLine(" Found:X = { 0},Y = {1}",first.X,first.Y);


请注意,您不需要创建委托,您可以使用类内方法:


//此方法实现Find

//方法的测试条件。

private static bool ProductGT10(Point p)

{

if(... return false;

}

}

}


正如你所看到的那样,你传递了方法到方法。 - 即委托,这是一个与匿名方法相似的
- 如果忘记你可以使用

ProductGT10也作为标准静态方法。

HTH

Alex

" Lit" < sq ********** @ hotmail.comwrote in message

新闻:Oc ************** @ TK2MSFTNGP04.phx.gbl ...
Look at Predicate Generic Delegate in MSDN, which provides basic sample.

Extract from sample
// 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, 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);

Note that you don''t need create delegate and you can use in-class method:

// This method implements the test condition for the Find
// method.
private static bool ProductGT10(Point p)
{
if (... return false;
}
}
}

As you can see you pass "method to method" - namely, delegate, which is
somewhat similat to anonymous method - if to forget that you can use
ProductGT10 also as standard static method.

HTH
Alex
"Lit" <sq**********@hotmail.comwrote in message
news:Oc**************@TK2MSFTNGP04.phx.gbl...




有人可以解释一下:


<这是来自Using generic- By Ludwig Stuyck>


string nameToFind =" Ludwig Stuyck" ;;

PersonNameFilter personNameFilter = new PersonNameFilter(nameToFind);


//创建一个新的谓词,使用FilterByName方法确定

是否找到了这个人


Predicate< PersonfilterByName = new

Predicate< Person>(personNameFilter.FilterByName);


//找到收藏中的人物


人物foundPerson = persons.Find(filterByName);


??我们是将方法传递给方法还是什么


??这类似于匿名方法。


??无法看到完全理解它或看到全部功率,但我知道/听说它是非常强大的


谢谢


Lit.
Hi,

Can someone explain this:

<this is from Using generic- By Ludwig Stuyck>

string nameToFind = "Ludwig Stuyck";
PersonNameFilter personNameFilter = new PersonNameFilter(nameToFind);

// Create a new predicate, that uses the FilterByName method to determine
whether the person has been found

Predicate<PersonfilterByName = new
Predicate<Person>(personNameFilter.FilterByName);

// Find the person in the collection

Person foundPerson = persons.Find(filterByName);

?? Are we passing a method to a method or what

?? is this similar to anonymous methods.

?? can not see the fully understand it or see the full power yet, but I
know/heard it is very powerful

Thanks

Lit.



Predicate< Tcan可以被认为是一个(类型化的)函数指针,所以

是的,这实际上是将方法传递给方法;这允许

Find方法依次调用每个项目(Person)上的方法,并且

查看它是否匹配(通过返回true)。你如何构建

谓词是它变得有趣的地方;这里显示的方式显然是使用

a类定义 - 但正如你所看到的(我想象

PersonNameFilter代码显示)这是一个痛苦,而不是非常多才多艺的<每次你有一个不同的过滤器需要越来越多的类/方法,因此你需要越来越多的类/方法。匿名方法更简洁,但确实* b $ b *完全相同的东西;它是C#编译器写的

PersonNameFilter类和FilterByName方法(虽然名称

当然会有所不同)。

这允许我们只需使用:

找到人= person.Find(委托人(p){return p.Name ==

nameToFind;});


在C#3.0(.NET 3.5)中,我们可以使用Lambda表达式来做同样的事情

整洁:

找到的人=查找(p = > p.Name == nameToFind);


为了说明我对函数指针的意义(第一部分),

内部(由反射器提供)列表< T> .Find方法显示

以下。


Marc


公共T查找(Predicate< ; Tmatch)

{

if(match == null)

{


ThrowHelper。 ThrowArgumentNullException(ExceptionAr gument.match);

}

for(int i = 0; i< this._size; i ++)

{

if(match(this._items [i]))

{

返回this._items [i];

}

}

返回默认值(T);

}

The Predicate<Tcan be thought of as a (typed) function pointer, so
yes this is essentially passing a method to a method; this allows the
Find method to invoke the method on each item (Person) in turn, and
see if it is a match (by returning true). How you construct the
predicate is where it gets fun; the way shown here is obviously using
a class definition - but as you can see (I imagine the
PersonNameFilter code is shown) this is a pain, and not very versatile
as you need more and more classes / methods every time you have a
different filter. Anonymous methods are far more concise, but do
*exactly* the same thing; it is the C# compiler that writes the
PersonNameFilter class and FilterByName method (although the names
will be different of course).
This allows us to simply use:
Person found = persons.Find(delegate (Person p) {return p.Name ==
nameToFind;});

In C# 3.0 (.NET 3.5) we can use Lambda expressions to do the same even
tidier:
Person found = Find(p=>p.Name==nameToFind);

To illustrate what I meant about function pointers (the first part),
internally (courtesy of reflector) the List<T>.Find method is shown
below.

Marc

public T Find(Predicate<Tmatch)
{
if (match == null)
{

ThrowHelper.ThrowArgumentNullException(ExceptionAr gument.match);
}
for (int i = 0; i < this._size; i++)
{
if (match(this._items[i]))
{
return this._items[i];
}
}
return default(T);
}


谢谢AlexS和Marc


我头疼但试图看看大图。

请让我看看如果我在这里了解你们。


我们正在调用一个传递参数的方法,其中一个是指针

a方法。


所以如果我的泛型方法大致相同但是它的一个元素

需要一个专门的方法(我正在通过的那个)然后这就是它的地方

有用???


你有几个问题,你认为这是一个解决方案唯一的好处

解决方案或通用解决方案。一切的弦理论?


这对收藏品最有用吗?


这对于数学问题是否有用。


你在哪里看到这个有用吗?人工智能,超级

动态多态系统还是什么?


我还没有拉我的头发......但是变灰了。 />

请指教,


谢谢,


点亮

"点亮" < sq ********** @ hotmail.comwrote in message

新闻:Oc ************** @ TK2MSFTNGP04.phx.gbl ...
Thanks AlexS and Marc

I have a headache but trying to see the big picture.
Please let me see If I understand you both here.

we are invoking a method passing parameters and one of them is a pointer to
a method.

So If I have a generic method that is mostly the same but one element of it
needs a specialized Method ( the one I am passing ) then this is where it is
useful???

do you have few problems where you see this as a solution the only good
solution or generic solution. the string theory of everything?

Is this most useful for collections?

Is this useful for Mathematical problems.

where else have you seen this useful? Artificial Intelligence, super
dynamic polymorphic systems or what?

I am not at the point of pulling my hair yet... but getting gray.

Enlighten me please,

Thank you,

Lit
"Lit" <sq**********@hotmail.comwrote in message
news:Oc**************@TK2MSFTNGP04.phx.gbl...




有人可以解释一下:


<这是来自Using generic- By Ludwig Stuyck>


string nameToFind =" Ludwig Stuyck" ;;

PersonNameFilter personNameFilter = new PersonNameFilter(nameToFind);


//创建一个新的谓词,使用FilterByName方法确定

是否找到了这个人


Predicate< PersonfilterByName = new

Predicate< Person>(personNameFilter.FilterByName);


//找到收藏中的人物


人物foundPerson = persons.Find(filterByName);


??我们是将方法传递给方法还是什么


??这类似于匿名方法。


??无法看到完全理解它或看到全部功率,但我知道/听说它是非常强大的


谢谢


点亮。

Hi,

Can someone explain this:

<this is from Using generic- By Ludwig Stuyck>

string nameToFind = "Ludwig Stuyck";
PersonNameFilter personNameFilter = new PersonNameFilter(nameToFind);

// Create a new predicate, that uses the FilterByName method to determine
whether the person has been found

Predicate<PersonfilterByName = new
Predicate<Person>(personNameFilter.FilterByName);

// Find the person in the collection

Person foundPerson = persons.Find(filterByName);

?? Are we passing a method to a method or what

?? is this similar to anonymous methods.

?? can not see the fully understand it or see the full power yet, but I
know/heard it is very powerful

Thanks

Lit.



这篇关于谓语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆