如何在c#中将两个参数作为一个参数发送?

Ele*_*fee 2 c# parameters tuples

我的问题在于我有一个采用可变数量参数的方法.

这些参数中的每一个都是一个对象,真正的问题在于new ClassName(p1, p2)为该方法中的每个参数写入非常冗长.

有没有办法发送p1p2作为单个参数的形式{p1, p2}(p1, p2)

所以我可以写Insert(("John", "Doe"), ("Sherlock", "Holmes"), ... etc),然后将这些传递给方法本身的新闻,而不是写Insert(new Person("John", "Doe"), new Person("Sherlock", "Holmes"), ... etc)

我知道F#中的元组和scala可以这样做,但在C#中使用元组只会使代码更长

有没有办法让它变得不那么冗长?

编辑:我不打算创建新数组或新列表,而是希望尽可能避免使用新关键字

Edit2:有些人要求查看我的Insert方法的样子; 目前它看起来像这样:

public void Insert(params Person[] arr)
{
    //inserts the person in a hash table
    Action<Person> insert = (person) => _table[hasher(person.Name)].Add(person);

    // calls the insert function/action for each person in the parameter array
    Array.ForEach(arr, insert);
}
Run Code Online (Sandbox Code Playgroud)

spe*_*der 5

您可以创建一个支持初始化程序语法的集合,并将其作为方法的参数提供.这将允许以下内容:

void Main()
{
    SomeMethod(new PersonCollection{{1, 2}, {3, 4}, {5, 6}});
}
void SomeMethod(PersonCollection pc)
{
}

//...
class Person
{
    public Person(int a, int b)
    {
    }
}
class PersonCollection:IEnumerable
{
    IList<Person> personList = new List<Person>();
    public void Add(int a, int b)
    {
        personList.Add(new Person(a,b));
    }

    public IEnumerator GetEnumerator()
    {
        return personList.GetEnumerator();
    }
}
Run Code Online (Sandbox Code Playgroud)

支持这种构造所需要的只是一种合适的void Add方法和实现方法IEnumerable.