Monday, May 13, 2013

lambda expression in vb.net

A function is a data object. A lambda expression is used to specify a function. It can be used as an argument to another function. In this case it is called a higher-order procedure. The VB.NET language has a special syntax form.
Higher-order procedures can serve as powerful abstraction mechanisms, vastly increasing the expressive power of our language.Abelson & Sussman, p. 57

Example

Rectangle
First, this example uses four lambda expressions. It uses these expressions to implement Func and Action instances. In the example, func1, func2, func3, and action1 are all assigned to lambda expressions.
Tip:The lambda expressions are found after the = sign and start with the Function or Sub keywords.
Tip 2:A Function is a method that returns a value. In contrast a Sub returns no value.
Program that uses lambda expressions: VB.NET

Module Module1
    Sub Main()
 ' Lambda expression that receives Integer, returns Integer.
 Dim func1 As Func(Of Integer, Integer) = Function(value As Integer)
           Return value + 1
       End Function

 ' Lambda expression that receives two Integers, returns Integer.
 Dim func2 As Func(Of Integer, Integer, Integer) =
     Function(value As Integer, value2 As Integer)
  Return value * value2
     End Function

 ' Lambda expression that receives Integer, returns String.
 ' ... Short syntax.
 Dim func3 As Func(Of Integer, String) = Function(x) (x + 1).ToString()

 ' Lambda expression that returns void.
 Dim action1 As Action = Sub()
        Console.WriteLine("action1")
    End Sub

 ' Use Func and Action instances.
 Console.WriteLine(func1.Invoke(4))
 Console.WriteLine(func2.Invoke(2, 3))
 Console.WriteLine(func3.Invoke(3))
 action1.Invoke()
    End Sub
End Module

Output

5
6
4
action1
Steps
In the example, the first two lambdas use the verbose Function syntax. The first example func1 is assigned to a lambda that receives one Integer and returns an Integer. The second example receives two Integers and also returns an Integer.
Integer
The third lambda expression uses the abbreviated syntax. With this syntax, you do not need to specify the type of the formal parameter. You do not need to use the End Function statement either.
Tip:There is no difference in the actual code generated with this syntax. This means performance is not affected.
Sub keyword
The final example shows the full syntax for a Sub() lambda expression. As with Function, you can use formal parameters (not shown) or the abbreviated syntax (also not shown). At the end of Main, we test all the delegate instances.
Sub

Discussion

Question and answer
What is so special about lambda expressions? There is actually no low-level difference between lambda expressions and regular Functions that are assigned with AddressOf. Lambda expressions are syntactic sugar.
AddressOf
Tip:This means they are a special syntax form in the VB.NET language that allows you to write more concise and clear programs.
Syntactic Sugar
Also:Lambda expressions can be used as arguments to List methods in the VB.NET language. More details are available.
List Find Function: FindIndex, FindLast

Summary

The VB.NET programming language
Lambda expressions are used throughout many new VB.NET programs. Lambda expressions are important for using the List type's searching methods. They are also essential for many LINQ methods.
Finally:Lambdas are useful for implementing delegates, as we saw in this example.

No comments:

Post a Comment