Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of "=>" in C#? [duplicate]

Tags:

syntax

c#

lambda

Possible Duplicate:
C# Lambda ( => )

For instance

Messenger.Default.Register<AboutToCloseMessage>(this, (msg) =>
        {
            if (msg.TheContainer == this.MyContainer) // only care if my container.
            {
                // decide whether or not we should cancel the Close
                if (!(this.MyContainer.CanIClose))
                {
                    msg.Execute(true); // indicate Cancel status via msg callback.
                }
            }
        });
like image 834
Rdeluca Avatar asked Feb 02 '26 11:02

Rdeluca


2 Answers

The

=>
Operator is used for Lambda Expressions.

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

It allows you to define an anonymous function "on the fly" and can be used to create delegates or expression tree types.

like image 61
invalidsyntax Avatar answered Feb 04 '26 23:02

invalidsyntax


It is a lambda, it allows you to create a function easily.

in your example you can also write:

Messenger.Default.Register<AboutToCloseMessage>(this, delegate(Message msg)
{
    if (msg.TheContainer == this.MyContainer) // only care if my container.
    {
        // decide whether or not we should cancel the Close
        if (!(this.MyContainer.CanIClose))
        {
            msg.Execute(true); // indicate Cancel status via msg callback.
        }
    }
});

or even

Messenger.Default.Register<AboutToCloseMessage>(this, foobar);

// somewhere after //
private void foobar(Message msg)
{
    if (msg.TheContainer == this.MyContainer) // only care if my container.
    {
        // decide whether or not we should cancel the Close
        if (!(this.MyContainer.CanIClose))
        {
            msg.Execute(true); // indicate Cancel status via msg callback.
        }
    }
}
like image 21
Baptiste Pernet Avatar answered Feb 05 '26 00:02

Baptiste Pernet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!