<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jonathon Bolster &#187; dotnet</title>
	<atom:link href="http://www.bolsterweb.com/category/dotnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bolsterweb.com</link>
	<description>Programming, drawing, random, life.</description>
	<lastBuildDate>Mon, 03 Oct 2011 17:30:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>IEnumerable extension to create a delimited string from a string list</title>
		<link>http://www.bolsterweb.com/2011/08/ienumerable-extension-to-create-a-delimited-string-from-a-string-list/</link>
		<comments>http://www.bolsterweb.com/2011/08/ienumerable-extension-to-create-a-delimited-string-from-a-string-list/#comments</comments>
		<pubDate>Sat, 20 Aug 2011 09:33:56 +0000</pubDate>
		<dc:creator>Jonathon Bolster</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[ienumerable]]></category>

		<guid isPermaLink="false">http://www.bolsterweb.com/?p=255</guid>
		<description><![CDATA[With string lists, one thing that I find myself doing a lot is combining the list by some delimiter. In C#, this is a fairly easy thing to do but (today especially) I&#8217;ve seen a lot of places where people suggest using a string builder and appending a formatted string (then getting a substring to [...]]]></description>
			<content:encoded><![CDATA[<p>With string lists, one thing that I find myself doing a lot is combining the list by some delimiter. In C#, this is a fairly easy thing to do but (today especially) I&#8217;ve seen a lot of places where people suggest using a string builder and appending a formatted string (then getting a substring to remove the last delimiter) or something similar. I&#8217;m not completely against this, I just think this method is neater:</p>
<pre class="brush: csharp; title: ; notranslate">
        public static string ToString&lt;T&gt;(this IEnumerable&lt;T&gt; source, string delimiter)
        {
            return string.Join(delimiter, source);
        }
</pre>
<p>That will throw up an exception if &#8216;source&#8217; is null. That&#8217;s fine for me, since I don&#8217;t want that extension from hiding something that might be wrong elsewhere (i.e. why am I trying to join a null list). If you want to make this work fine and dandy for a null source, you can just use the <a title="Null Coalescing Operator" href="http://msdn.microsoft.com/en-US/library/ms173224(v=VS.80).aspx" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-US/library/ms173224_v=VS.80_.aspx?referer=');">null coalescing operator</a> and provide an empty source:</p>
<pre class="brush: csharp; title: ; notranslate">
        public static string ToString&lt;T&gt;(this IEnumerable&lt;T&gt; source, string delimiter)
        {
            return string.Join(delimiter, source ?? Enumerable.Empty&lt;T&gt;());
        }
</pre>
<p>Now why do I think this is a better solution that the string builder option? Well, if you whack open <a title="Redgate's Reflector" href="http://reflector.red-gate.com" onclick="pageTracker._trackPageview('/outgoing/reflector.red-gate.com?referer=');">Reflector</a>, you&#8217;ll notice that the <a title="String.Join on MSDN" href="http://msdn.microsoft.com/en-us/library/57a79xd0.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/57a79xd0.aspx?referer=');">Join</a> method uses a StringBuilder object and builds up from there. So since it&#8217;s already built straight into the framework, why reinvent the wheel? Of course, the Join method with an IEnumerable as an argument is only available on .NET 4.0. Anything prior to .NET 4.0 and you&#8217;ll have to pass in a string array (at which point the string builder method would be useful).</p>
<p>Another point to notice from the documentation is that the Join method will set the separator to String.Empty, if you pass it a null. The same applies to any element in the list that is null &#8211; it is simply replaced by another empty string.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bolsterweb.com/2011/08/ienumerable-extension-to-create-a-delimited-string-from-a-string-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Indexed IEnumerable extension</title>
		<link>http://www.bolsterweb.com/2010/10/indexed-ienumerable-extension/</link>
		<comments>http://www.bolsterweb.com/2010/10/indexed-ienumerable-extension/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 17:40:43 +0000</pubDate>
		<dc:creator>Jonathon Bolster</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[extension]]></category>

		<guid isPermaLink="false">http://www.bolsterweb.com/?p=173</guid>
		<description><![CDATA[The original intention for this was to clean up some code which was calling a Select to get an Index value so that basic zebra striping of rows could be done. It return an IndexedType generic class of type T which has properties for the original object and the index value of it. And the [...]]]></description>
			<content:encoded><![CDATA[<p>The original intention for this was to clean up some code which was calling a <code>Select</code> to get an Index value so that basic zebra striping of rows could be done. It return an <code>IndexedType</code> generic class of type <code>T</code> which has properties for the original object and the index value of it.</p>
<pre class="brush: csharp; title: ; notranslate">
public static IEnumerable&lt;IndexedType&lt;T&gt;&gt; Index&lt;T&gt;(this IEnumerable&lt;T&gt; source)
{
	return source.Select((x, i) =&gt; new IndexedType&lt;T&gt;() { Item = x, Index = i });
}
</pre>
<p>And the class returned:</p>
<pre class="brush: csharp; title: ; notranslate">
public class IndexedType&lt;T&gt;
{
	public T Item { get; set; }
	public int Index { get; set; }
}
</pre>
<p>This could be extended to include values like if the index value is even, N-Indexed values, etc but I leave that up to the imagination of the programmer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bolsterweb.com/2010/10/indexed-ienumerable-extension/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Getting a list of numbers between two values</title>
		<link>http://www.bolsterweb.com/2010/05/getting-a-list-of-numbers-between-two-values/</link>
		<comments>http://www.bolsterweb.com/2010/05/getting-a-list-of-numbers-between-two-values/#comments</comments>
		<pubDate>Sat, 15 May 2010 13:19:50 +0000</pubDate>
		<dc:creator>Jonathon Bolster</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[extension]]></category>

		<guid isPermaLink="false">http://www.bolsterweb.com/?p=132</guid>
		<description><![CDATA[This is yet another extension. I don&#8217;t like Enumerable.Range() for the sole reason you have to give a start index and a count. So I made up my own. This is an extension for an integer type and will allow you to specify two values that you want a range for This will also allow [...]]]></description>
			<content:encoded><![CDATA[<p>This is yet another extension.</p>
<p>I don&#8217;t like Enumerable.Range() for the sole reason you have to give a start index and a count. So I made up my own. This is an extension for an integer type and will allow you to specify two values that you want a range for</p>
<pre class="brush: csharp; title: ; notranslate">
public static IEnumerable&lt;int&gt; RangeTo(this int startValue, int endValue, int step)
{
	return (endValue &lt; startValue) ?
		Enumerable.Range(endValue, startValue - endValue + 1).Reverse() :
		Enumerable.Range(startValue, endValue - startValue + 1);
}
</pre>
<p>This will also allow for reverse lists &#8211; see how to use it below:</p>
<pre class="brush: csharp; title: ; notranslate">
1.RangeTo(10);  // {1,2,3,4,5,6,7,8,9,10}
5.RangeTo(10);  // {5,6,7,8,9,10}
10.RangeTo(5);  // {10,9,8,7,6,5}
3.RangeTo(3);   // {3}

int x = 15;
int y = 17;
x.RangeTo(10);  // {15,14,13,12,11,10}
y.RangeTo(x);  // {17,16,15}
x.RangeTo(y);  // {15,16,17}
13.RangeTo(x); // {13,14,15}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bolsterweb.com/2010/05/getting-a-list-of-numbers-between-two-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random sort on IEnumerable object</title>
		<link>http://www.bolsterweb.com/2010/05/random-sort-on-ienumerable-object/</link>
		<comments>http://www.bolsterweb.com/2010/05/random-sort-on-ienumerable-object/#comments</comments>
		<pubDate>Sat, 15 May 2010 12:25:14 +0000</pubDate>
		<dc:creator>Jonathon Bolster</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://www.bolsterweb.com/?p=93</guid>
		<description><![CDATA[Quick extension on an IEnumerable object to return the collection in a random order: For example, to return a random set of 5 items from the collection: If anyone knows of a better way, let me know]]></description>
			<content:encoded><![CDATA[<p>Quick extension on an IEnumerable object to return the collection in a random order:</p>
<pre class="brush: csharp; title: ; notranslate">
public static IEnumerable&lt;T&gt; Random&lt;T&gt;(this IEnumerable&lt;T&gt; source)
{
	return source.OrderBy(x =&gt; Guid.NewGuid());
}
</pre>
<p>For example, to return a random set of 5 items from the collection:</p>
<pre class="brush: csharp; title: ; notranslate">
var randomSet = myEnumerable.Random().Take(5);
</pre>
<p>If anyone knows of a better way, let me know <img src='http://www.bolsterweb.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.bolsterweb.com/2010/05/random-sort-on-ienumerable-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

