开发者

What weird method call is this?

开发者 https://www.devze.com 2023-01-20 21:45 出处:网络
I just read the Microsoft Surface Tutorial. There is the following C# sample: private void OnCenterItems(object sender, RoutedEventArgs e)

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?


These are lambda expressions, generating anonymous methods.

d => d.GetType() == typeof(ScatterViewItem)

this can be written as

(object d) => { return (d.GetType() == typeof(ScatterViewItem)); }

this call would represent this method, if written out:

public bool CheckObjecteEqualsScatterViewItemType (object d)
{
  return d.GetType() == typeof(ScatterViewItem);
}

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).


What your seeing with the following is a C# lambda expression

d => d.GetType() == typeof(ScatterViewItem)

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

  • http://msdn.microsoft.com/en-us/library/bb397687.aspx
  • http://msdn.microsoft.com/en-us/library/bb882516.aspx


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:

static bool myPredicate(DependencyObject d)
{
  return d.GetType() == typeof(ScatterViewItem);
}

called with:

FindChildren(this.Photos, myPredicate, [...] )

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.


The => is the new "lambda" operator, for defining functions on the fly.

See: www.switchonthecode.com/tutorials/csharp-tutorial-the-lambda-operator

0

精彩评论

暂无评论...
验证码 换一张
取 消