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());
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 int
s, because arrays T[]
implement their corresponding IList<T>
interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With