<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
    <channel>
        <title>James Michael Hare</title>
        <link>http://blackrabbitcoder.net/Default.aspx</link>
        <description>...hare-brained ideas from the realm of software development...</description>
        <language>en-US</language>
        <copyright>James Michael Hare</copyright>
        <managingEditor>james.michael.hare@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <image>
            <title>James Michael Hare</title>
            <url>http://blackrabbitcoder.net/images/RSS2Image.gif</url>
            <link>http://blackrabbitcoder.net/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/BlackRabbitCoder" /><feedburner:info uri="blackrabbitcoder" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
            <title>C#/.NET Little Wonders: Select() and Where() with Indexes</title>
            <category>.NET</category>
            <category>C#</category>
            <category>Little Wonders</category>
            <category>My Blog</category>
            <category>Software</category>
            <link>http://feedproxy.google.com/~r/BlackRabbitCoder/~3/d6qcwKTZZqc/c.net-little-wonders-select-and-where-with-indexes.aspx</link>
            <description>&lt;div style="float: right"&gt;&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-0591047544853326";
/* Inline 2 */
google_ad_slot = "1318145269";
google_ad_width = 300;
google_ad_height = 250;
//--&gt;&lt;/script&gt;&lt;script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;&lt;/script&gt;&lt;ins style="position: relative; border-bottom-style: none; padding-bottom: 0px; margin: 0px; border-left-style: none; padding-left: 0px; width: 300px; padding-right: 0px; display: inline-table; border-top-style: none; height: 250px; visibility: visible; border-right-style: none; padding-top: 0px"&gt;&lt;ins style="position: relative; border-bottom-style: none; padding-bottom: 0px; margin: 0px; border-left-style: none; padding-left: 0px; width: 300px; padding-right: 0px; display: block; border-top-style: none; height: 250px; visibility: visible; border-right-style: none; padding-top: 0px" id="aswift_0_anchor"&gt;&lt;iframe style="position: absolute; top: 0px; left: 0px" id="aswift_0" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;amp;&amp;amp;s.handlers,h=H&amp;amp;&amp;amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;amp;&amp;amp;d&amp;amp;&amp;amp;(!d.body||!d.body.firstChild)){if(h.call){i+='.call';setTimeout(h,0)}else if(h.match){i+='.nav';w.location.replace(h)}s.log&amp;amp;&amp;amp;s.log.push(i)}" height="250" marginheight="0" frameborder="0" width="300" allowtransparency="allowtransparency" name="aswift_0" marginwidth="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;/ins&gt;&lt;/ins&gt;&lt;/div&gt;  &lt;p&gt;&lt;em&gt;Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders posts can be found &lt;a href="http://blackrabbitcoder.net/archive/2011/10/24/c.net-little-wonders-the-complete-collection-again.aspx"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;We’ve talked about the &lt;strong&gt;Select()&lt;/strong&gt; and &lt;strong&gt;Where()&lt;/strong&gt; LINQ extension methods before.  The &lt;strong&gt;Select()&lt;/strong&gt; method lets you project from the source type to a new type, and the &lt;strong&gt;Where()&lt;/strong&gt; method lets you filter the list of items to the ones you are interested in.  &lt;/p&gt;  &lt;p&gt;Most people know of these methods in their simplest form, where they simply take a projection and predicate respectively that operates on just an element.  However, there are overloads for both of these methods that take a delegate that operates on both the element and the index of the element.&lt;/p&gt;  &lt;p&gt;So let’s take a look at these and see what we can do with them.&lt;/p&gt;  &lt;h3&gt;Select() – Projects elements&lt;/h3&gt;  &lt;p&gt;The &lt;strong&gt;Select() &lt;/strong&gt;method is responsible for projecting a sequence into a new sequence which may or may not be different types and/or values.&lt;/p&gt;  &lt;p&gt;As an extension method, it’s most common form is:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Select(Func&amp;lt;T, TResult&amp;gt; projection)&lt;/strong&gt;       &lt;ul&gt;       &lt;li&gt;&lt;em&gt;Projects the &lt;/em&gt;&lt;strong&gt;source&lt;/strong&gt;&lt;em&gt; sequence into a new resulting sequence consisting of the results of each item passed through the &lt;/em&gt;&lt;strong&gt;projection&lt;/strong&gt; &lt;em&gt;delegate.&lt;/em&gt; &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;As you know, this gives us a lot of power, we can use it to change values (and keep types the same):&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// an array of 1 to 10&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; var numbers = Enumerable.Range(1, 10).ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;// converts to an array of 2, 4, 6, ..., 20&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; var doubles = numbers.Select(i =&amp;gt; i*2).ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt; &lt;/em&gt;&lt;/strong&gt;So that’s an example of a projection that changes the values, you can also project to a different type:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// the numbers from 1 to 10&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; var numbers = Enumerable.Range(1, 10).ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;// project to list of strings "1: ", "2: ", ...&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; var lineNumbers = numbers.Select(i =&amp;gt; i.ToString() + &lt;span style="color: #006080"&gt;": "&lt;/span&gt;).ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Now that we’ve reviewed the basic form, let’s look at a lesser-used overload of &lt;strong&gt;Select():&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Select(Func&amp;lt;T, int, TResult&amp;gt; projection)&lt;/strong&gt; 

    &lt;ul&gt;
      &lt;li&gt;&lt;em&gt;Projects the &lt;/em&gt;&lt;strong&gt;source &lt;/strong&gt;&lt;em&gt;sequence into a new resulting sequence consisting of the results of each item&lt;/em&gt; &lt;strong&gt;and its index in source&lt;/strong&gt;&lt;em&gt; passed through the&lt;/em&gt; &lt;strong&gt;projection&lt;/strong&gt;&lt;em&gt; delegate.&lt;/em&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note the difference here: in this overload the delegate takes not just an element, but an element and the &lt;em&gt;index&lt;/em&gt; of that element in the source sequence this method is invoked upon.&lt;/p&gt;

&lt;p&gt;Thus, if you had a list of items and wanted to take advantage of the item’s index as well in the projection, you can:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// say that racers consists of the names of racers in the order of finish:&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; IEnumerable&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt; racers = ...;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;// get a projection of new objects consisting of placing and name&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; var finishers = racers.Select((r, index) =&amp;gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; { Place = index, Name = r }).ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;The code snippet above will take a sequence of &lt;strong&gt;string&lt;/strong&gt; and convert it into a list of anonymous objects representing the racer’s name and their place.  So given this form of the &lt;strong&gt;Select()&lt;/strong&gt;, you can use the index as part of the projection and either store it, or use it to build a new value.&lt;/p&gt;

