Clarification needed
Hi
I need some explanation of the following code...
Class Car
Public id As Integer
Public manufacturer As String
Public color As String
End ClassFunction GetCars() As List(Of Car)
Dim ret = New List(Of Car)
ret.Add(New Car With {.id = 1, .manufacturer = "Toyota", .color = "Red"})
ret.Add(New Car With {.id = 2, .manufacturer = "Honda", .color = "Red"})Return ret
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cars = GetCars()
Dim dictionary = cars.ToDictionary(Function(c) c.id)
End Sub
Where does the "c" in ToDictionary(Function(c) come from, and what is that?
Answers
- Its the vb way of doing lambda expressions
In c#:
var dictionary = cars.ToDictionary(c => c.id);
the same as Dim dictionary = cars.ToDictionary(Function(c) c.id)
Don't know all the high tech jizz maybe one of the mods can chime in but I'm pretty sure thats what it means.
http://msdn.microsoft.com/en-us/library/bb531253.aspx
Thats a good microsoft link into vb.net lambdas.
Hope this helps!
P.s. you can define the c in anyway you want doesnt have to be a c e.g. Dim dictionary = cars.ToDictionary(Function(testForLambda) testForLambda.id)- Proposed As Answer byLingzhi SunMSFT, ModeratorWednesday, November 11, 2009 10:10 AM
- Marked As Answer byLingzhi SunMSFT, ModeratorTuesday, November 17, 2009 12:47 AM
All Replies
- Its the vb way of doing lambda expressions
In c#:
var dictionary = cars.ToDictionary(c => c.id);
the same as Dim dictionary = cars.ToDictionary(Function(c) c.id)
Don't know all the high tech jizz maybe one of the mods can chime in but I'm pretty sure thats what it means.
http://msdn.microsoft.com/en-us/library/bb531253.aspx
Thats a good microsoft link into vb.net lambdas.
Hope this helps!
P.s. you can define the c in anyway you want doesnt have to be a c e.g. Dim dictionary = cars.ToDictionary(Function(testForLambda) testForLambda.id)- Proposed As Answer byLingzhi SunMSFT, ModeratorWednesday, November 11, 2009 10:10 AM
- Marked As Answer byLingzhi SunMSFT, ModeratorTuesday, November 17, 2009 12:47 AM
Hi magmo,
How is the problem now? I think d13mr3m1x has provided a really helpful reply.
If you need any further assistance, please feel free to let me know.
Have a nice weekend!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


