共用方式為


Lambda 表達式 (Visual Basic)

Lambda 運算式是函式或子程式,沒有名稱,可在委派有效的地方使用。 Lambda 運算式可以是函式或子程式,而且可以是單行或多行。 您可以將目前範圍中的值傳遞至 Lambda 運算式。

備註

語句 RemoveHandler 是例外狀況。 您無法將 Lambda 表達式作為 RemoveHandler 的委派參數進行傳遞。

您可以使用 FunctionSub 關鍵詞來建立 lambda 運算式,就像建立標準函式或子程序一樣。 不過,Lambda 表達式會包含在語句中。

下列範例是 Lambda 表達式,會遞增其自變數並傳回值。 此範例會顯示函式的單行和多行 Lambda 表達式語法。

Dim increment1 = Function(x) x + 1
Dim increment2 = Function(x)
                     Return x + 2
                 End Function

' Write the value 2.
Console.WriteLine(increment1(1))

' Write the value 4.
Console.WriteLine(increment2(2))

下列範例是將值寫入主控台的 Lambda 表達式。 此範例會顯示子程式的單行和多行 Lambda 表達式語法。

Dim writeline1 = Sub(x) Console.WriteLine(x)
Dim writeline2 = Sub(x)
                     Console.WriteLine(x)
                 End Sub

' Write "Hello".
writeline1("Hello")

' Write "World"
writeline2("World")

請注意,在先前的範例中,Lambda 運算式會指派給變數名稱。 每當參考變數時,您就會叫用 Lambda 表達式。 您也可以同時宣告和叫用 Lambda 運算式,如下列範例所示。

Console.WriteLine((Function(num As Integer) num + 1)(5))

Lambda 表達式可以當做函數調用的值傳回(如本主題稍後的 Context 區段中所示),或以自變數形式傳入接受委派類型的參數,如下列範例所示。

Module Module2

    Sub Main()
        ' The following line will print Success, because 4 is even.
        testResult(4, Function(num) num Mod 2 = 0)
        ' The following line will print Failure, because 5 is not > 10.
        testResult(5, Function(num) num > 10)
    End Sub

    ' Sub testResult takes two arguments, an integer value and a 
    ' delegate function that takes an integer as input and returns
    ' a boolean. 
    ' If the function returns True for the integer argument, Success
    ' is displayed.
    ' If the function returns False for the integer argument, Failure
    ' is displayed.
    Sub testResult(ByVal value As Integer, ByVal fun As Func(Of Integer, Boolean))
        If fun(value) Then
            Console.WriteLine("Success")
        Else
            Console.WriteLine("Failure")
        End If
    End Sub

End Module

Lambda 表達式語法

Lambda 表達式的語法類似於標準函式或子程式。 差異如下:

  • Lambda 運算式沒有名稱。

  • Lambda 運算式不能有 修飾詞,例如 OverloadsOverrides

  • 單行 Lambda 函式不會使用 As 子句來指定傳回類型。 相反地,型別會從 Lambda 運算式主體評估的值推斷。 例如,如果 Lambda 表達式的主體為 cust.City = "London",則其傳回類型為 Boolean

  • 在多行 Lambda 函式中,您可以使用 子句來指定傳回類型 As ,或省略 As 子句,以便推斷傳回類型。 當省略多行 Lambda 函式的As子句時,傳回型別會推斷為多行 Lambda 函式中所有Return語句的最主要型別。 主要類型是所有其他類型可以擴展至的唯一類型。 如果無法判斷這個唯一類型,則主控類型是陣列中所有其他類型可以縮小到的唯一類型。 如果這兩個唯一類型都無法判斷,則主要類型為 Object。 在此情況下,如果 Option Strict 設定為 On,則會發生編譯程序錯誤。

    例如,如果提供給Return語句的運算式包含IntegerLongDouble類型的值,則產生的陣列為Double類型。 IntegerLong都會擴大至Double,且只有Double會擴大。 因此, Double 是主要類型。 如需詳細資訊,請參閱 擴大和縮小轉換

  • 單行函式的主體必須是傳回值的表達式,而不是語句。 單行函式沒有 Return 語句。 單行函式所傳回的值是函式主體中的運算式值。

  • 單行子程式主體必須是單行語句。

  • 單行函式和子程式不包含 End FunctionEnd Sub 語句。

  • 您可以使用 關鍵詞來指定 Lambda 運算式參數 As 的數據類型,也可以推斷參數的數據類型。 所有參數都必須具有指定的數據類型,或是必須推斷所有參數。

  • 不允許使用 OptionalParamarray 參數。

  • 不允許泛型參數。

非同步 Lambda 函式

您可以使用 AsyncAwait 運算符 關鍵詞,輕鬆地建立 Lambda 運算式和語句,併入異步處理。 例如,下列 Windows Forms 範例包含呼叫和等候異步方法的事件處理程式,ExampleMethodAsync

Public Class Form1

    Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' ExampleMethodAsync returns a Task.
        Await ExampleMethodAsync()
        TextBox1.Text = vbCrLf & "Control returned to button1_Click."
    End Sub

    Async Function ExampleMethodAsync() As Task
        ' The following line simulates a task-returning asynchronous process.
        Await Task.Delay(1000)
    End Function

