函式運算式 (Visual Basic)Function Expression (Visual Basic)

宣告定義函式 lambda 運算式的參數和程式碼。Declares the parameters and code that define a function lambda expression.

語法Syntax

Function ( [ parameterlist ] ) expression  
- or -  
Function ( [ parameterlist ] )  
  [ statements ]  
End Function  

組件Parts

詞彙Term 定義Definition
parameterlist 選擇性。Optional. 本機變數名稱的清單,代表此程式的參數。A list of local variable names that represent the parameters of this procedure. 即使清單是空的,括弧也必須存在。The parentheses must be present even when the list is empty. 請參閱參數清單See Parameter List.
expression 必要。Required. 單一運算式。A single expression. 運算式的類型是函式的傳回型別。The type of the expression is the return type of the function.
statements 必要。Required. 使用 Return 語句來傳回值的語句清單。A list of statements that returns a value by using the Return statement. (請參閱Return 語句)。傳回值的類型是函式的傳回型別。(See Return Statement.) The type of the value returned is the return type of the function.

備註Remarks

Lambda 運算式是沒有名稱的函式,會計算並傳回值。A lambda expression is a function without a name that calculates and returns a value. 除了 RemoveHandler的引數以外,您可以在任何可使用委派類型的位置使用 lambda 運算式。You can use a lambda expression anywhere you can use a delegate type, except as an argument to RemoveHandler. 如需委派的詳細資訊,以及搭配使用 lambda 運算式和委派的用法,請參閱委派語句寬鬆委派轉換For more information about delegates, and the use of lambda expressions with delegates, see Delegate Statement and Relaxed Delegate Conversion.

Lambda 運算式語法Lambda Expression Syntax

Lambda 運算式的語法類似于標準函式。The syntax of a lambda expression resembles that of a standard function. 差異如下:The differences are as follows:

  • Lambda 運算式沒有名稱。A lambda expression does not have a name.

  • Lambda 運算式不能有修飾詞,例如 OverloadsOverridesLambda expressions cannot have modifiers, such as Overloads or Overrides.

  • Lambda 運算式不會使用 As 子句來指定函式的傳回型別。Lambda expressions do not use an As clause to designate the return type of the function. 相反地,型別是從單行 lambda 運算式的主體評估為的值,或多行 lambda 運算式的傳回值推斷而來。Instead, the type is inferred from the value that the body of a single-line lambda expression evaluates to, or the return value of a multiline lambda expression. 例如,如果單行 lambda 運算式的主體是 Where cust.City = "London",則其傳回型別會 BooleanFor example, if the body of a single-line lambda expression is Where cust.City = "London", its return type is Boolean.

  • 單行 lambda 運算式的主體必須是運算式,而不是語句。The body of a single-line lambda expression must be an expression, not a statement. 主體可以包含函式程式的呼叫,而不是對 sub 程式的呼叫。The body can consist of a call to a function procedure, but not a call to a sub procedure.

  • 所有參數都必須具有指定的資料類型,否則必須推斷全部。Either all parameters must have specified data types or all must be inferred.

  • 不允許選擇性和 Paramarray 參數。Optional and Paramarray parameters are not permitted.

  • 不允許泛型參數。Generic parameters are not permitted.

範例Example

下列範例示範兩種建立簡單 lambda 運算式的方式。The following examples show two ways to create simple lambda expressions. 第一個使用 Dim 來提供函式的名稱。The first uses a Dim to provide a name for the function. 若要呼叫函式,請傳送參數的值。To call the function, you send in a value for the parameter.

Dim add1 = Function(num As Integer) num + 1
' The following line prints 6.
Console.WriteLine(add1(5))

範例Example

或者,您可以同時宣告和執行函數。Alternatively, you can declare and run the function at the same time.

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

範例Example

以下是 lambda 運算式的範例,它會遞增其引數並傳回值。Following is an example of a lambda expression that increments its argument and returns the value. 此範例會顯示函數的單行和多行 lambda 運算式語法。The example shows both the single-line and multiline lambda expression syntax for a function. 如需更多範例,請參閱Lambda 運算式For more examples, see Lambda Expressions.

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))

範例Example

Lambda 運算式會在 Language-Integrated Query (LINQ)Language-Integrated Query (LINQ)中的許多查詢運算子,而且可以在以方法為基礎的查詢中明確地使用。Lambda expressions underlie many of the query operators in Language-Integrated Query (LINQ)Language-Integrated Query (LINQ), and can be used explicitly in method-based queries. 下列範例顯示一般 LINQLINQ 查詢,然後將查詢轉譯成方法格式。The following example shows a typical LINQLINQ query, followed by the translation of the query into method format.

Dim londonCusts = From cust In db.Customers  
                       Where cust.City = "London"  
                       Select cust  
  
' This query is compiled to the following code:  
Dim londonCusts = db.Customers.  
                  Where(Function(cust) cust.City = "London").  
                  Select(Function(cust) cust)  

如需查詢方法的詳細資訊,請參閱查詢For more information about query methods, see Queries. 如需標準查詢運算子的詳細資訊,請參閱標準查詢運算子總覽For more information about standard query operators, see Standard Query Operators Overview.

請參閱See also