C#和VB.NET中Params的使用

C#中params关键字之后不允许存在任何其他参数。可以传入可变参数。

如下例:

        static void UseParams(params int[] list)

        {

            string temp = "";

            for (int i = 0; i < list.Length; i++)

            {

                temp = temp + " " + list[i].ToString();

            }

            Console.WriteLine(temp);

        }

        static void UseParams2(params object[] list)

        {

            string temp = "";

            for (int i = 0; i < list.Length; i++)

            {

                temp = temp + " " + list[i].ToString();

            }

            Console.WriteLine(temp);

        }

        static void Main()

        {

            UseParams(1, 2, 3);//看参数是3个

            UseParams(1, 2);   //看参数是2个,可变吧



            UseParams2(1, 'a', "test");



            int[] myarray = new int[3] { 10, 11, 12 };

            UseParams(myarray); //看也可以是容器类,可变吧:)

            Console.ReadKey();

        }

 

下面是VB中的用法:

    Public Class Test

Public Shared Sub add(ByVal ParamArray cc() As String)
For i As Integer = 0 To cc.Length - 1
Console.WriteLine(cc(i))
Next
End Sub

End Class

Test.add("A", "B", "C", "D", "E", "F")

输入变长字符串。

 

你可能感兴趣的:(VB.NET)