0

I have seen => used in sample code, but I don't know what this operand is called. Because the term is just symbols, Google search is not very helpful.

Here is an example of its use:

var rolesAllowed = rolePermission.Where(permission => permission.Permissions.Any(p => Demand.HasFlag(p.Type))).ToList();

This question is different from the ones so brazenly marked as "duplicate". I was looking for the name of the expression so I could research how to use it. The so-called duplicate item was for someone who already knew that => was called a lambda expression.

I want to thank the people who took the time to read and understand the question, and post a quality response.

Rob Cole
  • 721
  • 1
  • 6
  • 16

5 Answers5

5

It is called Lambda operator

To create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator =>, and you put the expression or statement block on the other side. For example, the lambda expression x => x * x specifies a parameter that’s named x and returns the value of x squared.

Lambda expressions use special syntax. They allow functions to be used as data such as variables or fields. The lambda expression syntax uses the => operator. This separates the parameters and statement body of the anonymous function.

The => operator can be read as "goes to" and it is always used when declaring a lambda expression.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
3

That is used to for lambda expressions. Essentially what that the Where function takes in is another function that iterates through the collection. The left side of the => defines the parameters and the right side is the actual function that gets run.

Andy Meyers
  • 1,561
  • 1
  • 15
  • 21
  • 3
    Great, thanks. So if I was to translate my sample code into an English sentence, it would be "Look for permissions in the Permissions class where that class is flagged as a specific type of permission, place it in a list, and assign it to the rolesAllowed variable"? – Rob Cole Apr 13 '13 at 20:07
  • That sounds like it describes the function above. – Andy Meyers Apr 13 '13 at 20:36
2

It is called lambda operator and it's the part of the lambda expression syntax.

Lambda expressions are very concise way to create anonymous methods.

Anonymous method example:

button1.Click += delegate(System.Object o, System.EventArgs e)
                   { MessageBox.Show("Click!"); };

The same code, but this time using lambda expression:

button1.Click += (o,e) => MessageBox.Show("Click!"); 

As shown, this syntax is a great shorthand notation for authoring anonymous methods, where a stack of arguments can be passed into a group of statements for processing. Any method in the .NET platform that takes a delegate object as an argument can be substituted with a related lambda expression, which will typically simplify your code base quite a bit.

milan-j
  • 619
  • 7
  • 15
1

This is the lambda operator, it creates a lambda expression, which is like an anonymous method.

Michael Greene
  • 10,343
  • 1
  • 41
  • 43
1

=> is a lambda operator, check the link to read more.

Also check this answer for more details.

Community
  • 1
  • 1
Mayank
  • 8,777
  • 4
  • 35
  • 60