Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create List<int> with values at compile time

It is possible to create an array at compile time like;

int[] myValues = new int[] { 1, 2, 3 } ;

But I would like to do something like this;

List<int> myValues = new List<int>() { 1, 2, 3 };

The compiler says No. Is there a way to do this (C# 2.0) without using LINQ (C# 3.0)?

like image 358
Dead account Avatar asked Apr 27 '09 10:04

Dead account


People also ask

How to make list of integers in C#?

In the above example, List<int> primeNumbers = new List<int>(); creates a list of int type. In the same way, cities and bigCities are string type list. You can then add elements in a list using the Add() method or the collection-initializer syntax.

How to access values from list in C#?

Add(new string[] { "Id", "Name", "Age" }); for(int i=0; i<list. Count; i++) for(int j=0; j<list[i]. Count(); j++) Console. WriteLine(list[i][j]); // simpler with foreach foreach(var l in list) foreach(string s in l) Console.


4 Answers

List<int> myValues = new List<int>(new int[] { 1, 2, 3 } ); 

This will create an intermediate array however so there may be a more efficient way of doing the same thing.

EDIT:

John Feminella suggested creating a factory method to accept a list of parameters and return a List which you could implement as follows:

List<T> CreateList<T>(params T[] values) {     return new List<T>(values); } 

which you can use as follows:

List<int> myValues = CreateList(1, 2, 3); 
like image 85
Patrick McDonald Avatar answered Sep 22 '22 14:09

Patrick McDonald


Patrick has the answer you are looking for. But I wanted to add a little bit. If you want to do longer ranges of numbers and don't feel like typing them out by hand, you should look at the Enumerable.Range method. It can be used to generate a range of sequential numbers at runtime. For instance, your sample could have been written as follows

var list = Enumerable.Range(1,3).ToList(); 
like image 40
JaredPar Avatar answered Sep 24 '22 14:09

JaredPar


The way you suggests was first introduced in C# 3.0 (has nothing to do with LINQ, it was a language feature that was introduced).

There's no "shortcut" (list initialization) in C# 2.0 to do just that, either new up the list and then add the numbers manually via myValues.Add, or you could do the following:

int[] arrMyValues = new int[] {1, 2, 3};
List<int> myValues = new List<int>(arrMyValues);

List of T can take an IEnumerable of T in it's constructor, of which it'll include all T's in that IEnumerable in the list created, seeing as int[] implements IEnumerable of T you can "mix and match" the features like so.

Besides that, there's no way in C# 2.0 to do a such thing that you describe.

like image 35
kastermester Avatar answered Sep 21 '22 14:09

kastermester


The collection initializers are just syntax sugar, so you can use them in an application targeted at 2.0 if you compile with the 3.0 compiler.

like image 26
Martin Harris Avatar answered Sep 23 '22 14:09

Martin Harris