参数数目未知的函数或方法 [英] Function or Method with unknown number of parameters
本文介绍了参数数目未知的函数或方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法创建一个参数数量未知的方法?
Is there a way to create a method with unknown number of parameters?
如果是这样:
- 如何在此方法中访问它们?
- 它们必须来自同一类型吗?
推荐答案
Yes &是的.
Yes & yes.
这是可能的,而且它们都必须是相同的类型,如果您需要传递各种类型,请改用对象数据类型,然后在函数中将它们拆箱.使用参数数组:
it is possible, and all of them must be the same type, if you need to pass various types use object datatype instead, and then unbox them within function. use ParamArray:
' Accept variable number of arguments
Function Sum(ByVal ParamArray nums As Integer()) As Integer
Sum = 0
For Each i As Integer In nums
Sum += i
Next
End Function ' Or use Return statement like C#
Dim total As Integer = Sum(4, 3, 2, 1) ' returns 10
有关详细信息,请参阅此
这篇关于参数数目未知的函数或方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文