vb.net可变参数与参数省略
可变参数
ParamArray 修饰符使函数能够接受可变数量的参数。
- Public Function calcSum(ByVal ParamArray args() As Double) As Double
- calcSum = 0
- If args.Length <= 0 Then Exit Function
- For i As Integer = 0 To UBound(args, 1)
- calcSum += args(i)
- Next i
- End Function
Optional
指定在调用过程时可以省略过程参数。
必须为所有可选过程参数指定默认值。
- Public Function FindMatches(ByRef values As List(Of String),
- ByVal searchString As String,
- Optional ByVal matchCase As Boolean = False) As List(Of String)
- Dim results As IEnumerable(Of String)
- If matchCase Then
- results = From v In values
- Where v.Contains(searchString)
- Else
- results = From v In values
- Where UCase(v).Contains(UCase(searchString))
- End If
- Return results.ToList()
- End Function