How do I pronounce = as used in lambda expressions in .Net
I usually say 'such that' when reading that operator.
In your example, p => p.Age > 16 reads as "P, such that p.Age is greater than 16."
In fact, I asked this very question on the official linq pre-release forums, and Anders Hejlsberg responded by saying
I usually read the => operator as "becomes" or "for which". For example,
Func f = x => x * 2;
Func test = c => c.City == "London";
reads as "x becomes x * 2" and "c for which c.City equals London"
As far as 'goes to' - that's never made sense to me. 'p' isn't going anywhere.
In the case of reading code to someone, say, over the phone, then as long as they're a fellow C# programmer, I'd just use the word 'lambda' - that is, "p lambda p dot age greater-than sixteen."
In comments Steve Jessop mentioned 'maps to' in the case of transformations - so taking Anders' example:
x => x * 2;
would read
x maps to x times 2.
That does seem much closer to the actual intention of the code than 'becomes' for this case.
How do you read lambda expressions?
Given that LINQ generally only works with IEnumerable objects, you could read s =>
as 'for each s in the IEnumerable'.
Update:
Revisiting this answer over 5 years on, I am deeply unsatisfied with it. Personally, nowadays I find myself considering it as "maps to" or I've seen "such that" which is also pertinent depending on the circumstance.
What is the = token called?
Lambda operator
What does '=' do in C#?
The =>
operator designates a Lambda Expression:
A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.
All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x." This expression can be assigned to a delegate type as follows:
static void Main(string[] args)
{
Func<int, int> func = x => x * x;
int j = func(5);
// j == 25
}
What's the name of the = operator in linq (e.g .Select(x=x.Prop1) )
In C# it's called a "lambda operator". In JS it's called an "arrow function operator".
References:
- https://msdn.microsoft.com/en-nz/library/bb311046.aspx
- http://www.ecma-international.org/ecma-262/6.0/#sec-arrow-function-definitions
C# lambda expressions without variable / parameter declaration?
Methods don't accept lambdas as parameters. They accept delegates as parameters. A lambda is just one way of creating a delegate.
Another way is supplying a method group, as is done in your second example, which can be converted to a delegate.
A similar way is to use the anonymous method feature. This was more or less replaced with lambdas when they were added though, so you don't see it much. Your example using that syntax would be:
Func<char, bool> predicate = delegate(char c) { return Char.IsDigit(c); };
Yet another way would be to create a delegate using Delegate.CreateDelegate
. (This isn't something you see all that often though.)
A final way is to have a delegate variable that you got from somewhere else. (That somewhere else would have created the delegate using one of these other options.)
What's going on in that second snippet, where the Char.IsDigit() method is (apparently) being called with an implicit parameter? What is this syntax called?
It's not being called. That's the whole point. We're trying to create a delegate. A delegate is an object that keeps track of a method to be invoked, and an object that it should be invoked on. You can then invoke the delegate and it will call the method that was used to create it. So here you're not calling IsDigit
, you're creating a delegate that is pointing to the IsDigit
method, and that will call it whenever that delegate is invoked.
When you use a lambda you're creating a new method, possibly in a new class, (neither of which have a name you can refer to, but they'll have one at runtime) and the body of that anonymous method will call IsDigit
. The lambda then resolves to a delegate pointing to that anonymous method, which maintains the semantics of the other example of having a method that when called, calls an anonymous method which, in its implementation, calls IsDigit
. It's adding an extra layer of indirection (that may or may not just get optimized out at runtime) to accomplish the same thing.
What does the = mean?
it is a lambda expression it means "goes to"
this is a pretty good link for explaining here
the left side is the input the right side the expression or statement block, here you are calling the LabelFor method inputing model and using the Email field on the model passed in.
Related Topics
How to Deserialize a JSON Array into an Object Using JSON.Net
Initialization of Instance Fields VS. Local Variables
How to Remove Accents on a String
Globally Convert Utc Datetimes to User Specified Local Datetimes
How to Connect to SQL Server Database from a Windows 10 Uwp App
Watermark in System.Windows.Forms.Textbox
Map Category Parent Id Self Referencing Table Structure to Ef Core Entity
System.Data.Sqlclient.Sqlexception: Login Failed for User
How to Enable Cross Origin Requests in ASP.NET MVC
ASP.NET Core Metadatatype Attribute Not Working
Delegate Caching Behavior Changes in Roslyn
C# Vertical Label in a Windows Forms
Can You Have a Property Name Containing a Dash
Getting a System.Type from Type's Partial Name
Force JSON.Net to Include Milliseconds When Serializing Datetime (Even If Ms Component Is Zero)