&lt;p&gt;One very important note, the &lt;em&gt;index &lt;/em&gt;provided to the projection is the index from the sequence the &lt;strong&gt;Select()&lt;/strong&gt; was immediately called from.  This is an important distinction because you can chain together multiple operations which may alter the number of items in the sequence (or their order) before the &lt;strong&gt;Select()&lt;/strong&gt; is invoked.  This is very similar to the way that &lt;a href="http://blackrabbitcoder.net/archive/2012/03/29/c.net-little-wonders-skip-and-take.aspx"&gt;Skip() and Take() (click here for a discussion of these two methods)&lt;/a&gt; work with their index overloads.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// fibs under 100&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; var numbers = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt;[] { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;// even fibs and their indexes?&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; var evenFibs = numbers&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;     .Where(f =&amp;gt; (f % 2) == 0)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;     .Select((f, index) =&amp;gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; { Number = f, Index = index })&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;     .ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;So looking at this code, you might think you’d get a list that contains Number = 2 at Index = 2, Number = 8 at Index 5, and Number = 34 at Index 8.  But instead you’ll get Number = 2 at Index = 0, Number = 8 at Index = 1, and Number = 34 at Index = 2.&lt;/p&gt;

&lt;p&gt;Why?  Because remember that the &lt;strong&gt;Where()&lt;/strong&gt; filters the sequence from (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89) down to just a sequence of the evens (2, 8, 34) and &lt;em&gt;that&lt;/em&gt; is the sequence the &lt;strong&gt;Select()&lt;/strong&gt; is operating on, thus that sequence is the one who’s indexes are used.  Make sense?  The &lt;strong&gt;Where() &lt;/strong&gt;returns a new, shorter sequence, and that is the sequence that &lt;strong&gt;Select()&lt;/strong&gt; uses for its elements and what it bases the indexes from.&lt;/p&gt;

&lt;p&gt;So, how would you get the results with the original indexes?  Well, one way would be to perform the &lt;strong&gt;Select()&lt;/strong&gt; first, and then filter the results with the &lt;strong&gt;Where()&lt;/strong&gt; second:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// fibs under 100&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; var numbers = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt;[] { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;// yes, this works...&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; var evenFibs = numbers&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;     .Select((f, index) =&amp;gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; { Number = f, Index = index })&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;     .Where(r =&amp;gt; (r.Number % 2) == 0)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;     .ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;So this one first projects the sequence into a sequence of an anonymous type holding both the number and the index, and then it filters down to only the those that have an even number.&lt;/p&gt;

&lt;h3&gt;Where() – filters elements&lt;/h3&gt;

&lt;p&gt;As you know, &lt;strong&gt;Where()&lt;/strong&gt; filters based on a predicate.  It’s basic form is:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Where(Func&amp;lt;T, bool&amp;gt; predicate)&lt;/strong&gt; 

    &lt;ul&gt;
      &lt;li&gt;&lt;em&gt;Filters the &lt;/em&gt;&lt;strong&gt;source &lt;/strong&gt;&lt;em&gt;sequence to a new sequence that contains only elements that return &lt;/em&gt;&lt;strong&gt;true &lt;/strong&gt;&lt;em&gt;from the &lt;/em&gt;&lt;strong&gt;predicate&lt;/strong&gt;&lt;em&gt;&lt;strong&gt; &lt;/strong&gt;applied to each element.&lt;/em&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, of course, as you’ve seen in the examples above, we can use &lt;strong&gt;Where()&lt;/strong&gt; to filter a sequence of numbers to just the evens:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// 1 through 10&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; var numbers = Enumerable.Range(1, 10);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;// 2, 4, 6, 8, 10&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; var evens = numbers.Where(n =&amp;gt; (n % 2) == 0).ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Just like &lt;strong&gt;Select(), &lt;/strong&gt;the &lt;strong&gt;Where()&lt;/strong&gt; extension method also has an overload that takes passes an index to the predicate:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Where(Func&amp;lt;T, int, bool&amp;gt; predicate)&lt;/strong&gt; &lt;/li&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;em&gt;Filters the &lt;/em&gt;&lt;strong&gt;source&lt;/strong&gt;&lt;em&gt; sequence to a new sequence that contains only elements that return &lt;/em&gt;&lt;strong&gt;true&lt;/strong&gt;&lt;em&gt; from the &lt;/em&gt;&lt;strong&gt;predicate &lt;/strong&gt;&lt;em&gt;applied to each element &lt;/em&gt;&lt;strong&gt;and its index in source.&lt;/strong&gt; F&lt;/li&gt;
  &lt;/ul&gt;
&lt;/ul&gt;

&lt;p&gt;For example, let’s say you wanted to pull every other item in a sequence, you could do this with the overload:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// fibs under 100&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; var numbers = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt;[] { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;// get every other fib number &lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; var everyOtherFib = numbers.Where((n, index) =&amp;gt; (index % 2) == 0).ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;The resulting sequence from the above will yield (1, 2, 5, 13, 34, 89).  These may seem like trivial uses, but you could also use them to coordinate across sequences.  For example, let’s say you kick off multiple service requests in various threads, and have an array of &lt;strong&gt;bool&lt;/strong&gt; representing whether the results came back successfully, we could filter the requests to get the list of failed requests:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt;[] wasSuccessful = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ...;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; &lt;span style="color: #008000"&gt;// get the list of requests that had bad return codes:&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; var failedRequests = requests.Where((r, index) =&amp;gt; !wasSuccessful[index]).ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;So the code above goes through the requests, takes the index of each request, and checks to see if &lt;strong&gt;wasSuccessful&lt;/strong&gt; at that same index is &lt;strong&gt;false.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, once again it is important to note that the index passed to the &lt;strong&gt;Where()&lt;/strong&gt; clause is the index of the item in the sequence &lt;strong&gt;Where()&lt;/strong&gt; is immediately called upon.  So, once again, if you need the index of the item in the original sequence, make sure that none of the clauses before your indexed &lt;strong&gt;Select(), Where(), Skip(), &lt;/strong&gt;or &lt;strong&gt;Take()&lt;/strong&gt; alter the sequence by re-ordering, adding, or removing items.&lt;/p&gt;

&lt;h3&gt;Sidebar: Naming style for index?&lt;/h3&gt;

&lt;p&gt;When you use the form of &lt;strong&gt;Select(), Where(), Skip(), &lt;/strong&gt;or &lt;strong&gt;Take()&lt;/strong&gt; where you provide a lambda expression that takes an index, it is often useful to name the lambda variables such that it’s clear which item is the index.  &lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// Ummm, is j the number and k the index?  or vice-versa...?&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; var evenFibs = numbers&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     .Where((j, k) =&amp;gt; (k % 2) == 0)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     .ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;In the code above, because &lt;strong&gt;j&lt;/strong&gt; and &lt;strong&gt;k&lt;/strong&gt; don’t really have any derivable meaning from the context, we have to know from experience (or Intellisense) that &lt;strong&gt;k&lt;/strong&gt; is the index.  However, many folks reading this code may not know that.&lt;/p&gt;

