<?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>Justin DuJardin &#187; C#</title>
	<atom:link href="http://www.justindujardin.com/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.justindujardin.com/blog</link>
	<description>Thinker, Developer, Nerd.</description>
	<lastBuildDate>Thu, 03 Nov 2011 17:36:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Canvas Element Positioning Performance</title>
		<link>http://www.justindujardin.com/blog/2010/05/canvas-element-positioning-performance/</link>
		<comments>http://www.justindujardin.com/blog/2010/05/canvas-element-positioning-performance/#comments</comments>
		<pubDate>Thu, 13 May 2010 00:32:19 +0000</pubDate>
		<dc:creator>Justin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Canvas]]></category>
		<category><![CDATA[DependencyProperty]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Silverlight 3]]></category>

		<guid isPermaLink="false">http://www.justindujardin.com/blog/?p=1076</guid>
		<description><![CDATA[<a href="http://www.justindujardin.com/blog/2010/05/canvas-element-positioning-performance/" title="Canvas Element Positioning Performance"></a>C# and Silverlight provide a rich set of layout controls that can be used in constructing elegant user interfaces, including a do-it-yourself Canvas class that allows you to manually position elements on it&#8217;s surface. Positioning Elements in Code With a &#8230;<p class="read-more"><a href="http://www.justindujardin.com/blog/2010/05/canvas-element-positioning-performance/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.justindujardin.com/blog/2010/05/canvas-element-positioning-performance/" title="Canvas Element Positioning Performance"></a><p style="text-align: left;">C# and Silverlight provide a rich set of layout controls that can be used in constructing elegant user interfaces, including a do-it-yourself <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.aspx" target="_blank">Canvas</a> class that allows you to manually position elements on it&#8217;s surface.</p>
<p style="text-align: left;"><a href="http://www.justindujardin.com/blog/wp-content/uploads/2010/05/Silverlight_Canvas_Element_Positioning.jpg"><img class="size-medium wp-image-1078 alignright" title="Canvas Positioning    Performance" src="http://www.justindujardin.com/blog/wp-content/uploads/2010/05/Silverlight_Canvas_Element_Positioning-300x236.jpg" alt="" width="300" height="236" /></a><strong>Positioning Elements in Code<br />
</strong></p>
<p style="text-align: left;">With a Canvas you&#8217;re almost always going to want to have some code that positions the child elements.  To move things about, the property you are concerned with is not, as you might expect, a Position or Left/Top property on the child element, but rather it is a property of the Canvas that can be set on a child element.</p>
<p style="text-align: left;">There are two ways I have seen used to set the Canvas.Left or Canvas.Top property on a child element, and in some cases you&#8217;ll want to distinguish between the two.</p>
<p style="text-align: left;">The first way is to call SetValue on the Child element with the DependencyProperty  you want to set, Canvas.LeftProperty or Canvas.TopProperty.   The second way is to call Canvas.SetLeft or  Canvas.SetTop and pass in the UIElement to set it on.</p>
<p style="text-align: left;"><span id="more-1076"></span></p>
<p style="text-align: left;"><strong>When Speed Matters</strong></p>
<p style="text-align: left;">In some situations it can be helpful to understand the difference in performance of the various ways of setting these properties.</p>
<p style="text-align: left;">I could not find any information comparing the performance of these two methods so I wrote a small unit test (below) that could tell me which was faster.  It turns out that on average, calling the Canvas.SetLeft/SetTop methods are about 3 times faster than calling Element.SetValue with the DependencyProperty.</p>
<p style="text-align: left;">When you think about that for a moment, it begins to make sense, because the Element.SetValue method takes a DependencyProperty and a value, which means that it resolve the property to end up at it&#8217;s destination.  On the other hand the SetLeft/SetTop methods already know what data they&#8217;re setting (they even explicitly specify it in the naming!), which means they should have a pretty direct path to the data.</p>
<p style="text-align: left;"><strong>Testing Performance</strong></p>
<p>I&#8217;ve provided  below the source code that I used to generate the data  for this graph so you can try it for yourself.</p>
<table style="height: 108px;" border="0" cellspacing="0" cellpadding="3" width="412">
<tbody>
<tr style="text-align: center;">
<td></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">1,000,000</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">500,000</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">100,000</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">10,000</span></span></td>
</tr>
<tr>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">Canvas.SetLeft</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">446</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">220</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">46</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">5</span></span></td>
</tr>
<tr>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">Canvas.SetTop</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">456</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">222</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">46</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">5</span></span></td>
</tr>
<tr>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">Canvas.LeftProperty<br />
</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">1361</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">683</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">139</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">18</span></span></td>
</tr>
<tr>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">Canvas.TopProperty<br />
</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">1354</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">680</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">137</span></span></td>
<td><span style="font-family: arial,sans,sans-serif;"><span style="font-size: x-small;">14</span></span></td>
</tr>
</tbody>
</table>
<pre class="brush: csharp; title: ; notranslate">
public void TestCanvasElementPositioning()
{
   long elapsedSetValueTop = 0;
   long elapsedSetTop = 0;
   long elapsedSetValueLeft = 0;
   long elapsedSetLeft = 0;
   var timer = new Stopwatch();

   var testCanvas = new Canvas();
   var testChild = new Ellipse();
   testCanvas.Children.Add(testChild);

   // How many iterations to time
   const long iterations = 10000;

   // Canvas.SetLeft
   timer.Start();
   for (int i = 0; i &lt; iterations; i++)
      Canvas.SetLeft(testChild, (double)i % 10);
   timer.Stop();
   elapsedSetLeft = timer.ElapsedMilliseconds;
   timer.Reset();

   // Element.SetValue(Canvas.LeftProperty)
   timer.Start();
   for (int i = 0; i &lt; iterations; i++)
      testChild.SetValue(Canvas.LeftProperty, (double)i % 10);
   timer.Stop();
   elapsedSetValueLeft = timer.ElapsedMilliseconds;
   timer.Reset();

   testCanvas = new Canvas();
   testChild = new Ellipse();
   GC.Collect();

   // Element.SetValue(Canvas.TopProperty)
   timer.Start();
   for (int i = 0; i &lt; iterations; i++)
      testChild.SetValue(Canvas.TopProperty, (double)i % 10);
   timer.Stop();
   elapsedSetValueTop = timer.ElapsedMilliseconds;
   timer.Reset();

   // Canvas.SetTop
   timer.Start();
   for (int i = 0; i &lt; iterations; i++)
      Canvas.SetTop(testChild, (double)i % 10);
   timer.Stop();
   elapsedSetTop = timer.ElapsedMilliseconds;
   timer.Reset();

   Assert.IsTrue(elapsedSetLeft &lt; elapsedSetValueLeft,
                 &quot;Expected SetLeft to be less than SetValue&quot;);
   Assert.IsTrue(elapsedSetTop &lt; elapsedSetValueTop,
                 &quot;Expected SetTop to be less than SetValue&quot;);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.justindujardin.com/blog/2010/05/canvas-element-positioning-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom Metadata tags in C# code</title>
		<link>http://www.justindujardin.com/blog/2010/03/custom-metadata-tags-in-c-code/</link>
		<comments>http://www.justindujardin.com/blog/2010/03/custom-metadata-tags-in-c-code/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 19:36:59 +0000</pubDate>
		<dc:creator>Justin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Things I Like]]></category>
		<category><![CDATA[Attribute]]></category>
		<category><![CDATA[Metadata]]></category>

		<guid isPermaLink="false">http://www.justindujardin.com/blog/?p=983</guid>
		<description><![CDATA[<a href="http://www.justindujardin.com/blog/2010/03/custom-metadata-tags-in-c-code/" title="Custom Metadata tags in C# code"></a>Some dynamic languages like C# and ActionScript allow you to put metadata inline with your code.  Metadata can be a really cool way to make extra data available to you at runtime.  To make full use of it, you need &#8230;<p class="read-more"><a href="http://www.justindujardin.com/blog/2010/03/custom-metadata-tags-in-c-code/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.justindujardin.com/blog/2010/03/custom-metadata-tags-in-c-code/" title="Custom Metadata tags in C# code"></a><p>Some dynamic languages like C# and ActionScript allow you to put metadata inline with your code.  Metadata can be a really cool way to make extra data available to you at runtime.  To make full use of it, you need to know how to specify this metadata and then how to access it.</p>
<p><strong>MetadataSample.cs</strong></p>
<p>The sample below shows how to specify a custom Attribute called &#8220;SomeExtraData&#8221;, use it on a class &#8220;SomeClass&#8221; and retrieve the custom data in code for further use.<br />
<strong><br />
</strong></p>
<pre class="brush: csharp; title: ; wrap-lines: false; notranslate">
using System;
using System.Diagnostics;

namespace SilverShorts
{
   // This class makes the [SomeExtraData] tag below valid
   class SomeExtraData : Attribute
   {
      public string SomeField;
   }

   // Tag this class with a custom attribute
   [SomeExtraData (SomeField=&quot;SomeValue&quot;)]
   class SomeClass
   {
      public SomeClass()
      {
         // Retrieve the value of the SomeField item in the metadata
         // tag on this class.
         Type t = typeof(SomeClass);
         SomeExtraData attr = t.GetCustomAttributes(false)[0] as SomeExtraData;
         Debug.WriteLine(&quot;SomeExtraData.SomeField = &quot; + attr.SomeField);
      }
   }
}
</pre>
<p><strong>A word of caution</strong>: Be aware of how often you perform operations like querying custom attributes in the constructor above.  The system reflection calls can negatively impact performance if you abuse them.  I won&#8217;t preach about it, just use them only when you need to.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.justindujardin.com/blog/2010/03/custom-metadata-tags-in-c-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

