What a strange method call?

I just read a tutorial on Microsoft Surface. The following C # sample exists:

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 these two methods do, but I have never seen a method call like this:

  FindChildren(this.Photos, d => d.GetType() == typeof(ScatterViewItem), d => ((ScatterViewItem)d).Center = new Point(x,y)); 

Perhaps this is because I am a Java programmer. So can anyone explain what this syntax does?

+4
source share
4 answers

These are lambda expressions that generate anonymous methods.

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

it can be written as

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

this call will represent this method if it is written out:

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

The FindChildren method requires a common delegate (comparable to a function pointer in C, but strongly typed) Func<object, bool> (i.e. any method that takes an object and returns bool), which should be specified as a parameter, the lambda expression generates this on-the-fly method. (This is done by the compiler, therefore it is not executed at runtime and is fully checked by type and does not require performance loss).

+5
source

What you see with the following lambda expression C #

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

This is essentially the expression that the delegate creates. Some people like to think of them as built-in functions.

Here are some links to read.

+5
source

d => d.GetType() == typeof(ScatterViewItem) is called a lambda expression. It says: "For any DependencyObject, d, return (d.GetType () == typeof (ScatterViewItem))." This is an unnamed method, an anonymous method.

Long hands:

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

called with:

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

Predicate<DependencyObject> is a delegate type or "signature form". It may contain a reference to any method that takes a DependencyObject and returns a bool.

FindChildren knows how to use the provided predicate to evaluate what it should do. Since it accepts certain types of data and returns certain types of data, FindChildren does not care about how the operation occurs, it can simply call it and know that it will receive a useful answer.

+2
source

=> - the new lambda operator for defining functions on the fly.

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

+1
source

All Articles