&lt;p&gt;This is why typically, and this is just my personal style, I like to name the index variable explicitly &lt;strong&gt;index, &lt;/strong&gt;or at least &lt;strong&gt;x&lt;/strong&gt; so it’s fairly clear that we’re talking about an index and not the value:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// Ah... it was the index!&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; var evenFibs = numbers&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     .Where((j, index) =&amp;gt; (index % 2) == 0)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     .ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Select()&lt;/strong&gt; and &lt;strong&gt;Where()&lt;/strong&gt; LINQ extension methods provide powerful ways to manipulate lists.  The &lt;strong&gt;Select()&lt;/strong&gt; clause is great for projecting from one sequence to another sequence containing different types/values, and the &lt;strong&gt;Where()&lt;/strong&gt; clause is useful for filtering a sequence down to only those that match a given predicate.  Both of these extension methods allow you to not only project and filter based on each element in the sequence, but also based on the element’s index in the sequence.  Care should be taken to realize that the index of the item may be altered by chained expressions.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:8acfbf44-1921-4711-9044-104b014bd887" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/C%23" rel="tag"&gt;C#&lt;/a&gt;,&lt;a href="http://technorati.com/tags/CSharp" rel="tag"&gt;CSharp&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Little+Wonders" rel="tag"&gt;Little Wonders&lt;/a&gt;,&lt;a href="http://technorati.com/tags/LINQ" rel="tag"&gt;LINQ&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Where" rel="tag"&gt;Where&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Select" rel="tag"&gt;Select&lt;/a&gt;&lt;/div&gt; &lt;img src="http://blackrabbitcoder.net/aggbug/149677.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/XuT6EunkQUKc9k9vCxgCiqYVcv8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XuT6EunkQUKc9k9vCxgCiqYVcv8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/XuT6EunkQUKc9k9vCxgCiqYVcv8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XuT6EunkQUKc9k9vCxgCiqYVcv8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BlackRabbitCoder/~4/d6qcwKTZZqc" height="1" width="1"/&gt;</description>
            <dc:creator>James Michael Hare</dc:creator>
            <guid isPermaLink="false">http://blackrabbitcoder.net/archive/2012/05/17/c.net-little-wonders-select-and-where-with-indexes.aspx</guid>
            <pubDate>Fri, 18 May 2012 00:11:35 GMT</pubDate>
            <wfw:comment>http://blackrabbitcoder.net/comments/149677.aspx</wfw:comment>
            <comments>http://blackrabbitcoder.net/archive/2012/05/17/c.net-little-wonders-select-and-where-with-indexes.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://blackrabbitcoder.net/comments/commentRss/149677.aspx</wfw:commentRss>
            <trackback:ping>http://blackrabbitcoder.net/services/trackbacks/149677.aspx</trackback:ping>
        <feedburner:origLink>http://blackrabbitcoder.net/archive/2012/05/17/c.net-little-wonders-select-and-where-with-indexes.aspx</feedburner:origLink></item>
        <item>
            <title>C#/.NET Little Wonders: The Enumerable.Repeat() Static Method</title>
            <category>.NET</category>
            <category>C#</category>
            <category>Little Wonders</category>
            <category>My Blog</category>
            <category>Software</category>
            <link>http://feedproxy.google.com/~r/BlackRabbitCoder/~3/QZd7xfVgA6I/c.net-little-wonders-the-enumerable.repeat-static-method.aspx</link>
            <description>&lt;div style="margin: 5px; float: right"&gt;&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-0591047544853326";
/* Inline 2 */
google_ad_slot = "1318145269";
google_ad_width = 300;
google_ad_height = 250;
//--&gt;&lt;/script&gt;&lt;script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;&lt;/script&gt;&lt;/div&gt;  &lt;p&gt;&lt;em&gt;Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders posts can be found &lt;a href="http://blackrabbitcoder.net/archive/2011/10/24/c.net-little-wonders-the-complete-collection-again.aspx"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;So last week we covered the &lt;strong&gt;Enumerable.Range()&lt;/strong&gt; method in the &lt;strong&gt;System.Linq&lt;/strong&gt; namespace, which gives us a handy way to generate a sequence of integers to either use directly, or to feed into a more complex expression.  Today we’re going to look at another static method called &lt;strong&gt;Enumerable.Repeat()&lt;/strong&gt; that allows us to repeat an element the specified number of times.&lt;/p&gt;  &lt;h3&gt;Using Repeat() to generate a sequence of a repeated value&lt;/h3&gt;  &lt;p&gt;Again, if we take a peek into the &lt;strong&gt;Enumerable&lt;/strong&gt; static class in the &lt;strong&gt;System.Linq&lt;/strong&gt; namespace, we see the vast array of LINQ extension methods, as well as a few static methods.  One of which is a method called &lt;strong&gt;Repeat()&lt;/strong&gt; whose purpose is to generate a sequence of repeated occurrences of a given element.&lt;/p&gt;  &lt;p&gt;It’s syntax looks like this:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Repeat(element, count)&lt;/strong&gt;       &lt;ul&gt;       &lt;li&gt;&lt;em&gt;Repeats the given &lt;/em&gt;element&lt;em&gt; for the specified &lt;/em&gt;count.  &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;So given this, if we wanted to create an array of 10 occurrences of the same string, we could do so by coding:&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// Creates a sequence of 5 occurrences of "Hello World!" and then stores in array&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; var repeatedValues = Enumerable.Repeat(&lt;span style="color: #006080"&gt;"Hello World!"&lt;/span&gt;, 5).ToArray();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;The main thing to notice in &lt;strong&gt;Repeat()&lt;/strong&gt; is that it repeats the given &lt;em&gt;element&lt;/em&gt;, it does not (at least not directly) repeatedly call an expression to generate multiple separate values.&lt;/p&gt;

&lt;p&gt;For example, consider the following code:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; var ran = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Random();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; &lt;span style="color: #008000"&gt;// does this generate 100 random numers?  &lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;// Or repeat first random number 100 times?&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; var repeatedRandom = Enumerable.Repeat(ran.Next(), 100);&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;What do we get?  Do we get a sequence of 100 random numbers?  Or a sequence of the first random number repeated 100 times?  &lt;/p&gt;

&lt;p&gt;The answer is the latter.  The &lt;strong&gt;ran.Next()&lt;/strong&gt; is resolved to get the value of the parameter, and then that result is the element that is repeated 100 times.  That is, if the first random number generated was 103242, then the array would contain { 103242, 103242, 103242, …, 103242 }.&lt;/p&gt;

&lt;p&gt;So, we can repeat a single element several times, right?  Obviously if that’s what you needed to do then it’s right up your alley, but once again if we look past the simple examples, we see some more interesting other things that this also enables us to do.&lt;/p&gt;

&lt;h3&gt;Using Repeat() to turn a single item into a sequence of one item&lt;/h3&gt;

