日期:2014-05-19  浏览次数:21049 次

C#是否支持动态参数??
刚接触c#,请问C#是否支持动态参数??

------解决方案--------------------
private void example(params object[] args)
{
}
------解决方案--------------------
当然支持....
public int method(int a,string str,params object[] list)
{
//XXXX
}

调用的时候,可以这样调用

method(5, "sd ",5)
method(5, "sd ", "sd ",class1)
method(5, "sd ", "sd ",class1,5)
------解决方案--------------------
using System;
using System.Collections.Generic;

public class MyClass
{
public static void Main()
{
method(5, "2 ",3,4,5, "str ");
Console.ReadLine();
}
public static int method(int a,string str,params object[] st)
{
Console.WriteLine( "a:{0} ",a);
Console.WriteLine( "str:{0} ",str);
foreach(object obj in st)
{
Console.WriteLine( "obj:{0} ",obj);
}
return 0;
}
}
------解决方案--------------------
命令参数:
比如函数原形为
string SubString(string Source, int From, int To);

我可以这样调用
SubString(Source = "ABC ", To = 2, From = 1);

SubString( "ABC ", To = 2);