<p>I need a list of integers from 1 to x where x is set by the user.  I could build it with a for loop eg assuming x is an integer set previously:</p>

<pre><code>List&lt;int&gt; iList = new List&lt;int&gt;();
for (int i = 1; i &lt;= x; i++)
{
    iList.Add(i);
}
</code></pre>

<p>This seems dumb, surely there's a more elegant way to do this, something like the <a href="http://au2.php.net/manual/en/function.range.php" rel="noreferrer">PHP range method</a></p>

[{'answer_id': 49216, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 6, 'selected': False, 'text': '<p>LINQ to the rescue:</p>\n\n<pre><code>// Adding value to existing list\nvar list = new List&lt;int&gt;();\nlist.AddRange(Enumerable.Range(1, x));\n\n// Creating new list\nvar list = Enumerable.Range(1, x).ToList();\n</code></pre>\n\n<p>See <a href="http://msdn.microsoft.com/en-us/vcsharp/aa336752.aspx#range" rel="noreferrer">Generation Operators</a> on <a href="http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx" rel="noreferrer">LINQ 101</a></p>\n'}, {'answer_id': 49218, 'author': 'jfs', 'author_id': 718, 'author_profile': 'https://Stackoverflow.com/users/718', 'pm_score': 8, 'selected': True, 'text': '<p>If you\'re using .Net 3.5, <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx" rel="noreferrer">Enumerable.Range</a> is what you need.</p>\n\n<blockquote>\n  <p>Generates a sequence of integral\n  numbers within a specified range.</p>\n</blockquote>\n'}, {'answer_id': 49342, 'author': 'Samuel Jack', 'author_id': 1727, 'author_profile': 'https://Stackoverflow.com/users/1727', 'pm_score': 4, 'selected': False, 'text': '<p>I\'m one of many who has <a href="http://blog.functionalfun.net/2008/04/project-euler-problem-4.html" rel="noreferrer">blogged</a> about a ruby-esque <em>To</em> extension method that you can write if you\'re using C#3.0:</p>\n\n<p><pre><code>\npublic static class IntegerExtensions\n{\n    public static IEnumerable&lt;int&gt; To(this int first, int last)\n    {\n        for (int i = first; i &lt;= last; i++)<br>\n        { \n            yield return i;\n        } \n    }\n}\n</pre></code></p>\n\n<p>Then you can create your list of integers like this</p>\n\n<p><pre><code>List&lt;int&gt; = first.To(last).ToList();</pre></code> </p>\n\n<p>or\n<pre><code>List&lt;int&gt; = 1.To(x).ToList();</pre></code></p>\n'}, {'answer_id': 36462316, 'author': 'Gaspare Bonventre', 'author_id': 3976189, 'author_profile': 'https://Stackoverflow.com/users/3976189', 'pm_score': 1, 'selected': False, 'text': '<p>Here is a short method that returns a List of integers.</p>\n\n<pre><code>    public static List&lt;int&gt; MakeSequence(int startingValue, int sequenceLength)\n    {\n        return Enumerable.Range(startingValue, sequenceLength).ToList&lt;int&gt;();\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/49214', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2975/']