C# Params object[] args

C# params object[] args 可以传多个参数,可以不限制类型

static void Main(string[] args)

{

print("Information", new Field("Name", "mengyu"), new Field("AA", "BB"));

}

static void print(string a, params object[] args)

{

Console.WriteLine(a);

foreach (object obj in args)

{

Field field = (Field)obj;

Console.WriteLine(field.Name + " = " + field.Value);

}

}

class Field

{

private string name;

private string value;

public Field(string name, string value)

{

this.name = name;

this.value = value;

}

public string Name

{

get

{

return name;

}

set

{

name = value;

}

}

public string Value

{

get

{

return value;

}

set

{

this.value = value;

}

}

}