This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the => token called?
Hey,
In LINQ what is the name of the => operator e.g:
list.Where(a => a.value == 5);
It's called the lambda operator and is pronounced "goes to".
From here.
It's the lambda operator. Or at least, an expression of the form
x => y
(or any of the longer forms involving =>) is a lambda expression. (I don't personally tend to think of it as an operator as such, even though the linked page refers to the lambda operator. The C# 4 spec doesn't contain the phrase "lambda operator" anywhere.)
The lambda operator.
You read it as "such that". So your example reads as "list where a such that a dot value equals 5"
its called ... lambda
Its used to create delegates or expression tree types, and yes the new term for it is lambda expression
Related
This question already has an answer here:
LINQ - Query syntax vs method chains & lambda [closed]
(1 answer)
Closed 5 years ago.
In some cases I see LINQ written this way:
L.Select(_ => _.A).Where(...)
and in some other cases, I see this:
A = from B in C where (...)
Do these two syntaxes have different names?
I understand both, but they seem to be referred to as LINQ so I am a bit confused.
The first one is Method Syntax or Method extension syntax or Fluent
The second one is Query Syntax or Query Expression Syntax
This question already has answers here:
Lambda for getter and setter of property
(3 answers)
Closed 5 years ago.
I've read some code in setting property for a class like below
private int id;
public int ID {
get=>id;
set=>id=value;
}
and it works. But I'm totally confused by the lambda syntax.
I know that lambda expression would be (input-parameters) => expression.
how could it drop brackets in the left.
I've search the answer in google but not found. the likely answer would be that get&set should be equal to delegate, but I've no idea about this. Does someone has document about this?
You could find the usage of lambda of C# here
Quoted here:
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.
Note the (if any)
As Furmek mentioned, these are called expression bodied members. These are similar, but different than lambda functions.
Another way of writing this is:
public int ID { get; set; }
This question already has answers here:
Call constructor as a function in C#
(3 answers)
Closed 7 years ago.
I have the following lambda expression:
Something((o, i) => new TheNewSwiss(o, i));
Is there a shorthand syntax? Something simliar to this but for ctors?
Something((o, i) => TheNewSwiss.New(o,i));
Something(TheNewSwiss.New);
No, there's no equivalent of method group conversions for constructors. (I can see it being handy, but it doesn't exist.) You just need to use the lambda expression syntax you've got in your first snippet.
This question already has answers here:
Direct casting vs 'as' operator?
(16 answers)
Casting vs using the 'as' keyword in the CLR
(18 answers)
What is the difference between casting and using "as" in C#?
(8 answers)
Closed 10 years ago.
Assume, that there is an interface A and a variable 'x' of a class that implements that interface. Now I can perform these:
var a = (A) x;
Or:
var a = x as A;
I know that in the case of failure, the first statement will throw an InvalidCastException and the second will return null. But is there any other difference? Particurarly in performance?
By doing (A)x you are doing a cast which will definatly try and cast, if it can't cast there will be an exception.
If you use as it will either cast or be null.
However, you have all the example code you need to try this yourself so you could of potentially tried this before asking us what the code you have stated is going to do.
The as operator is like a cast operation. However, if the conversion isn't possible, as returns null instead of raising an exception.
You can use the as operator to perform certain types of conversions between compatible reference types or nullable types.
Consider the following example:
expression as type
The code is equivalent to the following expression except that the expression variable is evaluated only one time.
expression is type ? (type)expression : (type)null
Note that the as operator performs only reference conversions, nullable conversions, and boxing conversions. The as operator can't perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.
Refer:
as (C# Reference)
The first one attempts to convert immediately, the second one actually checks whether x is of type A.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
delegate keyword vs. lambda notation
I am using both anonymous methods and lambda expressions, but I couldn't find the difference between those expressions except syntax. Can any one please help me to find out the advantages of lambda expressions? In which scenarios would I need to use lambdas and in which would I need to go use anonymous methods?
There are subtle differences which is explained by Eric Lippert #
Lambda Expressions vs. Anonymous Methods, Part One
Lambda Expressions vs. Anonymous Methods, Part Two
Lambda Expressions vs. Anonymous Methods, Part Three
Update:
Another difference is that, Lambda expressions can be represented into Expression tress and can be parsed at runtime. While Anonymous methods can't.
They are same; in a lambda expression you can leave out the definition of your method parameters types, so it is less typing.
A lambda expression is a way to define your function in a mathematical way.