Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lambda expression and var keyword in c# [duplicate]

Tags:

c#

lambda

Possible Duplicate:
C# Why can't an anonymous method be assigned to var?

I have following statement in c#

Func <int, int, int> add = (x, y) => x + y; 

But when I am replacing left hand side statement with following

var add = (x, y) => x + y; 

I am getting compiler error(Cannot assign lambda expression to an implicitly-typed local variable).Why?

like image 456
santosh singh Avatar asked Aug 01 '11 11:08

santosh singh


People also ask

Is var keyword in C?

C# var keyword is used to declare implicit type variables. Let's learn what is var in C# and when to use a var in C#.

What is lambda in C?

Lambda Function − Lambda are functions is an inline function that doesn't require any implementation outside the scope of the main program. Lambda Functions can also be used as a value by the variable to store. Lambda can be referred to as an object that can be called by the function (called functors).

What is lambda variable?

An environment variable is a pair of strings that is stored in a function's version-specific configuration. The Lambda runtime makes environment variables available to your code and sets additional environment variables that contain information about the function and invocation request.

Why do we use lambda expression in C#?

Lambda expressions in C# are used like anonymous functions, with the difference that in Lambda expressions you don't need to specify the type of the value that you input thus making it more flexible to use. The '=>' is the lambda operator which is used in all lambda expressions.


2 Answers

Because the compiler can't figure out what type the RHS has from

var add = (x, y) => x + y; 

Any type that supports the + operator is a candidate and since the type of x and y is not constraint to be of the same type. There's quit a lot of possible + operators that could be used and therefore the set of possible types for x and y is rather large but to be able to determine the type of add, the compiler need to be able to reduce the set to just one type for x and one for y (not exactly true, it might be that both a base class and a derived class would fit) and still even if the compiler could figure out the type for x and y or that you specified the types to let's say int you'd still be left with the fact that both Expression<Func<int,int,int>> and Func<int,int,int> are possible types for add

There are multiple options for how to reduce the set of possible types. The compiler could try to look at how add is used later but doesn't (and potentially couldn't figure the types out even if it did)

like image 146
Rune FS Avatar answered Oct 13 '22 19:10

Rune FS


The var keyword will not work, because lambda expressions are used for both delegates as expression trees and the compiler does not know to which it should convert the lambda. In other words, the following types are valid for your (x, y) => x + y lambda: Func<int, int, int> and Expression<Func<int, int, int>>.

like image 24
Steven Avatar answered Oct 13 '22 19:10

Steven