看了一些c#的资料,c#是不支持参数可选的.但大家都用过的Console.write()去可以参数可选,你可以传递无数过参数,请问这是如何实现的?
在网上查了一下资料是用的params,但是Console.write()的参数提示去是string value 请问c#如何实现参数可选的?
在网上查了一下资料是用的params,但是Console.write()的参数提示去是string value 请问c#如何实现参数可选的?
可选参数? 啥意思?
难道是 数组(object[]),list? 还是任意connection集合?
public Class ConSole
{
public void Print(string value)
{}
public void Print(int value)
{}
public void Print(byte value)
{}}
是这个意思?
protected void Button1_Click(object sender, EventArgs e)
{
TestFunction("A", "b", "C");
}
public void TestFunction(params string[] strings)
{ for (int i = 0; i < strings.Length; i++)
{
Response.Write(strings[i] + "<br>");
}
}
这样??
复制代码
// cs_params.cs
using System;
public class MyClass
{ public static void UseParams(params int[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
} public static void UseParams2(params object[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
} static void Main()
{
UseParams(1, 2, 3);
UseParams2(1, 'a', "test"); // An array of objects can also be passed, as long as
// the array type matches the method being called.
int[] myarray = new int[3] {10,11,12};
UseParams(myarray);
}
}
输出
1
2
31
a
test10
11
12
ps:这种问题msdn肯定会有答案的...