&lt;p&gt;One of the nice things about &lt;strong&gt;Repeat()&lt;/strong&gt; is it allows us to easily represent a single item as a sequence of a single item.  For example, if you have a method that takes in an &lt;strong&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/strong&gt;, but you only have one &lt;strong&gt;T&lt;/strong&gt; to give it, &lt;strong&gt;Repeat()&lt;/strong&gt; makes it easy to pass in a sequence of length &lt;strong&gt;1.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example, if we had this method that takes an &lt;strong&gt;IEnumerabe&amp;lt;T&amp;gt;&lt;/strong&gt; sequence of &lt;strong&gt;string:&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// method that takes a sequence of string&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AddElements(IEnumerable&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt; elementSequence)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;But we just wanted to pass the single string &lt;strong&gt;“EOF”&lt;/strong&gt; into the method, we can use &lt;strong&gt;Repeat()&lt;/strong&gt; to do so:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// makes "EOF" into a sequence of 1 containing "EOF"&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; AddElements(Enumerable.Repeat(&lt;span style="color: #006080"&gt;"EOF"&lt;/span&gt;, 1));&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;So that’s a handy feature, though we could also, of course, do this by creating an explicit &lt;strong&gt;string[1]&lt;/strong&gt; array or a &lt;strong&gt;List&amp;lt;string&amp;gt;&lt;/strong&gt; as well.  In fact I have a blog post on &lt;a href="http://blackrabbitcoder.net/archive/2011/12/08/c-fundamentals-returning-zero-or-one-item-as-ienumerablelttgt.aspx"&gt;Returning Zero or One Item As IEnumerable&amp;lt;T&amp;gt; (here)&lt;/a&gt; where I talk about this in more detail, including weighing the performance and mutability ramifications.&lt;/p&gt;

&lt;p&gt;We could also create a sequence of size length &lt;strong&gt;0&lt;/strong&gt;, though this is a less interesting use of &lt;strong&gt;Repeat()&lt;/strong&gt; as the &lt;em&gt;element&lt;/em&gt; value would be unused and the &lt;strong&gt;Enumerable.Empty&amp;lt;T&amp;gt;() &lt;/strong&gt;singleton generator is more efficient if we know the sequence is intended to be empty (again, see the above mentioned blog post for more details).&lt;/p&gt;

&lt;h3&gt;Using Repeat() to repeat a generator to create sequences&lt;/h3&gt;

&lt;p&gt;We said before that &lt;strong&gt;Repeat()&lt;/strong&gt; can be used to repeat a given &lt;em&gt;element&lt;/em&gt; a specific number of times.  This makes it sound like this can only be used to generate sequences of a repeated value.  While this is generally true when you consider &lt;strong&gt;Repeat()&lt;/strong&gt; by itself, this is not necessarily true if you think beyond the &lt;em&gt;element&lt;/em&gt; representing a simple value.&lt;/p&gt;

&lt;p&gt;For example, let’s revisit the idea of creating a sequence of 100 random numbers, except this time, we will make the &lt;em&gt;element&lt;/em&gt; a &lt;strong&gt;Func&amp;lt;int&amp;gt; &lt;/strong&gt;that returns a random number.  So we will repeat the &lt;strong&gt;Func&amp;lt;int&amp;gt;&lt;/strong&gt; 100 times, well wait, how does that help us?  Won’t we just get the same reference to the delegate repeated 100 times?  Well yes, but that’s not where we’ll end, we’ll then use &lt;strong&gt;Select()&lt;/strong&gt; to invoke that delegate and return the result, giving us 100 random numbers:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; var ran = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Random();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; &lt;span style="color: #008000"&gt;// create a delegate from the method group,&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;// repeat that delegate 100 times,&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; &lt;span style="color: #008000"&gt;// for each delegate, project it to its result,&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt; &lt;span style="color: #008000"&gt;// convert to array.&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt; var results = Enumerable.Repeat&amp;lt;Func&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt;&amp;gt;(ran.Next, 100)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;     .Select(f =&amp;gt; f())&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     .ToArray();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Notice that we have to tell &lt;strong&gt;Repeat()&lt;/strong&gt; that the type of element is a &lt;strong&gt;Func&amp;lt;int&amp;gt;&lt;/strong&gt;, this is because it’s harder for the compiler to correctly infer the type &lt;strong&gt;Func&amp;lt;int&amp;gt; &lt;/strong&gt;from a method group or delegate.  Also notice our element is &lt;strong&gt;ran.Next&lt;/strong&gt; which is a method group and roughly equivalent to the lambda &lt;strong&gt;() =&amp;gt; ran.Next().&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, that delegate reference is in a sequence 100 times, and for each item, the &lt;strong&gt;Select() &lt;/strong&gt;projects it to the result from executing the delegate, which gives us a nice sequence of 100 random values.&lt;/p&gt;

&lt;p&gt;Note that we could have gotten a similar result by using &lt;strong&gt;Enumerable.Range()&lt;/strong&gt; as follows:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; var ran = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Random();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; &lt;span style="color: #008000"&gt;// create a sequence from 0 to 999 (1000 items),&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;// for each int, project to the next random number,&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; &lt;span style="color: #008000"&gt;// convert to array&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt; var results = Enumerable.Range(0, 1000)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;     .Select(i =&amp;gt; ran.Next())&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;     .ToArray();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;So in this method, we create a sequence of 1000 items (from 0 to 999) which we really don’t want to use, we’re more just using them to drive the &lt;strong&gt;Select()&lt;/strong&gt; projection to return 1000 random numbers.  For more information on &lt;strong&gt;Range()&lt;/strong&gt; see last week’s post on &lt;a href="http://blackrabbitcoder.net/archive/2012/04/26/c.net-little-wonders-the-enumerable.range-static-method.aspx"&gt;The Enumerable.Range() Static Method (here).&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both of these are similar ways to get the same results.  Some may prefer the first because &lt;strong&gt;Repeat()&lt;/strong&gt; seems a more natural idiom than &lt;strong&gt;Range() &lt;/strong&gt;for repeating a delegate, but it does have the baggage of creating two delegates (one for the generator, one for the projection) instead of the one for &lt;strong&gt;Range() &lt;/strong&gt;(only requires the one for projection)&lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Which is better?  Execution-wise, the use of &lt;strong&gt;Range&lt;/strong&gt; above is a hair faster (but really, the difference is very minor and of course that’s subject to change as the framework changes).  Other than that, it’s really up to you which way you prefer as they both accomplish the same goal.  &lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Enumerable.Repeat()&lt;/strong&gt; method performs the simple task of creating a sequence by repeating an element a specific number of times.  While this is, in of itself, a trivial need, it can also be used to drive more useful results such as repeating a generator delegate, or creating sequences out of single items.&lt;/p&gt;

