1

What is the equivalent C# code for the following VB.NET:

Dim moo = Function(x as String) x.ToString()

I thought that this should work:

var moo = (string x) => x.ToString();

but that yielded a compiler error: Cannot assign lamda expression to an implicitly-typed local variable

After some further investigation, I discovered that the type of the variable moo (moo.GetType()) in the VB example is VB$AnonymousDelegate_0'2[System.String,System.String]

Is there anything equivalent to this in C#?

Jesan Fafon
  • 2,234
  • 1
  • 25
  • 29
  • Perhaps this is just example code, but it seems strange to convert a string to a string! – Chris Dunaway Jan 17 '14 at 16:33
  • Just an example :P The original was a local function in a VB file used to perform some string cleaning. This is one of the shortest examples that works for demonstrating an anonymous delegate in VB.NET – Jesan Fafon Jan 17 '14 at 16:45

4 Answers4

5

The lambda needs to infer the type of the delegate used from its context. An implicitly typed variable will infer its type from what is assigned to it. They are each trying to infer their type from the other. You need to explicitly use the type somewhere.

There are lots of delegates that can have the signature that you're using. The compiler needs some way of knowing which one to use.

The easiest option is to use:

Func<string, string> moo = x => x.ToString();

If you really want to still use var, you can do something like this:

var moo = new Func<string, string>(x => x.ToString());
Servy
  • 202,030
  • 26
  • 332
  • 449
  • 1
    If the compiler needs to know which type, then why does the VB work? – Jesan Fafon Jan 16 '14 at 18:34
  • @JesanFafon I have no idea. What is the type of the delegate in that case? How does it know which delegate to use? Perhaps the VB compiler will just pick one (at random?) for you. – Servy Jan 16 '14 at 18:35
  • 1
    In the VB case, `moo.GetType()` returns `VB$AnonymousDelegate_0'2[System.String,System.String]`. It inferred a delegate type which C# seems to not do at all. – Jesan Fafon Jan 16 '14 at 18:36
  • @JesanFafon So in that case it *created an entirely new delegate type on the spot*. That generally seems like a bad idea. You almost certainly won't have much that you could do that would be able to use such a type, so you could only ever execute it in the current context, making it not terribly useful as a practice. – Servy Jan 16 '14 at 18:37
  • 1
    while I agree, it looks like this delegate type is assignable as follows: `Dim moo2 as Func(of String, String) = moo` – Jesan Fafon Jan 16 '14 at 18:40
  • @JesanFafon So then why not just define that type right from the start? You're just deferring it until later. And in C# such an operation would not succeed. – Servy Jan 16 '14 at 18:41
0

You don't need to specify parameter type in lambda expressions, just do the following:

Func<string, string> moo = (x) => x.ToString();
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • You cannot assign a lambda to a `var` in C#, it doesn't know if you want the return value or not. – Guvante Jan 16 '14 at 18:32
0
Func<string, string> moo = (x) => x.ToString();

With var C# doesn't know if you want a Func and Action or something else.

Guvante
  • 18,775
  • 1
  • 33
  • 64
  • @Servy: Brain fart and fixed. Took me a second since I couldn't figure out what `x` was in the VB.Net (stopped doing VB before they added lambdas) – Guvante Jan 16 '14 at 18:35
0

The problem is that you can't use implicit typing for delegates. Meaning the var on the LHS is the actual cause of the issue, your lambda expression is fine. If you change it to the following it will compile and work as expected;

 Func<string, string> moo = (string x) => x.ToString();
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115