End Class

您可以在 AddHandler 語句中使用異步 Lambda 來新增相同的事件處理程式。 若要新增此處理程式,請在 Lambda 參數清單之前新增 Async 修飾詞,如下列範例所示。

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler Button1.Click,
            Async Sub(sender1, e1)
                ' ExampleMethodAsync returns a Task.
                Await ExampleMethodAsync()
                TextBox1.Text = vbCrLf & "Control returned to Button1_ Click."
            End Sub
    End Sub

    Async Function ExampleMethodAsync() As Task
        ' The following line simulates a task-returning asynchronous process.
        Await Task.Delay(1000)
    End Function

End Class

如需如何建立和使用異步方法的詳細資訊,請參閱 使用 Async 和 Await 進行異步程序設計

上下文

Lambda 運算式會將其內容與定義所在的範圍共用。 它的存取權限與在包含範圍內撰寫的任何程式碼相同。 這包括存取包含範圍中的成員變數、函式和子變數、 Me以及參數和局部變數。

存取包含範圍中的局部變數和參數,可以延伸到該範圍的存留期之外。 只要參考 Lambda 表達式的委派無法用於垃圾收集,就會保留原始環境中變數的存取權。 在下列範例中,變數 target 是局部於方法 makeTheGame 的,而該方法是定義 Lambda 運算式 playTheGame 的地方。 請注意,指派給 takeAGuess 並在 Main 中傳回的 Lambda 表達式,仍然具有對局部變數 target 的存取權。

Module Module6

    Sub Main()
        ' Variable takeAGuess is a Boolean function. It stores the target
        ' number that is set in makeTheGame.
        Dim takeAGuess As gameDelegate = makeTheGame()

        ' Set up the loop to play the game.
        Dim guess As Integer
        Dim gameOver = False
        While Not gameOver
            guess = CInt(InputBox("Enter a number between 1 and 10 (0 to quit)", "Guessing Game", "0"))
            ' A guess of 0 means you want to give up.
            If guess = 0 Then
                gameOver = True
            Else
                ' Tests your guess and announces whether you are correct. Method takeAGuess
                ' is called multiple times with different guesses. The target value is not 
                ' accessible from Main and is not passed in.
                gameOver = takeAGuess(guess)
                Console.WriteLine("Guess of " & guess & " is " & gameOver)
            End If
        End While

    End Sub

    Delegate Function gameDelegate(ByVal aGuess As Integer) As Boolean

    Public Function makeTheGame() As gameDelegate

        ' Generate the target number, between 1 and 10. Notice that 
        ' target is a local variable. After you return from makeTheGame,
        ' it is not directly accessible.
        Randomize()
        Dim target As Integer = CInt(Int(10 * Rnd() + 1))

        ' Print the answer if you want to be sure the game is not cheating
        ' by changing the target at each guess.
        Console.WriteLine("(Peeking at the answer) The target is " & target)

        ' The game is returned as a lambda expression. The lambda expression
        ' carries with it the environment in which it was created. This 
        ' environment includes the target number. Note that only the current
        ' guess is a parameter to the returned lambda expression, not the target. 

        ' Does the guess equal the target?
        Dim playTheGame = Function(guess As Integer) guess = target

        Return playTheGame

    End Function

End Module

下列範例示範巢狀 Lambda 表達式的各種訪問許可權。 當傳回的 Lambda 表達式以 Main 的形式 aDel執行時,它會存取下列元素:

  • 定義類別的欄位: aField

  • 該類別中定義的屬性:aProp

  • 方法 functionWithNestedLambda的參數,其定義於其中: level1

  • functionWithNestedLambda局部變數: localVar

  • Lambda 表達式的參數,其為巢狀結構: level2

Module Module3

    Sub Main()
        ' Create an instance of the class, with 1 as the value of 
        ' the property.
        Dim lambdaScopeDemoInstance =
            New LambdaScopeDemoClass With {.Prop = 1}

        ' Variable aDel will be bound to the nested lambda expression  
        ' returned by the call to functionWithNestedLambda.
        ' The value 2 is sent in for parameter level1.
        Dim aDel As aDelegate =
            lambdaScopeDemoInstance.functionWithNestedLambda(2)

        ' Now the returned lambda expression is called, with 4 as the 
        ' value of parameter level3.
        Console.WriteLine("First value returned by aDel:   " & aDel(4))

        ' Change a few values to verify that the lambda expression has 
        ' access to the variables, not just their original values.
        lambdaScopeDemoInstance.aField = 20
        lambdaScopeDemoInstance.Prop = 30
        Console.WriteLine("Second value returned by aDel: " & aDel(40))
    End Sub

    Delegate Function aDelegate(
        ByVal delParameter As Integer) As Integer

    Public Class LambdaScopeDemoClass
        Public aField As Integer = 6
        Dim aProp As Integer

        Property Prop() As Integer
            Get
                Return aProp
            End Get
            Set(ByVal value As Integer)
                aProp = value
            End Set
        End Property

        Public Function functionWithNestedLambda(
            ByVal level1 As Integer) As aDelegate

            Dim localVar As Integer = 5

            ' When the nested lambda expression is executed the first 
            ' time, as aDel from Main, the variables have these values:
            ' level1 = 2
            ' level2 = 3, after aLambda is called in the Return statement
            ' level3 = 4, after aDel is called in Main
            ' localVar = 5
            ' aField = 6
            ' aProp = 1
            ' The second time it is executed, two values have changed:
            ' aField = 20
            ' aProp = 30
            ' level3 = 40
            Dim aLambda = Function(level2 As Integer) _
                              Function(level3 As Integer) _
                                  level1 + level2 + level3 + localVar +
                                    aField + aProp

            ' The function returns the nested lambda, with 3 as the 
            ' value of parameter level2.
            Return aLambda(3)
        End Function

    End Class