&lt;table border="0"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/C%23" rel="tag"&gt;C#&lt;/a&gt;, &lt;a href="http://technorati.com/tags/CSharp" rel="tag"&gt;CSharp&lt;/a&gt;, &lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Little+Wonders" rel="tag"&gt;Little Wonders&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Enumerable" rel="tag"&gt;Enumerable&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Repeat" rel="tag"&gt;Repeat&lt;/a&gt;, &lt;a href="http://technorati.com/tags/LINQ" rel="tag"&gt;LINQ&lt;/a&gt; 

        &lt;div&gt;&lt;/div&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt; &lt;img src="http://blackrabbitcoder.net/aggbug/149529.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/yGskPAfqLRva4BHbcIvgwgGHjTs/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/yGskPAfqLRva4BHbcIvgwgGHjTs/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/yGskPAfqLRva4BHbcIvgwgGHjTs/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/yGskPAfqLRva4BHbcIvgwgGHjTs/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BlackRabbitCoder/~4/QZd7xfVgA6I" height="1" width="1"/&gt;</description>
            <dc:creator>James Michael Hare</dc:creator>
            <guid isPermaLink="false">http://blackrabbitcoder.net/archive/2012/05/03/c.net-little-wonders-the-enumerable.repeat-static-method.aspx</guid>
            <pubDate>Fri, 04 May 2012 00:21:38 GMT</pubDate>
            <wfw:comment>http://blackrabbitcoder.net/comments/149529.aspx</wfw:comment>
            <comments>http://blackrabbitcoder.net/archive/2012/05/03/c.net-little-wonders-the-enumerable.repeat-static-method.aspx#feedback</comments>
            <slash:comments>11</slash:comments>
            <wfw:commentRss>http://blackrabbitcoder.net/comments/commentRss/149529.aspx</wfw:commentRss>
            <trackback:ping>http://blackrabbitcoder.net/services/trackbacks/149529.aspx</trackback:ping>
        <feedburner:origLink>http://blackrabbitcoder.net/archive/2012/05/03/c.net-little-wonders-the-enumerable.repeat-static-method.aspx</feedburner:origLink></item>
        <item>
            <title>C#/.NET Little Wonders: The Enumerable.Range() Static Method</title>
            <category>.NET</category>
            <category>C#</category>
            <category>Little Wonders</category>
            <category>My Blog</category>
            <category>Software</category>
            <link>http://feedproxy.google.com/~r/BlackRabbitCoder/~3/LwrwsXP1exo/c.net-little-wonders-the-enumerable.range-static-method.aspx</link>
            <description>&lt;div style="margin: 5px; float: right"&gt;&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-0591047544853326";
/* Inline 2 */
google_ad_slot = "1318145269";
google_ad_width = 300;
google_ad_height = 250;
//--&gt;&lt;/script&gt;&lt;script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;&lt;/script&gt;&lt;/div&gt;  &lt;p&gt;&lt;em&gt;Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders posts can be found &lt;a href="http://blackrabbitcoder.net/archive/2011/10/24/c.net-little-wonders-the-complete-collection-again.aspx"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Thanks for all of your patience while I’ve been dealing with other matters these last few weeks.  I didn’t want to let my post slide a third week, so I decided to say a few words about a small static method in the &lt;strong&gt;Enumerable&lt;/strong&gt; class from &lt;strong&gt;System.Linq&lt;/strong&gt;.  &lt;/p&gt;  &lt;h3&gt;Using Range() to generates a sequence of consecutive integers&lt;/h3&gt;  &lt;p&gt;So, if we look in the &lt;strong&gt;Enumerable&lt;/strong&gt; static class, where most of the linq extension methods are defined, we will also see a &lt;strong&gt;static&lt;/strong&gt; method called &lt;strong&gt;Range()&lt;/strong&gt; whose purpose is to generate a range of integers from a given start value and for a given count.  &lt;/p&gt;  &lt;p&gt;It’s syntax looks like:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Range(int start, int count)&lt;/strong&gt;       &lt;ul&gt;       &lt;li&gt;&lt;em&gt;Returns a sequence of &lt;strong&gt;int&lt;/strong&gt; from &lt;strong&gt;start&lt;/strong&gt; to &lt;strong&gt;(start + count – 1).&lt;/strong&gt;&lt;/em&gt; &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;So, for example, the code snippet below will create an &lt;strong&gt;List&amp;lt;int&amp;gt; &lt;/strong&gt;of the numbers from &lt;strong&gt;1&lt;/strong&gt; to &lt;strong&gt;10&lt;/strong&gt;:&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; var numbers = Enumerable.Range(1, 10).ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;So, this seems simple enough, right? Well, yes, it is a handy way to create a sequence of consecutive &lt;strong&gt;int&lt;/strong&gt; values that you can use directly, but when coupled with other constructs, it has many other uses as well. &lt;/p&gt;

&lt;h3&gt;Using Range() to feed a more complex LINQ expression&lt;/h3&gt;

&lt;p&gt;For example, if we wanted a list of the first 5 even numbers, we could start with the number of expected items and multiply up by our step factor in a &lt;strong&gt;Select()&lt;/strong&gt;:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// take sequence 0, 1, 2, 3, 4 and multiply each by two...&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; var evens = Enumerable.Range(0, 5).Select(n =&amp;gt; n*2).ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Or, you could create a range over the total range of values and use &lt;strong&gt;Where()&lt;/strong&gt; to filter it down:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// generates sequence from 0..9, but only selects even ones&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; var odds = Enumerable.Range(0, 10).Where(n =&amp;gt; (n % 2) == 0).ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;But the great thing about &lt;strong&gt;Range()&lt;/strong&gt; is you don’t have to use it to just produce numbers, you can use the sequence it generates either directly or as the starting point for a more complex LINQ expression.  &lt;/p&gt;

&lt;p&gt;For example, if we wanted to generate a series of strings for font sizes we want to allow in a windows form, we could do that easily:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// takes the range from 1 to 10 and multiples by 10 and puts % on end.&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; var percentages = Enumerable&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     .Range(1, 10)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     .Select(i =&amp;gt; (i * 10) + &lt;span style="color: #006080"&gt;" pt"&lt;/span&gt;)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;     .ToArray();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This would give us an array of strings containing “10 pt”, “20 pt”, “30 pt”, … “100 pt”.&lt;/p&gt;

&lt;p&gt;So &lt;strong&gt;Range()&lt;/strong&gt; comes in handy for any expression you can create that starts with a simple sequence of integers.  But what else?&lt;/p&gt;

&lt;h3&gt;Using Range() to generate multiple complex objects&lt;/h3&gt;

&lt;p&gt;One other use of &lt;strong&gt;Range()&lt;/strong&gt; that can come in handy is for repeating some action multiple times.  For example, say we want to create and start several &lt;strong&gt;Task&lt;/strong&gt; instances for some parallel work.&lt;/p&gt;

