A simple generation of sequences?

I am looking for an ultralight way to generate a list of numbers, 1-200. (it could be List, Array, Enumerable ... I'm not interested in a specific type)

Apparently .NET 4.0 has a Sequence.Range (min, max) method. But I'm on .Net 3.5 now.

Here is an example of using what I need shown with Sequence.Range.

public void ShowOutput(Sequence.Range(1,200)); 

At the moment, I need consecutive numbers 1-200. In future iterations, I may need arbitrary lists of numbers, so I try to keep the design flexible.

Perhaps there is a good LINQ solution? Any other ideas?

+4
source share
3 answers

.NET 3.5 has a Range too. Actually Enumerable.Range and returns an IEnumerable<int> .

The page associated with you is very outdated - it speaks of 3 as a "future version", and the static Enumerable class was called Sequence one moment before the release.

If you want to implement it yourself in C # 2 or later, this is easy: here is one:

 IEnumerable<int> Range(int count) { for (int n = 0; n < count; n++) yield return n; } 

You can easily write other methods that further list filter lists:

 IEnumerable<int> Double(IEnumerable<int> source) { foreach (int n in source) yield return n * 2; } 

But since you have 3.5, you can use the extension methods in System.Linq.Enumerable to do this:

 var evens = Enumerable.Range(0, someLimit).Select(n => n * 2); 
+13
source
 var r = Enumerable.Range( 1, 200 ); 
+6
source

Check System.Linq.Enumerable.Range .

Regarding the second part of your question, what do you mean by "random lists"? If you can define a function from int for new values, you can use the Range result with other LINQ methods:

 var squares = from i in Enumerable.Range(1, 200) select i * i; 
+3
source

All Articles