这是什么奇怪的方法调用?
我刚刚阅读了 Microsoft Surface 教程。有以下 C# 示例:
private void OnCenterItems(object sender, RoutedEventArgs e)
{
var x = this.Photos.ActualWidth / 2;
var y = this.Photos.ActualHeight / 2;
FindChildren(this.Photos, d => d.GetType() == typeof(ScatterViewItem),
d => ((ScatterViewItem)d).Center = new Point(x,y));
}
private void FindChildren(DependencyObject source,
Predicate<DependencyObject> predicate,
Action<DependencyObject> itemFoundCallback)
{
int childCount = VisualTreeHelper.GetChildrenCount(source);
for (int i = 0; i < childCount; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(source, i);
if (predicate(child))
{
itemFoundCallback(child);
}
FindChildren(child, predicate, itemFoundCallback);
}
}
我想我或多或少了解这两个方法在做什么,但我从未见过这样的方法调用:
FindChildren(this.Photos, d => d.GetType() == typeof(ScatterViewItem),
d => ((ScatterViewItem)d).Center = new Point(x,y));
这可能是因为我是一名 Java 程序员。那么谁能解释一下这个语法是做什么的?
I just read the Microsoft Surface Tutorial. There is the following C# sample:
private void OnCenterItems(object sender, RoutedEventArgs e)
{
var x = this.Photos.ActualWidth / 2;
var y = this.Photos.ActualHeight / 2;
FindChildren(this.Photos, d => d.GetType() == typeof(ScatterViewItem),
d => ((ScatterViewItem)d).Center = new Point(x,y));
}
private void FindChildren(DependencyObject source,
Predicate<DependencyObject> predicate,
Action<DependencyObject> itemFoundCallback)
{
int childCount = VisualTreeHelper.GetChildrenCount(source);
for (int i = 0; i < childCount; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(source, i);
if (predicate(child))
{
itemFoundCallback(child);
}
FindChildren(child, predicate, itemFoundCallback);
}
}
I think I understand more or less what this two methods are doing, but I never saw a method call like this:
FindChildren(this.Photos, d => d.GetType() == typeof(ScatterViewItem),
d => ((ScatterViewItem)d).Center = new Point(x,y));
This is maybe because I'm a Java programmer. So can anyone explain what this syntax is doing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这些是 lambda 表达式,生成匿名方法。
这可以写成
这个调用代表这个方法,如果写出来的话:
FindChildren 方法需要一个通用委托(相当于 C 中的函数指针,但强类型)Func
These are lambda expressions, generating anonymous methods.
this can be written as
this call would represent this method, if written out:
The method
FindChildren
requires a generic delegate (compareable to a function pointer in C, but strongly typed)Func<object, bool>
(ie. any method taking an object and returning a bool) to be given as parameter, the lambda expression generates this method on the fly. (This is done by the compiler, so this is not done at runtime and is fully type checked, and involves no loss of performance).您在下面看到的是一个 C# lambda 表达式,
它本质上是一个生成委托的表达式。有些人喜欢将它们视为内联函数。
以下是一些阅读参考
What your seeing with the following is a C# lambda expression
It's essentially an expression which produces a delegate. Some people like to think of them as inline functions.
Here are some references for reading
<代码> d => d.GetType() == typeof(ScatterViewItem) 称为 Lambda 表达式。它说“对于任何 DependencyObject,d,返回 (d.GetType() == typeof(ScatterViewItem))”。这是一个没有名字的方法,一个匿名方法。
长手将是:
用以下方式调用:
Predicate
是委托类型,或“签名形状”。它可以保存对任何采用 DependencyObject 并返回 bool 的方法的引用。FindChildren 知道使用提供的谓词来评估它应该执行的操作。因为它需要某些类型的数据并返回某些类型的数据,所以 FindChildren 并不关心操作如何发生,它只需调用它并知道它将得到有用的答案。
d => d.GetType() == typeof(ScatterViewItem)
is called a Lambda Expression. It says "For any DependencyObject, d, return (d.GetType() == typeof(ScatterViewItem))". It's a method without a name, an anonymous method.Long-hand would be:
called with:
Predicate<DependencyObject>
is a delegate-type, or "signature shape". It can hold a reference to any method which takes a DependencyObject and returns a bool.FindChildren knows to use the provided predicate to evaluate whatever it's supposed to do. Because it takes certain kinds of data and returns certain kinds of data, FindChildren doesn't care how the operation happens, it can just call it and know it will get a useful answer.
=>
是新的“lambda”运算符,用于动态定义函数。请参阅:www.switchonthecode.com/tutorials/csharp-tutorial-the-lambda - 运算符
The
=>
is the new "lambda" operator, for defining functions on the fly.See: www.switchonthecode.com/tutorials/csharp-tutorial-the-lambda-operator