&lt;p&gt;We could do this using a standard array allocation and &lt;strong&gt;for&lt;/strong&gt; loop:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// first construct the array of the appropriate size&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; Task[] tasks = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Task[NumConsumers];&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;// then loop through each index and create the new instance&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; NumConsumers; i++)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;     tasks[i] = TaskFactory.StartNew(SomeAction);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Or we could do it in one LINQ expression:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// constructs an array of NumConsumers tasks that will each &lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; &lt;span style="color: #008000"&gt;// perform SomeAction in parallel&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; Task[] tasks = Enumerable.Range(1, NumConsumers)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     .Select(i =&amp;gt; TaskFactory.StartNew(SomeAction))&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;     .ToArray();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Now, you may be wondering why we can’t do this instead:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// Enumerable.Repeat() repeats a value the specified number of times&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; Task[] tasks = Enumerable.Repeat(TaskFactory.StartNew(SomeAction), NumConsumers)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     .ToArray();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;The answer is that &lt;strong&gt;Enumerable.Repeat()&lt;/strong&gt; repeats the given value the specified number of times, so in the example above, it would call &lt;strong&gt;TaskFactory.StartNew()&lt;/strong&gt; once and then repeat the resulting reference &lt;strong&gt;NumConsumers&lt;/strong&gt; times, which is clearly not what we want!  There are ways to do similar things with &lt;strong&gt;Repeat()&lt;/strong&gt;, but they’re not quite as straightforward as using &lt;strong&gt;Range()&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Enumerable.Range()&lt;/strong&gt; method performs a very simple function, but it’s results can be used to drive much more complex LINQ expressions.  Feel free to use it for generating simple &lt;strong&gt;int&lt;/strong&gt; sequences all the way to generating sequences of a repeated action.  Either way, it’s a good tool to keep handy.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;table border="0"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/C%23" rel="tag"&gt;C#&lt;/a&gt;, &lt;a href="http://technorati.com/tags/CSharp" rel="tag"&gt;CSharp&lt;/a&gt;, &lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Little+Wonders" rel="tag"&gt;Little Wonders&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Enumerable" rel="tag"&gt;Enumerable&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Range" rel="tag"&gt;Range&lt;/a&gt;, &lt;a href="http://technorati.com/tags/LINQ" rel="tag"&gt;LINQ&lt;/a&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt; &lt;img src="http://blackrabbitcoder.net/aggbug/149471.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/J0X7cI56BzZQ-Q-YBcyIFTsisH4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/J0X7cI56BzZQ-Q-YBcyIFTsisH4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/J0X7cI56BzZQ-Q-YBcyIFTsisH4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/J0X7cI56BzZQ-Q-YBcyIFTsisH4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BlackRabbitCoder/~4/LwrwsXP1exo" height="1" width="1"/&gt;</description>
            <dc:creator>James Michael Hare</dc:creator>
            <guid isPermaLink="false">http://blackrabbitcoder.net/archive/2012/04/26/c.net-little-wonders-the-enumerable.range-static-method.aspx</guid>
            <pubDate>Fri, 27 Apr 2012 00:11:28 GMT</pubDate>
            <wfw:comment>http://blackrabbitcoder.net/comments/149471.aspx</wfw:comment>
            <comments>http://blackrabbitcoder.net/archive/2012/04/26/c.net-little-wonders-the-enumerable.range-static-method.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://blackrabbitcoder.net/comments/commentRss/149471.aspx</wfw:commentRss>
            <trackback:ping>http://blackrabbitcoder.net/services/trackbacks/149471.aspx</trackback:ping>
        <feedburner:origLink>http://blackrabbitcoder.net/archive/2012/04/26/c.net-little-wonders-the-enumerable.range-static-method.aspx</feedburner:origLink></item>
        <item>
            <title>No post this week - MVP metrics and Surgery</title>
            <category>My Blog</category>
            <category>Software</category>
            <link>http://feedproxy.google.com/~r/BlackRabbitCoder/~3/NxCFzVSM5Bk/no-post-this-week---mvp-metrics-and-surgery.aspx</link>
            <description>I won't be able to create a post this week due to an upcoming medical issue with a member of the family next week, and prepping a contributions spreadsheet since receipt of  an MVP award last year in the hope that I'll get my MVP renewed this year &amp;lt;crossesFingers/&amp;gt;.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Happy coding!&lt;/div&gt; &lt;img src="http://blackrabbitcoder.net/aggbug/149299.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/7ShAiVuzVDJM8aGM7R7pzw2qUxw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7ShAiVuzVDJM8aGM7R7pzw2qUxw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/7ShAiVuzVDJM8aGM7R7pzw2qUxw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7ShAiVuzVDJM8aGM7R7pzw2qUxw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BlackRabbitCoder/~4/NxCFzVSM5Bk" height="1" width="1"/&gt;</description>
            <dc:creator>James Michael Hare</dc:creator>
            <guid isPermaLink="false">http://blackrabbitcoder.net/archive/2012/04/12/no-post-this-week---mvp-metrics-and-surgery.aspx</guid>
            <pubDate>Thu, 12 Apr 2012 15:23:46 GMT</pubDate>
            <wfw:comment>http://blackrabbitcoder.net/comments/149299.aspx</wfw:comment>
            <comments>http://blackrabbitcoder.net/archive/2012/04/12/no-post-this-week---mvp-metrics-and-surgery.aspx#feedback</comments>
            <wfw:commentRss>http://blackrabbitcoder.net/comments/commentRss/149299.aspx</wfw:commentRss>
            <trackback:ping>http://blackrabbitcoder.net/services/trackbacks/149299.aspx</trackback:ping>
        <feedburner:origLink>http://blackrabbitcoder.net/archive/2012/04/12/no-post-this-week---mvp-metrics-and-surgery.aspx</feedburner:origLink></item>
        <item>
            <title>C#/.NET Little Wonders: Skip() and Take()</title>
            <link>http://feedproxy.google.com/~r/BlackRabbitCoder/~3/L4Y_akUyVa8/c.net-little-wonders-skip-and-take.aspx</link>
            <description>&lt;div style="margin: 5px; float: right"&gt;&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-0591047544853326";
/* Inline 2 */
google_ad_slot = "1318145269";
google_ad_width = 300;
google_ad_height = 250;
//--&gt;&lt;/script&gt;&lt;script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;&lt;/script&gt;&lt;/div&gt;  &lt;p&gt;&lt;em&gt;Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders posts can be found &lt;a href="http://blackrabbitcoder.net/archive/2011/10/24/c.net-little-wonders-the-complete-collection-again.aspx"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;I’ve covered many valuable methods from &lt;strong&gt;System.Linq &lt;/strong&gt;class library before, so you already know it’s packed with extension-method goodness.  Today I’d like to cover two small families I’ve neglected to mention before: &lt;strong&gt;Skip()&lt;/strong&gt; and &lt;strong&gt;Take()&lt;/strong&gt;.  While these methods seem so simple, they are an easy way to create sub-sequences for &lt;strong&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/strong&gt;, much the way &lt;strong&gt;GetRange()&lt;/strong&gt; creates sub-lists for &lt;strong&gt;List&amp;lt;T&amp;gt;&lt;/strong&gt;.&lt;/p&gt;  &lt;h3&gt;Skip() and SkipWhile()&lt;/h3&gt;  &lt;p&gt;The &lt;strong&gt;Skip()&lt;/strong&gt; family of methods is used to ignore items in a sequence until either a certain number are passed, or until a certain condition becomes &lt;strong&gt;false&lt;/strong&gt;.  This makes the methods great for starting a sequence at a point possibly other than the first item of the original sequence.   &lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;Skip()&lt;/strong&gt; family of methods contains the following methods (shown below in extension method syntax):&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Skip(int count)&lt;/strong&gt;       &lt;ul&gt;       &lt;li&gt;&lt;em&gt;Ignores the specified number of items and returns a sequence starting at the item after the last skipped item (if any).  &lt;/em&gt;&lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;SkipWhile(Func&amp;lt;T, bool&amp;gt; predicate)&lt;/strong&gt;       &lt;ul&gt;       &lt;li&gt;&lt;em&gt;Ignores items as long as the predicate returns &lt;strong&gt;true &lt;/strong&gt;and returns a sequence starting with the first item to invalidate the predicate (if any).  &lt;/em&gt;&lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;SkipWhile(Func&amp;lt;T, int, bool&amp;gt; predicate)&lt;/strong&gt;       &lt;ul&gt;       &lt;li&gt;&lt;em&gt;Same as above, but passes not only the item itself to the predicate, but also the index of the item.  &lt;/em&gt;&lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;For example:&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; var list = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt;[] { 3.14, 2.72, 42.0, 9.9, 13.0, 101.0 };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; &lt;span style="color: #008000"&gt;// sequence contains { 2.72, 42.0, 9.9, 13.0, 101.0 }&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; var afterSecond = list.Skip(1);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; Console.WriteLine(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Join(&lt;span style="color: #006080"&gt;", "&lt;/span&gt;, afterSecond));&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt; &lt;span style="color: #008000"&gt;// sequence contains { 42.0, 9.9, 13.0, 101.0 }&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt; var afterFirstDoubleDigit = list.SkipWhile(v =&amp;gt; v &amp;lt; 10.0);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt; Console.WriteLine(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Join(&lt;span style="color: #006080"&gt;", "&lt;/span&gt;, afterFirstDoubleDigit));&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Note that the &lt;strong&gt;SkipWhile()&lt;/strong&gt; stops skipping at the first item that returns &lt;strong&gt;false &lt;/strong&gt;and returns from there to the rest of the sequence, even if further items in that sequence also would satisfy the predicate (otherwise, you’d probably be using &lt;strong&gt;Where()&lt;/strong&gt; instead, of course).&lt;/p&gt;

