Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a list from two ints

Tags:

c#

I have some code that looks like this:

int A = 3;
int B = 5;
List<int> TheList = new List<int>();

TheList.Add(A);
TheList.Add(B);

SomeFunction(TheList);

Is there some way to write something like this:

SomeFunction((A,B).ToList());
like image 438
frenchie Avatar asked Feb 18 '23 19:02

frenchie


1 Answers

Yes:

new List<int>{A, B}

produces a list with the two elements that you specified. You can pass that list to a function or do anything else with it.

Note that if your target function takes IList<int> rather than a List<int>, you could shorten the syntax some more by sending a new array of ints, because arrays T[] implement their corresponding IList<T> interface.

like image 190
Sergey Kalinichenko Avatar answered Feb 27 '23 03:02

Sergey Kalinichenko