相关文章推荐
绅士的杨桃  ·  Vue-router报错Argument ...·  1 年前    · 
  • 在括号中,直接在 Function 之后,键入函数的参数。 请注意,不要在 Function 之后指定名称。

    Dim add1 = Function (num As Integer)

  • 在参数列表之后,键入一个表达式作为函数的主体。 表达式计算结果所得的值是函数返回的值。 请勿使用 As 子句指定返回类型。

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

    可通过传递整数参数来调用 Lambda 表达式。

    ' The following line prints 6.
    Console.WriteLine(add1(5))
    
  • 也可通过以下示例实现相同的结果:

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

    创建单行 Lambda 表达式子例程

  • 在可以使用委托类型的任何情况下,键入关键字 Sub,如下例所示。

    Dim add1 = Sub

  • 在括号中,直接在 Sub 之后,键入例程的参数。 请注意,不要在 Sub 之后指定名称。

    Dim add1 = Sub (msg As String)

  • 在参数列表后,键入单个语句作为子例程的主体。

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

    可通过传递字符串参数来调用 Lambda 表达式。

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

    创建多行 Lambda 表达式函数

  • 在可以使用委托类型的任何情况下,键入关键字 Function,如下例所示。

    Dim add1 = Function

  • 在括号中,直接在 Function 之后,键入函数的参数。 请注意,不要在 Function 之后指定名称。

    Dim add1 = Function (index As Integer)

  • 按 Enter。 End Function 语句会自动进行添加。

  • 在函数的主体中,添加以下代码以创建表达式并返回值。 请勿使用 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 表达式子例程

  • 在可以使用委托类型的任何情况下,键入关键字 Sub,如下例所示:

    Dim add1 = Sub

  • 在括号中,直接在 Sub 之后,键入例程的参数。 请注意,不要在 Sub 之后指定名称。

    Dim add1 = Sub (msg As String)

  • 按 Enter。 End Sub 语句会自动进行添加。

  • 在函数的主体中,添加以下代码,以在调用子例程时执行。

    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 表达式就是用于此目的。 它为每个只有一个线程的进程返回 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)
    End Sub
    

    上一个示例等效于以下用语言集成查询 (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)
    End Sub
    
  • Enumerable
  • Lambda 表达式
  • Function 语句
  • Sub 语句
  • 如何:在 Visual Basic 中将过程传递给另一过程
  • Delegate 语句
  • Visual Basic 中的 LINQ 简介
  •