&lt;p&gt;If you do use the form of &lt;strong&gt;SkipWhile()&lt;/strong&gt; which also passes an index into the predicate, then you should keep in mind that this is the index of the item in the sequence you are calling &lt;strong&gt;SkipWhile()&lt;/strong&gt; &lt;em&gt;from&lt;/em&gt;, not the index in the &lt;em&gt;original &lt;/em&gt;collection.  &lt;/p&gt;

&lt;p&gt;That is, consider the following:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; var list = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt;[] { 1.0, 1.1, 1.2, 2.2, 2.3, 2.4 };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; &lt;span style="color: #008000"&gt;// Get all items &amp;lt; 10, then &lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; var whatAmI = list&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;     .Skip(2)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;     .SkipWhile((i, x) =&amp;gt; i &amp;gt; x);&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;For this example the result above is &lt;strong&gt;2.4, &lt;/strong&gt;and not &lt;strong&gt;1.2, 2.2, 2.3, 2.4 &lt;/strong&gt;as some might expect.  The key is knowing what the index is that’s passed to the predicate in &lt;strong&gt;SkipWhile()&lt;/strong&gt;.  In the code above, because &lt;strong&gt;Skip(2)&lt;/strong&gt; skips 1.0 and 1.1, the sequence passed to &lt;strong&gt;SkipWhile()&lt;/strong&gt; begins at 1.2 and thus it considers the “index” of 1.2 to be &lt;strong&gt;0&lt;/strong&gt; and not &lt;strong&gt;2&lt;/strong&gt;.  This same logic applies when using any of the extension methods that have an overload that allows you to pass an index into the delegate, such as &lt;strong&gt;SkipWhile(), TakeWhile(), Select(), Where(), &lt;/strong&gt;etc.  &lt;/p&gt;

&lt;p&gt;It should also be noted, that it’s fine to &lt;strong&gt;Skip() &lt;/strong&gt;more items than exist in the sequence (an empty sequence is the result), or even to &lt;strong&gt;Skip(0)&lt;/strong&gt; which results in the full sequence.  So why would it ever be useful to return &lt;strong&gt;Skip(0) &lt;/strong&gt;deliberately?  One reason might be to return a &lt;strong&gt;List&amp;lt;T&amp;gt;&lt;/strong&gt; as an immutable sequence.  &lt;/p&gt;

&lt;p&gt;Consider this class:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MyClass&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; List&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; _myList = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;     &lt;span style="color: #008000"&gt;// works on surface, but one can cast back to List&amp;lt;int&amp;gt; and mutate the original...&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerable&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; OneWay&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;         get { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _myList; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;     &lt;span style="color: #008000"&gt;// works, but still has Add() etc which throw at runtime if accidentally called&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ReadOnlyCollection&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; AnotherWay&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;         get { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ReadOnlyCollection&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt;(_myList); }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum17"&gt;  17:&lt;/span&gt;     &lt;span style="color: #008000"&gt;// immutable, can't be cast back to List&amp;lt;int&amp;gt;, doesn't have methods that throw at runtime&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum18"&gt;  18:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerable&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; YetAnotherWay&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum19"&gt;  19:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum20"&gt;  20:&lt;/span&gt;         get { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _myList.Skip(0); }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum21"&gt;  21:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum22"&gt;  22:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This code snippet shows three (among many) ways to return an internal sequence in varying levels of immutability.  Obviously if you just try to return as &lt;strong&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/strong&gt; without doing anything more, there’s always the danger the caller could cast back to &lt;strong&gt;List&amp;lt;T&amp;gt;&lt;/strong&gt; and mutate your internal structure.  You could also return a &lt;strong&gt;ReadOnlyCollection&amp;lt;T&amp;gt;&lt;/strong&gt;, but this still &lt;em&gt;has&lt;/em&gt; the mutating methods, they just throw at &lt;em&gt;runtime&lt;/em&gt; when called instead of giving compiler errors.  Finally, you can return the internal list as a sequence using &lt;strong&gt;Skip(0) &lt;/strong&gt;which skips no items and just runs an iterator through the list.  The result is an iterator, which cannot be cast back to &lt;strong&gt;List&amp;lt;T&amp;gt;&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;Of course, there’s many ways to do this (including just cloning the list, etc.) but the point is it illustrates a potential use of using an explicit &lt;strong&gt;Skip(0).&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;Take() and TakeWhile() &lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Take()&lt;/strong&gt; and &lt;strong&gt;TakeWhile()&lt;/strong&gt; methods can be though of as somewhat of the inverse of &lt;strong&gt;Skip()&lt;/strong&gt; and &lt;strong&gt;SkipWhile()&lt;/strong&gt;.  That is, while &lt;strong&gt;Skip()&lt;/strong&gt; ignores the first &lt;strong&gt;X&lt;/strong&gt; items and returns the rest, &lt;strong&gt;Take()&lt;/strong&gt; returns a sequence of the first &lt;strong&gt;X&lt;/strong&gt; items and ignores the rest.  &lt;/p&gt;

