如何:建立 Lambda 運算式 (Visual Basic)

Lambda 運算式是沒有名稱的函式或副程式。 委派類型有效時,即可使用 Lambda 運算式。

建立單行 Lambda 運算式函式

  1. 在可使用委派類型的任何情況下,請輸入 Function 關鍵字,如下列範例所示:

    Dim add1 = Function

  2. 在括弧中,直接在 Function 後面輸入函式的參數。 請注意,請勿在 Function 後面指定名稱。

    Dim add1 = Function (num As Integer)

  3. 在參數清單後,輸入單一運算式作為函式的主體。 運算式評估的值即函式傳回的值。 請勿使用 As 子句指定傳回型別。

    Dim add1 = Function(num As Integer) num + 1
    

    您可以在整數引數中傳入並呼叫 Lambda 運算式。

    ' The following line prints 6.
    Console.WriteLine(add1(5))
    
  4. 或者,下列範例會達到相同的結果:

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

建立單行 Lambda 運算式副程式

  1. 在可使用委派類型的任何情況下,請輸入 Sub 關鍵字,如下列範例所示。

    Dim add1 = Sub

  2. 在括弧中,直接在 Sub 後面輸入副程式的參數。 請注意,請勿在 Sub 後面指定名稱。

    Dim add1 = Sub (msg As String)

  3. 在參數清單後面,輸入單一陳述式作為副程式主體。

    Dim writeMessage = Sub(msg As String) Console.WriteLine(msg)
    

    您可以在字串引數中傳入並呼叫 Lambda 運算式。

    ' The following line prints "Hello".
    writeMessage("Hello")
    

建立多行 Lambda 運算式函式

  1. 在可使用委派類型的任何情況下,請輸入 Function 關鍵字,如下列範例所示。

    Dim add1 = Function

  2. 在括弧中,直接在 Function 後面輸入函式的參數。 請注意,請勿在 Function 後面指定名稱。

    Dim add1 = Function (index As Integer)

  3. 按 ENTER 鍵。 End Function 陳述式會自動新增。

  4. 在函式主體中,新增下列程式碼建立運算式並傳回值。 請勿使用 As 子句指定傳回型別。

    Dim getSortColumn = Function(index As Integer)
                            Select Case index
                                Case 0
                                    Return "FirstName"
                                Case 1
                                    Return "LastName"
                                Case 2
                                    Return "CompanyName"
                                Case Else
                                    Return "LastName"
                            End Select
                        End Function
    

    您可以在整數引數中傳入並呼叫 Lambda 運算式。

    Dim sortColumn = getSortColumn(0)
    

建立多行 Lambda 運算式副程式

  1. 在可使用委派類型的任何情況下,請輸入 Sub 關鍵字,如下列範例所示:

    Dim add1 = Sub

  2. 在括弧中,直接在 Sub 後面輸入副程式的參數。 請注意,請勿在 Sub 後面指定名稱。

    Dim add1 = Sub (msg As String)

  3. 按 ENTER 鍵。 End Sub 陳述式會自動新增。

  4. 在函式主體中,新增下列程式碼,即可在叫用副程式時執行。

    Dim writeToLog = Sub(msg As String)
                         Dim log As New EventLog()
                         log.Source = "Application"
                         log.WriteEntry(msg)
                         log.Close()
                     End Sub
    

    您可以在字串引數中傳入並呼叫 Lambda 運算式。

    writeToLog("Application started.")
    

範例

Lambda 運算式常見的用法是針對 Delegate 類型的參數,定義可作為引數傳入函式。 在下列範例中,GetProcesses 方法會傳回本機電腦上執行程序的陣列。 Enumerable 類別的 Where 方法需要 Boolean 委派為類別的引數。 範例中的 Lambda 運算式即用於此用途。 Lambda 運算式會針對只有一個執行緒的每個程序傳回 True,並在 filteredList 中選取這些程序。

Sub Main()

    ' Create an array of running processes.
    Dim procList As Process() = Diagnostics.Process.GetProcesses

    ' Return the processes that have one thread. Notice that the type
    ' of the parameter does not have to be explicitly stated.
    Dim filteredList = procList.Where(Function(p) p.Threads.Count = 1)

    ' Display the name of each selected process.
    For Each proc In filteredList
        MsgBox(proc.ProcessName)
    Next

End Sub

上述範例相當於下列程式碼,而此程式碼是以 Language-Integrated Query (LINQ) 語法撰寫:

Sub Main()

    Dim filteredQuery = From proc In Diagnostics.Process.GetProcesses
                        Where proc.Threads.Count = 1
                        Select proc

    For Each proc In filteredQuery
        MsgBox(proc.ProcessName)
    Next
End Sub

另請參閱