tags:

views:

848

answers:

5

Functionally, is there any difference (apart from syntax onbviously) between lambda expressions in C# and VB.Net?

EDIT: following up on CraigTP's answer: any references to the situation in .Net 4?

EDIT: I'm asking because I'm used to C#, but for a next project the customer asks VB.Net. We're not a priori against that. We realize that most language constructs are supported in both languages. However, we're particularly fond of the way C# implements lambda expressions. We would like to have an overview of the differences with VB.Net

EDIT: accepted CraigTP's answer for pointing out what I currently consider the most important difference.

So to summarize: VB.Net 9 does not support multiline statements in a lambda expression, and a lambda must always return a value. Both of these issues are addressed in VB.Net 10

+4  A: 

See no reason why there would be, at the end of the day the expressions will all build out to the same object structure - used by both languages under the cover (at least with the newest versions)

saret
+10  A: 

There's no functional difference, however, as Joe Albahari says in this forum post:

VB.NET doesn't support multi-statement lambda expressions or anonymous methods.

Note that this is based upon C# 3.0 and VB.NET 9.0 (ie. the Visual Studio 2008 versions of the languages) - I'm not sure if it still applies with Visual Studio 2010 (C# 4.0 and VB.NET 10.0 respectively).

EDIT:

As per Richard Szaley's and my own comments, VB.NET 10.0 (which will be part of Visual Studio 2010) DOES support multi-statement lambdas, and here is a link to an MSDN Channel 9 video that shows off this feature (along with many others!):

Lucian Wischik and Lisa Feigenbaum: What's new in Visual Basic 10

CraigTP
Multi-staement lambda expressions are supported in VB.NET 10
Richard Szalay
@Richard - Thanks for commenting. I had a feeling this was something they'd added to VB.NET 10.0 but wasn't 100% sure and didn't quite have the time (at the time of answering) to fully research this.
CraigTP
Here's a link to an MSDN Channel 9 video that shows off VB 10's support of multi-statement lambdas (and many more new features!): http://channel9.msdn.com/posts/Dan/Lucian-Wischik-and-Lisa-Feigenbaum-Whats-new-in-Visual-Basic-10/
CraigTP
+1  A: 

This might also be a good read regarding slight differences in use: Linq Training Guide

daft
+6  A: 

Lambda expressions in VB.NET (pre-2010) must return a value. For example, the following syntax was invalid in VB.NET 9, but is valid in VB.NET 10 (code from Mike McIntyre's blog):

Array.ForEach(numbers, Sub(n)
    Console.Write("Number: ")
    Console.WriteLine(n)
End Sub)
Richard Szalay
+4  A: 

The rules for generic method parameter type inference from implicitly typed lambda arguments are rather different in VB and C#.

We could list minor differences all day; this would probably go faster if you clarify why you're asking.

Eric Lippert
see my edited question
jeroenh