how do I send two parameters as one in c#? -


my problem lies in have method takes variable amount of parameters.

each of parameters object, real problem lies in gets horribly verbose write new classname(p1, p2) every single parameter in method.

is there way send p1 , p2 single parameter in form of either {p1, p2} or (p1, p2)?

so can write insert(("john", "doe"), ("sherlock", "holmes"), ... etc) , pass news in method rather writing insert(new person("john", "doe"), new person("sherlock", "holmes"), ... etc)

i know tuples in f# , scala can way, using tuples in c# makes longer code

so there way make less verbose?

edit: i'm not looking create new arrays or new lists instead want avoid new keyword as possible

edit2: people requested see insert method looks like; looks this:

public void insert(params person[] arr) {     //inserts person in hash table     action<person> insert = (person) => _table[hasher(person.name)].add(person);      // calls insert function/action each person in parameter array     array.foreach(arr, insert); } 

you make collection supports initializer syntax , provide parameter methods. allow following:

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();     } } 

all that's required support such construct suitable void add method , implementing of ienumerable.


Comments