What does operator '=>' mean in C #?

What does '=>' mean in this expression?

del = new SomeDelegate(() => SomeAction()); 

Is the above expression the same as this?

 del = new SomeDelegate(this.SomeAction); 

Thanks.

+6
c # lambda delegates
source share
4 answers

This basically specifies an anonymous function that does not accept parameters calling SomeAction. So yes, they are functionally equivalent. Although not equal. Using lambda is more equivalent:

 del = new SomeDelegate(this.CallSomeAction); 

where CallSomeAction is defined as:

 public void CallSomeAction() { this.SomeAction(); } 

Hope this helps!

+7
source share

They do the same, but the syntax "() => ..." is what is called a lambda expression, and as such is similar to an anonymous function. You could probably completely exclude the delegate part and just let the compiler infer the delegate type for you.

 del = this.SomeAction; 

Depending on what type of "del" stands out as.

Edit

Using lambdas or anonymous methods or just a regular starter method allows you to map methods that didn't have a delegate signature.

For example, let's say you have a delegate with the signature bool myDelegate (int, int), but you wanted to have a method with the signature bool myMethod (string, string) that processes the delegate. You can then use the lambda expression to allow you to do this in a string format with short syntax like this.

 delegate bool myDelegate(int x, int y); // With lambdas... myDelegate del = (int a, int b) => myMethod(a.ToString(), b.ToString()); // With anonymous methods... myDelegate del2 = delegate(int a, int b) { return myMethod(a.ToString(), b.ToString()); }; // The good ol' way without lambdas or anonymous methods... myDelegate del3 = SomeDelegateHandler; ... then write a method somewhere else ... // Handler method private bool SomeDelegateHandler(int a, int b) { return myMethod(a.ToString(), b.ToString()); } 

So you can see that lambdas and anonymous methods basically represent a shorter / inline way to create a method to handle the delegate. In this case, you may not need an additional method. It only depends on whether the delegate's signature is the same as your method signature, and it seems to me that it is.

+4
source share

"=>" can read "goes" ( source: Eric Lippert ) and just separates the argument from the operation in the lambda expression. In this case, the lambda is full. Best examples:

 var subList = list.FindAll(item => item.Type == "Foo"); 

(find all elements where element type is Foo)

etc .. In C # 2.0 this can also be written:

 var subList = list.FindAll(delegate(SomeType item) { return item.Type == "Foo";}); 

And this is a quick way to express the "inline" function, as well as support for "closing", i.e. It can also be:

 string itemType = ... var subList = list.FindAll(item => item.Type == itemType); 

For this, otherwise, type-definiton is required to be passed in the element type:

 class Searcher { public string itemType; public bool Find(SomeType item) {return item.Type == itemType;} } ... Searcher searcher = new Searcher(); searcher.itemType = ... var subList = list.FindAll(searcher.Find); 

In fact, this is exactly what the compiler does for us (both for "delegation" and for lambda use). The biggest difference is that lambda can also express Expression , for example for LINQ.

+4
source share

=> is a Lambda Operator , lambda expressions are similar to the evolution of C # 2.0 anonymous methods .

You can use anonymous methods and lambda expressions in a very similar way to instantiate a delegate:

 Func<string,int> wordCount; wordCount = delegate (string text) { return text.Split().Length; }; Console.WriteLine (wordCount("Hello World")); 

Using lambda expressions:

 Func<string,int> wordCount; wordCount = (string text) => { text.Split().Length; }; Console.WriteLine (wordCount("Hello World")); 
+2
source share

All Articles