Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding items to a generic list (novice)

Tags:

arrays

c#

list

add

I'm some what embarrassed to even ask this but I know there is a better way to do this I just don't know how

List<int> numbers = new List<int>(22);
numbers.Add(3);
numbers.Add(4);
numbers.Add(9);
numbers.Add(14);
numbers.Add(15);
//...
like image 537
Crash893 Avatar asked Dec 09 '22 10:12

Crash893


1 Answers

List<int> numbers = new List<int>(22) { 3, 4, 9, ..., 99 };

shorter than that? Only if your numbers follow a pattern which could be expressed mathematically.

This is the collection initializer.

like image 85
Femaref Avatar answered Feb 02 '23 01:02

Femaref