C#方法带不确定个数参数

先做一道MCSD试题;

C#方法带不确定个数参数_第1张图片

你正在实现一个名为"Show"的方法,它可带有不限制个数的整型参数。你如何定义它的方法声明?

method signature
方法签名; 方法声明;

答案是B;下面做一下程序;

    void Button1Click(object sender, EventArgs e)
		{
			textBox1.Text = UseParams(1,2,3);
			textBox2.Text = UseParams(1,2,3,4,5,6,7,99,1001);
			int[] myarray = new int[3] { 100, 101, 122 };
			textBox3.Text =  UseParams(myarray);
			textBox4.Text = UseParams2(11, "abc", new object());
		}
		
		private string UseParams(params int[] list)
    {
			string str1 = "";
      for (int i = 0; i < list.Length; i++)
      {
        str1 = str1 + list[i].ToString() + ";";
      }
      return str1;
    }
 
    private string UseParams2(params object[] list) 
    {
    		string str1="";
      for (int i = 0; i < list.Length; i++) 
      {
        str1 = str1 + list[i].ToString() + ";";
      }
      return str1;
    }

 运行如下;

C#方法带不确定个数参数_第2张图片 

 

你可能感兴趣的:(.Net,c#,params)