&lt;p&gt;Since they are somewhat of an inverse of each other, it makes sense that their calling signatures are identical (beyond the method name obviously):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Take(int count)&lt;/strong&gt; 

    &lt;ul&gt;
      &lt;li&gt;&lt;em&gt;Returns a sequence containing up to the specified number of items. Anything after the count is ignored.&lt;/em&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;TakeWhile(Func&amp;lt;T, bool&amp;gt; predicate)&lt;/strong&gt; 

    &lt;ul&gt;
      &lt;li&gt;&lt;em&gt;Returns a sequence containing items as long as the predicate returns &lt;strong&gt;true&lt;/strong&gt;.  Anything from the point the predicate returns &lt;strong&gt;false&lt;/strong&gt; and beyond is ignored.&lt;/em&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;TakeWhile(Func&amp;lt;T, int, bool&amp;gt; predicate)&lt;/strong&gt; 

    &lt;ul&gt;
      &lt;li&gt;&lt;em&gt;Same as above, but passes not only the item itself to the predicate, but also the index of the item. &lt;/em&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, for example, we could do the following:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; var list = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt;[] { 1.0, 1.1, 1.2, 2.2, 2.3, 2.4 };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; &lt;span style="color: #008000"&gt;// sequence contains 1.0 and 1.1&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; var firstTwo = list.Take(2);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt; &lt;span style="color: #008000"&gt;// sequence contains 1.0, 1.1, 1.2&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt; var underTwo = list.TakeWhile(i =&amp;gt; i &amp;lt; 2.0);&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;The same considerations for &lt;strong&gt;SkipWhile()&lt;/strong&gt; with index apply to &lt;strong&gt;TakeWhile()&lt;/strong&gt; with index, of course.  &lt;/p&gt;

&lt;h3&gt;Using Skip() and Take() for sub-sequences&lt;/h3&gt;

&lt;p&gt;A few weeks back, I talked about &lt;a href="http://blackrabbitcoder.net/archive/2012/02/23/c.net-little-wondersndashthe-listlttgt-range-methods.aspx"&gt;The List&amp;lt;T&amp;gt; Range Methods&lt;/a&gt; and showed how they could be used to get a sub-list of a &lt;strong&gt;List&amp;lt;T&amp;gt;&lt;/strong&gt;.  This works well if you’re dealing with &lt;strong&gt;List&amp;lt;T&amp;gt;&lt;/strong&gt;, or don’t mind converting to &lt;strong&gt;List&amp;lt;T&amp;gt;.  &lt;/strong&gt;But if you have a simple &lt;strong&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/strong&gt; sequence and want to get a sub-sequence, you can also use &lt;strong&gt;Skip()&lt;/strong&gt; and &lt;strong&gt;Take()&lt;/strong&gt; to much the same effect:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; var list = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="color: #0000ff"&gt;double&lt;/span&gt;&amp;gt; { 1.0, 1.1, 1.2, 2.2, 2.3, 2.4 };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; &lt;span style="color: #008000"&gt;// results in List&amp;lt;T&amp;gt; containing { 1.2, 2.2, 2.3 }&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; var subList = list.GetRange(2, 3);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt; &lt;span style="color: #008000"&gt;// results in sequence containing { 1.2, 2.2, 2.3 }&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt; var subSequence = list.Skip(2).Take(3);&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;I say “much the same effect” because there are some differences.  First of all &lt;strong&gt;GetRange()&lt;/strong&gt; will throw if the starting index or the count are greater than the number of items in the list, but &lt;strong&gt;Skip()&lt;/strong&gt; and &lt;strong&gt;Take() &lt;/strong&gt;do not.  Also &lt;strong&gt;GetRange()&lt;/strong&gt; is a method off of &lt;strong&gt;List&amp;lt;T&amp;gt;&lt;/strong&gt;, thus it can use direct indexing to get to the items much more efficiently, whereas &lt;strong&gt;Skip()&lt;/strong&gt; and &lt;strong&gt;Take()&lt;/strong&gt; operate on sequences and may actually have to walk through the items they skip to create the resulting sequence.  &lt;/p&gt;

&lt;p&gt;So each has their pros and cons.  My general rule of thumb is if I’m already working with a &lt;strong&gt;List&amp;lt;T&amp;gt;&lt;/strong&gt; I’ll use &lt;strong&gt;GetRange()&lt;/strong&gt;, but for any plain &lt;strong&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/strong&gt; sequence I’ll tend to prefer &lt;strong&gt;Skip() &lt;/strong&gt;and &lt;strong&gt;Take() &lt;/strong&gt;instead.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Skip()&lt;/strong&gt; and &lt;strong&gt;Take()&lt;/strong&gt; families of LINQ extension methods are handy for producing sub-sequences from any &lt;strong&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/strong&gt; sequence.  &lt;strong&gt;Skip()&lt;/strong&gt; will ignore the specified number of items and return the rest of the sequence, whereas &lt;strong&gt;Take()&lt;/strong&gt; will return the specified number of items and ignore the rest of the sequence.  &lt;/p&gt;

&lt;p&gt;Similarly, the &lt;strong&gt;SkipWhile() &lt;/strong&gt;and &lt;strong&gt;TakeWhile()&lt;/strong&gt; methods can be used to skip or take items, respectively, until a given predicate returns &lt;strong&gt;false&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;table border="0"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/C%23" rel="tag"&gt;C#&lt;/a&gt;, &lt;a href="http://technorati.com/tags/CSharp" rel="tag"&gt;CSharp&lt;/a&gt;, &lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;, &lt;a href="http://technorati.com/tags/LINQ" rel="tag"&gt;LINQ&lt;/a&gt;, &lt;a href="http://technorati.com/tags/IEnumerable%3cT%3e" rel="tag"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Skip" rel="tag"&gt;Skip&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Take" rel="tag"&gt;Take&lt;/a&gt;, &lt;a href="http://technorati.com/tags/SkipWhile" rel="tag"&gt;SkipWhile&lt;/a&gt;, &lt;a href="http://technorati.com/tags/TakeWhile" rel="tag"&gt;TakeWhile&lt;/a&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt; &lt;img src="http://blackrabbitcoder.net/aggbug/149145.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/bUCV16f6pwDJ_s9DntEfTDP0qeA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/bUCV16f6pwDJ_s9DntEfTDP0qeA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/bUCV16f6pwDJ_s9DntEfTDP0qeA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/bUCV16f6pwDJ_s9DntEfTDP0qeA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BlackRabbitCoder/~4/L4Y_akUyVa8" height="1" width="1"/&gt;</description>
            <dc:creator>James Michael Hare</dc:creator>
            <guid isPermaLink="false">http://blackrabbitcoder.net/archive/2012/03/29/c.net-little-wonders-skip-and-take.aspx</guid>
            <pubDate>Thu, 29 Mar 2012 22:58:13 GMT</pubDate>
            <wfw:comment>http://blackrabbitcoder.net/comments/149145.aspx</wfw:comment>
            <comments>http://blackrabbitcoder.net/archive/2012/03/29/c.net-little-wonders-skip-and-take.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://blackrabbitcoder.net/comments/commentRss/149145.aspx</wfw:commentRss>
            <trackback:ping>http://blackrabbitcoder.net/services/trackbacks/149145.aspx</trackback:ping>
        <feedburner:origLink>http://blackrabbitcoder.net/archive/2012/03/29/c.net-little-wonders-skip-and-take.aspx</feedburner:origLink></item>
    </channel>
</rss>

