DotNet · 2022年5月2日

vb.net可变参数与参数省略

可变参数

ParamArray 修饰符使函数能够接受可变数量的参数。

Vb.net代码  收藏代码
  1. Public Function calcSum(ByVal ParamArray args() As Double) As Double  
  2.     calcSum = 0  
  3.     If args.Length <= 0 Then Exit Function  
  4.     For i As Integer = 0 To UBound(args, 1)  
  5.         calcSum += args(i)  
  6.     Next i  
  7. End Function  

 

Optional

指定在调用过程时可以省略过程参数。

必须为所有可选过程参数指定默认值。

 

Vb.net代码  收藏代码
  1. Public Function FindMatches(ByRef values As List(Of String),  
  2.                             ByVal searchString As String,  
  3.                             Optional ByVal matchCase As Boolean = False) As List(Of String)  
  4.   
  5.     Dim results As IEnumerable(Of String)  
  6.   
  7.     If matchCase Then  
  8.         results = From v In values  
  9.                   Where v.Contains(searchString)  
  10.     Else  
  11.         results = From v In values  
  12.                   Where UCase(v).Contains(UCase(searchString))  
  13.     End If  
  14.   
  15.     Return results.ToList()  
  16. End Function  

  

 

最新电影,电视剧,尽在午夜剧场

电影电视剧午夜不寂寞