28

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?

4
  • Probably because it can't work out that x and y are ints. It's always useful to include the full error message in a question about it. Aug 1, 2011 at 11:18
  • Because you need to specify the types of the arguments, as well as the return type
    – Rob
    Aug 1, 2011 at 11:19
  • Cool, it seams to be a problem when you try to store an expression tree within an variable of the type var. Makes sense because the expression tree is evaluated at execution time... But really cool. ;) Aug 1, 2011 at 11:23
  • 2
    @Rob even if you specify the types of x and y (the return type is given by the expression implicitly) you'd still get "Cannot assign lambda expression to an implicitly typed variable"
    – Rune FS
    Aug 1, 2011 at 11:27

2 Answers 2

18

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 quite 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)

14
  • 4
    As an addendum you can tell the compiler what is going on var add = (Func<int,int,int>)((x,y) => x + y); but that doesn't really gain you anything as you may as well put the func on the LHS!
    – Bob Vale
    Jun 11, 2013 at 8:39
  • But aren't it would be right for the compiler to make such a lambda polymorphic?
    – Hi-Angel
    Apr 7, 2015 at 7:07
  • 1
    By the way, if it worked, it would be great addition to generics which for unknown reasons made limited.
    – Hi-Angel
    Apr 7, 2015 at 17:39
  • 1
    Eh... but the ambiguity is irrelevant. The specific error is An implicitly typed local variable declaration cannot be initialized with 'anonymous method'. It does not give an error because it's ambiguous, it gives an error because the language does not allow it. Saying it's because it's ambiguous implies that if you could write it unambiguously it would all of a sudden be ok. The ambiguity has nothing to do with it, except as the reason why the designers of the language made it illegal. But as for why it's an error, it's because the language does not allow it. Dec 7, 2016 at 23:02
  • 1
    var formatDate = (DateTime dt) => dt.ToString("yyyy/MM/dd");. It's perfectly clear that the input type is DateTime and the return type is string. No reason why that shouldn't be compilable as Func<DateTime,string>, except that the language can also compile the lambda as Expression<Func<DateTime,string>>, so there is always that ambiguity.
    – Triynko
    Dec 15, 2016 at 16:20
13

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>>.

2
  • 1
    Also, it doesn't know that x and y are int's. Aug 1, 2011 at 11:23
  • 1
    Also, the problem of the type x and y being int would be soluble with this (int x, int y) => x + y. The main point is Func vs Expression Sep 7, 2017 at 22:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.