What does the => symbol mean?

J

jmDesktop

I was reading the June 2008 MSDN magazine and it has this:

Array.Find(_handlers, h => h.CanProcess(order));

What does the => symbol mean? I understand it is using polymorphism,
but I don't know what the symbol means. Thanks.

I did try and search first.


all code and url below

public class OrderProcessingModule {
private IOrderHandler[] _handlers;

public OrderProcessingModule() {
_handlers = new IOrderHandler[] {
new InternationalOrderHandler(),
new SmallDomesticOrderHandler(),
new LargeDomesticOrderHandler(),
};
}

public void Process (OrderStatusMessage orderStatusMessage,
Order order) {
// Apply the changes to the Order from the OrderStatusMessage
updateTheOrder(order);

// Find the first IOrderHandler that "knows" how
// to process this Order
IOrderHandler handler =
Array.Find(_handlers, h => h.CanProcess(order));

handler.ProcessOrder(order);
}

private void updateTheOrder(Order order) {
}
}


http://msdn.microsoft.com/en-us/magazine/cc546578.aspx
 
K

kodehoved

I was reading the June 2008 MSDN magazine and it has this:

Array.Find(_handlers, h => h.CanProcess(order));

What does the => symbol mean? I understand it is using polymorphism,
but I don't know what the symbol means. Thanks.

I did try and search first.

all code and url below

public class OrderProcessingModule {
private IOrderHandler[] _handlers;

public OrderProcessingModule() {
_handlers = new IOrderHandler[] {
new InternationalOrderHandler(),
new SmallDomesticOrderHandler(),
new LargeDomesticOrderHandler(),
};
}

public void Process (OrderStatusMessage orderStatusMessage,
Order order) {
// Apply the changes to the Order from the OrderStatusMessage
updateTheOrder(order);

// Find the first IOrderHandler that "knows" how
// to process this Order
IOrderHandler handler =
Array.Find(_handlers, h => h.CanProcess(order));

handler.ProcessOrder(order);
}

private void updateTheOrder(Order order) {
}

}

http://msdn.microsoft.com/en-us/magazine/cc546578.aspx

It's the new (C# 3.0) syntax used in lambda expressions. For more info
check this: http://msdn.microsoft.com/en-us/library/bb397687.aspx

Brian
 
J

Jon Skeet [C# MVP]

jmDesktop said:
I was reading the June 2008 MSDN magazine and it has this:

Array.Find(_handlers, h => h.CanProcess(order));

What does the => symbol mean? I understand it is using polymorphism,
but I don't know what the symbol means. Thanks.

It's a lambda expression, new in C# 3 (and not really anything to do
with polymorphism, I'm afraid).

If you're familiar with the anonymous methods of C# 2, lambda
expressions are somewhat similar, but much more concise in most cases.

A search for C# and "lambda expression" should give you lots of
information.
 
J

jmDesktop

It's a lambda expression, new in C# 3 (and not really anything to do
with polymorphism, I'm afraid).

If you're familiar with the anonymous methods of C# 2, lambda
expressions are somewhat similar, but much more concise in most cases.

A search for C# and "lambda expression" should give you lots of
information.

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

Curious. How would that statement have been written without the
lambda expression?
 
J

Jon Skeet [C# MVP]

Curious. How would that statement have been written without the
lambda expression?

With an anonymous method:
Array.Find(_handlers, delegate(Handler h) { return
h.CanProcess(order); });

Alternatively you could put the body in a separate method and do:

Array.Find(_handlers, CanOrderPredicate);

bool CanOrderPredicate(Handler h)
{
return h.CanProcess(order);
}

That only works (as written) if "order" is a member variable -
otherwise you'd need to create a new class to hold the order reference
(which is exactly what anonymous methods and lambda expressions do,
when they need to).

Jon
 
M

Marc Gravell

Array.Find(_handlers, h => h.CanProcess(order));
...
How would that statement have been written without the
lambda expression?

With an anonymous method, that would be:

Array.Find(_handlers, delegate (IOrderHandler h)
{ return h.CanProcess(order);}
);
Array.Find<IOrderHandler>(_handlers, delegate (IOrderHandler h)
{ return h.CanProcess(order);}
);

depending on whether C# 2 can deduce the generic-arg by itself (the C# 3
type inference is much stronger).

Or you can do it the long way... the important thing to note is that you
are using a "captured" variable ("order"), which makes it much trickier
to explain - but here's a stab ;-o

internal class Capture {
public Capture(Order order) {this.order = order;}
Order order;
public bool Predicate(IOrderHandler h)
{ return h.CanProcess(order);}
}

then in your method use:

Capture capture = new Capture(order);
... Array.Find(_handlers, capture.Predicate)

with/without the <IOrderHandler>, and with/without the "new
Predicate[blah]" - i.e. the longest version

... Array.Find<IOrderHandler>(_handlers,
new Predicate<IOrderHandler>(capture.Predicate))

All in all, you can see why a lambda (or anon-method) makes working with
captured variables very tidy

Jon had a recent article on the same...
http://csharpindepth.com/Articles/Chapter5/Closures.aspx

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top