End Module

轉換成委派類型

Lambda 運算式可以隱式轉換成相容的委派類型。 如需相容性的一般需求相關信息,請參閱 寬鬆委派轉換。 例如,下列程式代碼範例會顯示隱含轉換成 Func(Of Integer, Boolean) 或相符委派簽章的 Lambda 表達式。

' Explicitly specify a delegate type.
Delegate Function MultipleOfTen(ByVal num As Integer) As Boolean

' This function matches the delegate type.
Function IsMultipleOfTen(ByVal num As Integer) As Boolean
    Return num Mod 10 = 0
End Function

' This method takes an input parameter of the delegate type. 
' The checkDelegate parameter could also be of 
' type Func(Of Integer, Boolean).
Sub CheckForMultipleOfTen(ByVal values As Integer(),
                          ByRef checkDelegate As MultipleOfTen)
    For Each value In values
        If checkDelegate(value) Then
            Console.WriteLine(value & " is a multiple of ten.")
        Else
            Console.WriteLine(value & " is not a multiple of ten.")
        End If
    Next
End Sub

' This method shows both an explicitly defined delegate and a
' lambda expression passed to the same input parameter.
Sub CheckValues()
    Dim values = {5, 10, 11, 20, 40, 30, 100, 3}
    CheckForMultipleOfTen(values, AddressOf IsMultipleOfTen)
    CheckForMultipleOfTen(values, Function(num) num Mod 10 = 0)
End Sub

下列程式代碼範例顯示隱含轉換成 Sub(Of Double, String, Double) 或相符委派簽章的 Lambda 表達式。

Module Module1
    Delegate Sub StoreCalculation(ByVal value As Double,
                                  ByVal calcType As String,
                                  ByVal result As Double)

    Sub Main()
        ' Create a DataTable to store the data.
        Dim valuesTable = New DataTable("Calculations")
        valuesTable.Columns.Add("Value", GetType(Double))
        valuesTable.Columns.Add("Calculation", GetType(String))
        valuesTable.Columns.Add("Result", GetType(Double))

        ' Define a lambda subroutine to write to the DataTable.
        Dim writeToValuesTable = Sub(value As Double, calcType As String, result As Double)
                                     Dim row = valuesTable.NewRow()
                                     row(0) = value
                                     row(1) = calcType
                                     row(2) = result
                                     valuesTable.Rows.Add(row)
                                 End Sub

        ' Define the source values.
        Dim s = {1, 2, 3, 4, 5, 6, 7, 8, 9}

        ' Perform the calculations.
        Array.ForEach(s, Sub(c) CalculateSquare(c, writeToValuesTable))
        Array.ForEach(s, Sub(c) CalculateSquareRoot(c, writeToValuesTable))

        ' Display the data.
        Console.WriteLine("Value" & vbTab & "Calculation" & vbTab & "Result")
        For Each row As DataRow In valuesTable.Rows
            Console.WriteLine(row(0).ToString() & vbTab &
                              row(1).ToString() & vbTab &
                              row(2).ToString())
        Next

    End Sub


    Sub CalculateSquare(ByVal number As Double, ByVal writeTo As StoreCalculation)
        writeTo(number, "Square     ", number ^ 2)
    End Sub

    Sub CalculateSquareRoot(ByVal number As Double, ByVal writeTo As StoreCalculation)
        writeTo(number, "Square Root", Math.Sqrt(number))
    End Sub
End Module

當您將 Lambda 運算式指派給委派或將它們當做自變數傳遞至程式時,您可以指定參數名稱,但省略其數據類型,讓類型取自委派。

範例

  • 下列範例會定義一個 Lambda 運算式,當可為 Null 的實值型別自變數具有指派的值時,傳回 True;如果其值為 False,則傳回 Nothing

    Dim notNothing =
      Function(num? As Integer) num IsNot Nothing
    Dim arg As Integer = 14
    Console.WriteLine("Does the argument have an assigned value?")
    Console.WriteLine(notNothing(arg))
    
  • 下列範例定義一個 lambda 表達式,以傳回陣列中最後一個元素的索引。

    Dim numbers() = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    Dim lastIndex =
      Function(intArray() As Integer) intArray.Length - 1
    For i = 0 To lastIndex(numbers)
        numbers(i) += 1
    Next
    

另請參閱