I’m a C# developer primarily. Wherever I can use it, my day-to-day language for getting stuff done is C#. The nature of my work dictates however that I sometimes have to stray into VB.NET territory and so for anyone in a similar situation, here is a brief outline of how you can translate your C# lambda expressions into VB.NET.
As a recap, let’s look at a lambda expression in C#:
List<string> ListOfStrings = new List<string>(); ListOfStrings.Add("hello"); ListOfStrings.Add("world"); ListOfStrings.Add("hey"); ListOfStrings.Add("yo"); var QueryResult = ListOfStrings.Where(x => x.Length == 5).ToList();
So above we are creating a simple list containing a bunch of strings called ListOfStrings. Then, we are creating a list called QueryResult, which is the result of the lambda expression. The list uses LINQ to find any values in the list which have a length of 5 characters and puts them into a list.
You’ve probably seen this many times, but if not, there’s a quick example of a C# lambda expression and you should check out this really good introduction to lambda expressions using C# examples here.
For those of you more familiar with lambdas in C# who just want to do the same thing in VB, read on.
So how do we do the same thing in VB.NET?
To start with, as most of the time I am using lambdas as part of a LINQ query, it’s worth mentioning that LINQ works in the same way in VB in that you define your predicate (in this case the product of the lambda) and then the query is not executed until you enumerate it in some way, so by using ToList() or FirstOrDefault() for example.
So in this regard, the only thing we need to concern ourselves with is how the predicate differs. The main difference that strikes me from doing this in C# is that in VB, we don’t use the lambda operator (=>). We still pass in an argument for the function to start with as we do on the left of the operator (eg: ‘x =>’) but we instead create a VB function and return the product of our predicate as if it were a regular function. That is not the easiest thing to visualise without code, so here is the above expression translated from C# to VB
Dim ListOfStrings As List(Of String) = new List(Of String) ListOfStrings.Add("hello") ListOfStrings.Add("world") ListOfStrings.Add("hey") ListOfStrings.Add("yo") Dim QueryResult = ListOfStrings.Where(Function(x) Return x.Length = 5 End Function).ToList()
The most obvious difference in VB.NET is that we don’t start with just the argument being passed into our lambda. We still have to tell the compiler that we are passing an anonymous function, through use of the Function keyword. Then we can pass in our arguments.
Following this, we use the Return keyword to return our predicate. This is in place of the => operator used in C#. All in all, not too complex and potential a little more readable to people not used to lambdas or anonymous functions in C#. I’ve got to say, I still prefer writing lambda expressions in C# but that is more about my overall preference for the language over VB.NET. Either way, I hope this helps to show the difference for people writing C# that occasionally to jump into VB.NET and still want some of that sweet syntactic sugar.