<?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; C#</title>
	<atom:link href="http://www.bolsterweb.com/category/programming/csharp/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>Little Red Riding Hood &#8211; In C#</title>
		<link>http://www.bolsterweb.com/2010/05/little-red-riding-hood-in-csharp/</link>
		<comments>http://www.bolsterweb.com/2010/05/little-red-riding-hood-in-csharp/#comments</comments>
		<pubDate>Sat, 29 May 2010 22:31:21 +0000</pubDate>
		<dc:creator>Jonathon Bolster</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Random Musings]]></category>
		<category><![CDATA[bedtime]]></category>
		<category><![CDATA[bored]]></category>
		<category><![CDATA[stories]]></category>

		<guid isPermaLink="false">http://www.bolsterweb.com/?p=136</guid>
		<description><![CDATA[Tonight, I was bored. Seriously, it was either this or watching the Eurovision song contest. Sadly, with BBC iPlayer, I was able to do both. I&#8217;ve decided that when I have kids, they&#8217;re going to know how to use computers and learn how to program (I can wish). So, I&#8217;ve rewritten the story of Little [...]]]></description>
			<content:encoded><![CDATA[<p>Tonight, I was bored. Seriously, it was either this or watching the Eurovision song contest. Sadly, with BBC iPlayer, I was able to do both.</p>
<p>I&#8217;ve decided that when I have kids, they&#8217;re going to know how to use computers and learn how to program (I can wish).</p>
<p>So, I&#8217;ve rewritten the story of Little Red Riding Hood. This time it&#8217;s in C#. I have no idea why I decided I would do this &#8211; by the time I&#8217;d realised, it was too late</p>
<p>So if you want to tell your kids a bedtime story whilst edging them into becoming programmers, read on.</p>
<p><span id="more-136"></span></p>
<pre class="brush: csharp; title: ; notranslate">
// The story of Little Red Riding Hood - as told in C#
var story = new Story()
{
	Name = &quot;Little Red Riding Hood&quot;,
	Description = &quot;The story of a girl who meets a wicked wolf on her way to her grandmother's house&quot;
};

// Once upon a time, there was one little girl who was loved by everyone. Her
// grandmother especially loved her and gave her a Red Riding Hood, which suited
// her so well that she would never wear anything else. So she was called 'Little
// Red Riding Hood'
var littleRedRidingHood = population.Where(x =&gt;
								x.Sex == Sex.Female &amp;&amp;
								x.Characteristics.HasFlag(Characteristics.Lovely) &amp;&amp;
								x.AgeGroup == AgeGroup.Child &amp;&amp;
								x.Clothes.Any(y =&gt;
									y.Name == &quot;Riding Hood&quot; &amp;&amp;
									y.Colour == Color.Red
									)
								)
								.OrderBy(x =&gt; x.Popularity)
								.First();

// One day, her mother gave her some cake and wine to take to her grandmother as
// she was ill. Little Red Riding Hood put them into her basket, and set off for
// grandmother's house.
var foodForGrandmother = cupboard.Where(x =&gt; x is Cake || x is Wine).ToList();

foreach (var foodItem in foodForGrandmother)
{
	cupboard.Remove(foodItem);
}

littleRedRidingHood.Basket.AddRange(foodForGrandmother);

// Her grandmother lived out in the wood and, just as Little Red Riding Hood entered
// the wood, she met a wolf. She was not afraid of him, since she did not know how
// wicked he was.
wood.Occupants.Add(littleRedRidingHood);

var wolf = new Character()
{
	Clothes = Enumerable.Empty&lt;Clothes&gt;(),
	CurrentLocation = wood,
	Home = wood,
	Popularity = 0,
	Characteristics = Characteristics.Wicked,
	Sex = Sex.Male
};

littleRedRidingHood.CurrentLocation = wolf.CurrentLocation;
littleRedRidingHood.Mood ^= Mood.Fear;

// The wolf asked where Little Red Riding Hood was going. She explained that she
// was bringing her grandmother some food, as she was poorly. The wolf asked
// where her grandmother lived.
wolf.Knowledge.Add(grandmother.Home);

// The wolf thought Little Red Riding Hood looked tasty and would be much better
// to eat than the old woman. He knew he must be crafty, to catch both.
wolf.Appetite.Add(grandmother);
wolf.Appetite.Add(littleRedRidingHood);
wolf.Mood |= Mood.Hungry;

// The wolf convinced Little Red Riding Hood to stray from the path and pick flowers
// for her grandmother. Then he ran off to her grandmother's house whilst Little Red
// Riding Hood picked flowers; until she could carry no more.
wolf.CurrentLocation = grandmother.Home;

littleRedRidingHood.Basket.AddRange(
	wood.Items.Where(x =&gt; x is Flower).Take(littleRedRidingHood.Basket.MaxSize - littleRedRidingHood.Basket.Count)
	);

// The wolf reached grandmother's house and knocked on the door. 'Who is it?', she asked.
// 'Little Red Riding Hood', replied the wolf. 'She is bringing wine and cake'.
// 'Lift the latch', grandmother said, 'I am too weak to open it'. The wolf opened the
// door and, without saying a word, immediately jumped in and gobbled up grandmother.
wolf.Belly.Add(grandmother);
wood.Occupants.Remove(grandmother);

// The wolf heard Little Red Riding Hood come close, and quickly put on the grandmother's
// clothes and jump into her bed.
wolf.Clothes = grandmother.Clothes;

// Little Red Riding Hood reached her grandmother's house and, seeing the door open, felt
// very uneasy - but entered anyway.
littleRedRidingHood.CurrentLocation = grandmother.Home;
littleRedRidingHood.Mood |= Mood.Nervous;

// 'Oh grandma, what big ears you have', said Little Red Riding Hood
// 'All the better to hear you with, my child', replied the wolf
if (!wolf.Features.Ears.Equals(grandmother.Features.Ears))
{
	littleRedRidingHood.Mood |= Mood.Nervous;
}

// 'But, grandmother, what big eyes you have', she said.
// 'All the better to see you with, my dear'
if (!wolf.Features.Eyes.Equals(grandmother.Features.Eyes))
{
	littleRedRidingHood.Mood |= Mood.Nervous;
}

// 'But, grandmother, what large hands you have!'
// 'All the better to hug you with'
if (!wolf.Features.Hands.Equals(grandmother.Features.Hands))
{
	littleRedRidingHood.Mood |= Mood.Nervous;
}

// 'Oh! grandma, what a terribly big mouth you have'
// 'All the better to eat you with!'
if (!wolf.Features.Mouth.Equals(grandmother.Features.Mouth))
{
	littleRedRidingHood.Mood |= Mood.Fear;
}

// And with that, the wolf jumped out of bed and gobbled up Little
// Red Riding Hood!
wolf.Belly.Add(littleRedRidingHood);
wolf.Appetite.Clear();

// After appeasing his appetite, the wolf lay on the bed, fell
// asleep and began snoring very loudly.
wolf.Mood = Mood.Tired;

// Just then, a huntsman was passing and thought to himself 'How
// the old woman is snoring! I must see if she wants anything.'
var huntsman = new Character()
{
	Characteristics = Characteristics.Strong,
	AgeGroup = AgeGroup.Adult,
	Sex = Sex.Male,
};

huntsman.CurrentLocation = grandmother.Home;

// The huntsman entered the house and saw the wolf sleeping.
// 'You!', exclaimed the huntsman, 'How long I have looked for you
// wicked thing' and raised his gun to shoot the wolf
huntsman.Mood = Mood.Anger;

// The huntsman realised the wolf may have eaten the old woman, and
// put his gun down. Instead, he used some scissors to open the wolf's
// belly. Out sprang Little Red Riding Hood. 'How frightened I have
// been', she told him.
wolf.Belly.Remove(littleRedRidingHood);
littleRedRidingHood.Mood ^= Mood.Fear;
littleRedRidingHood.Mood |= Mood.Happy;

// Out followed her grandmother, but scarcely able to breath.
wolf.Belly.Remove(grandmother);
grandmother.Mood = Mood.Happy | Mood.Tired | Mood.Hungry;

// Little Red Riding Hood ran out into the wood and picked up stones
// to fill the wolf's belly with
var stones = wood.Items.Where(x =&gt; x is Stone).Take(wolf.Belly.MaxSize);
wolf.Belly.AddRange(stones);

// When the wolf awoke, he tried to run away but the stones were so heavy
// that he fell at once, and fell dead
wolf.Mood ^= Mood.Tired;
wolf.Mood |= Mood.Shocked;
wood.Occupants.Remove(wolf);
wolf = null;

// The wolf had died, and the three were happy. The grandmother ate the cake
// and drank the wine, and was revived.
huntsman.Mood = Mood.Happy;
littleRedRidingHood.Mood = Mood.Happy;

grandmother.Belly.AddRange(littleRedRidingHood.Basket.Where(x=&gt; x is Cake || x is Wine));
grandmother.Mood = Mood.Happy;

// The huntsman took the skin off the wolf and took it home, and Little Red
// Riding Hood learnt that she should never stray off the path, especially
// when wolves tempt her.
//Assert.IsTrue(wolf.Characteristics.HasFlag(Characteristics.Wicked));

// The End
story.Ending = Ending.Happy;
</pre>
<p>This actually compiles and runs to a happy ending (with the source of the other classes, of course). If you&#8217;re going to read it as a bed time story to your children, it may as well compile.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bolsterweb.com/2010/05/little-red-riding-hood-in-csharp/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>

