<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Adventures of an aspiring agile developer in a not-quite agile world : C#</title><link>http://blogs.msdn.com/agilemonkey/archive/tags/C_2300_/default.aspx</link><description>Tags: C#</description><dc:language>en-CA</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Extension methods are gonna be fun!</title><link>http://blogs.msdn.com/agilemonkey/archive/2007/05/23/extension-methods-are-gonna-be-fun.aspx</link><pubDate>Wed, 23 May 2007 13:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2813964</guid><dc:creator>casper</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/agilemonkey/comments/2813964.aspx</comments><wfw:commentRss>http://blogs.msdn.com/agilemonkey/commentrss.aspx?PostID=2813964</wfw:commentRss><wfw:comment>http://blogs.msdn.com/agilemonkey/rsscomments.aspx?PostID=2813964</wfw:comment><description>
&lt;p&gt;So I've had a (very) small amount of time to play around with Orcas and Silverlight, but some of the C# 3.0 features are going to come in handy. Greg Schechter has already written about using &lt;a href="http://blogs.msdn.com/greg_schechter/archive/2007/05/19/wpf-silverlight-and-c-3-0-object-initializers.aspx" mce_href="http://blogs.msdn.com/greg_schechter/archive/2007/05/19/wpf-silverlight-and-c-3-0-object-initializers.aspx"&gt;object initializers with XAML objects&lt;/a&gt;&amp;nbsp;and back in March Scott Guthrie showed an &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx" mce_href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx"&gt;example of extension methods&lt;/a&gt;. I've already come up with a nice little use for the latter.&lt;/p&gt;
 
&lt;p&gt;Because of its declarative nature, there are some properties you can set quite easily inside XAML, but not so easily in C# — specifying the Canvas position for an object is one of those.&lt;/p&gt;
&lt;code&gt;&amp;lt;Rectangle Canvas.Left="100" Canvas.Top="50" Width="10" Height="10/&amp;gt;&lt;/code&gt; 
&lt;pre&gt;Rectangle r = new Rectangle();&lt;br&gt;r.SetProperty&amp;lt;double&amp;gt;&lt;double&gt;(Canvas.Left, 100);&lt;br&gt;r.SetProperty&lt;/double&gt;&amp;lt;double&amp;gt;&lt;double&gt;&lt;double&gt;(Canvas.Top, 50);&lt;br&gt;&lt;/double&gt;&lt;/double&gt;&lt;/pre&gt;
Here's a nice little way object extensions can simplify your life.
&lt;pre&gt;public static class SilverlightExtensions&lt;br&gt;{&lt;br&gt;   public static void SetCanvasPosition(this Visual v, double left, double top)&lt;br&gt;   {&lt;br&gt;      v.SetValue&lt;double&gt;&amp;lt;double&amp;gt;(Canvas.LeftProperty, left);&lt;br&gt;      v.SetValue&lt;/double&gt;&amp;lt;double&amp;gt;&lt;double&gt;&lt;double&gt;(Canvas.TopProperty, top);&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;&lt;/double&gt;&lt;/double&gt;&lt;/pre&gt;
&lt;p&gt;
Because I need to be able to create a ton of Rectangles programmatically, I can replace the two earlier lines of code with one&lt;/p&gt;
&lt;p&gt;&lt;code&gt;r.SetCanvasPosition(100, 50);&lt;/code&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2813964" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/agilemonkey/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/agilemonkey/archive/tags/XAML/default.aspx">XAML</category><category domain="http://blogs.msdn.com/agilemonkey/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>Happy New Year! Now back to work :)</title><link>http://blogs.msdn.com/agilemonkey/archive/2007/01/04/happy-new-year-now-back-to-work.aspx</link><pubDate>Thu, 04 Jan 2007 11:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1410092</guid><dc:creator>casper</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/agilemonkey/comments/1410092.aspx</comments><wfw:commentRss>http://blogs.msdn.com/agilemonkey/commentrss.aspx?PostID=1410092</wfw:commentRss><wfw:comment>http://blogs.msdn.com/agilemonkey/rsscomments.aspx?PostID=1410092</wfw:comment><description>&lt;p&gt;First off, let me wish everyone a Happy New Year and the best of luck with those millions of New Year's resolutions which seem to be flying around the place. I'm sure the cloud tag over at &lt;a href="http://43things.com" mce_href="http://43things.com"&gt;43things&lt;/a&gt; is dominated by subjects related to weight loss/fitness/all that good stuff.&amp;nbsp;My own&amp;nbsp;commitment for work&amp;nbsp;is to continue searching for better ways to do things: whether it's coding, planning, or our daily game of foosball. I'd &lt;i&gt;like&lt;/i&gt; to make a commitment to blog twice a week, but unless I stumble across a fountain of ideas, that seems unlikely.&lt;/p&gt; &lt;p&gt;I'm currently working on a class that needs to store unique information, so naturally I've got a Dictionary tucked away with a couple of accessor methods. While writing my tests, I decided that returning 0 for non-existent values rather than throwing would be more appropriate. So, I punched in the following test and then made it pass.&lt;/p&gt;  &lt;pre&gt;[Test]&lt;br&gt;public void RetrievingANonExistentStatisticReturnsZero()&lt;br&gt;{&lt;br&gt;  Campaign c = new Campaign();&lt;br&gt;&lt;br&gt;  double value = c.GetStatistic("non-existent");&lt;br&gt;&lt;br&gt;  Assert.AreEqual(0, value);&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;

The first implementation I wrote for &lt;code&gt;GetStatistic()&lt;/code&gt; used &lt;code&gt;Dictionary.ContainsKey()&lt;/code&gt; and then I immediately wondered if &lt;code&gt;TryGetValue()&lt;/code&gt; would be any faster. After all, the MSDN docs say they both approach O(1).
&lt;p&gt;Remembering my &lt;a href="http://blogs.msdn.com/agilemonkey/archive/2006/11/20/my-adventures-with-enums.aspx" mce_href="http://blogs.msdn.com/agilemonkey/archive/2006/11/20/my-adventures-with-enums.aspx"&gt;experience with enums&lt;/a&gt;, I whipped up another small benchmark. This time, though, there was only a minute difference, so I'm sticking with the &lt;code&gt;ContainsKey&lt;/code&gt; version. It's tough being so curious at times ;)&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1410092" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/agilemonkey/archive/tags/C_2300_/default.aspx">C#</category></item></channel></rss>