<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.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>Jon Skeet: Coding Blog : CSharpDevCenter</title><link>http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx</link><description>Tags: CSharpDevCenter</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Migrating from Visual Studio 2010 beta 1 to beta 2 – solution file change required</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/10/26/migrating-from-visual-studio-2010-beta-1-to-beta-2-solution-file-change-required.aspx</link><pubDate>Mon, 26 Oct 2009 10:46:36 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1735303</guid><dc:creator>skeet</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1735303</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1735303</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/10/26/migrating-from-visual-studio-2010-beta-1-to-beta-2-solution-file-change-required.aspx#comments</comments><description>&lt;p&gt;Having installed &lt;a href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx"&gt;Visual Studio 2010 beta 2&lt;/a&gt; on my freshly-reinstalled netbook (now with Windows 7 and and SSD – yummy) I found that &lt;a href="http://superuser.com/questions/60742"&gt;my solution file from Visual Studio 2010 beta 1 wasn’t recognised properly&lt;/a&gt;: double-clicking on the file didn’t do anything. Opening the solution file manually was absolutely fine, but slightly less convenient than being able to double-click.&lt;/p&gt;  &lt;p&gt;After a bit of investigation, I’ve found the solution. Manually edit the solution file, and change the first few lines from this:&lt;/p&gt;  &lt;pre&gt;Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 10&lt;/pre&gt;

&lt;p&gt;to this:&lt;/p&gt;

&lt;pre&gt;Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010&lt;/pre&gt;

&lt;p&gt;It&amp;#39;s just a case of changing &amp;quot;10&amp;quot; to &amp;quot;2010&amp;quot;.&lt;/p&gt;

&lt;p&gt;Hopefully between this and the linked SuperUser post, this should avoid others feeling the same level of bafflement :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1735303" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>For vs Foreach on arrays and lists</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/29/for-vs-foreach-on-arrays-and-lists.aspx</link><pubDate>Thu, 29 Jan 2009 23:08:02 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1667108</guid><dc:creator>skeet</dc:creator><slash:comments>23</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1667108</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1667108</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/01/29/for-vs-foreach-on-arrays-and-lists.aspx#comments</comments><description>&lt;p&gt;As promised &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2009/01/26/benchmarking-made-easy.aspx"&gt;earlier in the week&lt;/a&gt;, here are the results of benchmarking &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;foreach&lt;/code&gt;.&lt;/p&gt; &lt;p&gt;For each of &lt;code&gt;int&lt;/code&gt; and &lt;code&gt;double&lt;/code&gt;, I created an array and a &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt;, filled it with random data (the same for the list as the array) and ran each of the following ways of summing the collection: &lt;/p&gt; &lt;ul&gt; &lt;li&gt;A simple for loop, testing the index against the array length / list count each time  &lt;li&gt;A for loop with an initial variable remembering the length of the array, then comparing the index against the variable each time  &lt;li&gt;A foreach loop against the collection with the type known at compile and JIT time  &lt;li&gt;A foreach loop against the collection as &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt;  &lt;li&gt;Enumerable.Sum&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I won&amp;#39;t show the complete code in this post, but it&amp;#39;s you can &lt;a href="http://pobox.com/~skeet/csharp/blogfiles/LoopsBenchmark.cs"&gt;download it&lt;/a&gt; and then build it against the &lt;a href="http://pobox.com/~skeet/csharp/blogfiles/BenchmarkHelper.zip"&gt;benchmarking framework&lt;/a&gt;. Here&amp;#39;s a taste of what it looks like - the code for a list instead of an array, and &lt;code&gt;double&lt;/code&gt; instead of &lt;code&gt;int&lt;/code&gt; is pretty similar:&lt;/p&gt; &lt;div class="code"&gt;List&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; intList = Enumerable.Range(0, Size)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Select(x =&amp;gt; rng.Next(100))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ToList();&lt;br /&gt;&lt;span class="ValueType"&gt;int&lt;/span&gt;[] intArray = intList.ToArray();&lt;br /&gt;&lt;br /&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; intArraySuite = TestSuite.Create(&lt;span class="String"&gt;&amp;quot;int[]&amp;quot;&lt;/span&gt;, intArray, intArray.Sum())&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(input =&amp;gt; { &lt;span class="ValueType"&gt;int&lt;/span&gt; sum = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;for&lt;/span&gt; (&lt;span class="ValueType"&gt;int&lt;/span&gt; i = 0; i &amp;lt; input.Length; i++) sum += input;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; sum;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }, &lt;span class="String"&gt;&amp;quot;For&amp;quot;&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(input =&amp;gt; { &lt;span class="ValueType"&gt;int&lt;/span&gt; sum = 0; &lt;span class="ValueType"&gt;int&lt;/span&gt; length = input.Length;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;for&lt;/span&gt; (&lt;span class="ValueType"&gt;int&lt;/span&gt; i = 0; i &amp;lt; length; i++) sum += input;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; sum;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }, &lt;span class="String"&gt;&amp;quot;ForHoistLength&amp;quot;&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(input =&amp;gt; { &lt;span class="ValueType"&gt;int&lt;/span&gt; sum = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;foreach&lt;/span&gt; (&lt;span class="ValueType"&gt;int&lt;/span&gt; d &lt;span class="Statement"&gt;in&lt;/span&gt; input) sum += d;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; sum;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }, &lt;span class="String"&gt;&amp;quot;ForEach&amp;quot;&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(IEnumerableForEach)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(Enumerable.Sum, &lt;span class="String"&gt;&amp;quot;Enumerable.Sum&amp;quot;&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .RunTests();&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;int&lt;/span&gt; IEnumerableForEach(IEnumerable&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; input)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ValueType"&gt;int&lt;/span&gt; sum = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;foreach&lt;/span&gt; (&lt;span class="ValueType"&gt;int&lt;/span&gt; d &lt;span class="Statement"&gt;in&lt;/span&gt; input)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum += d;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; sum;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;(I don&amp;#39;t normally format code quite like that, and wouldn&amp;#39;t even use a lambda for that sort of code - but it shows everything quite compactly for the sake of blogging.)&lt;/p&gt; &lt;p&gt;Before I present the results, a little explanation:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;I considered &lt;code&gt;int&lt;/code&gt; and &lt;code&gt;double&lt;/code&gt; entirely separately - so I&amp;#39;m &lt;em&gt;not&lt;/em&gt; comparing the &lt;code&gt;int[]&lt;/code&gt; results against the &lt;code&gt;double[]&lt;/code&gt; results for example.  &lt;li&gt;I considered array and list results together - so I &lt;em&gt;am&lt;/em&gt; comparing iterating over an &lt;code&gt;int[]&lt;/code&gt; with iterating over a &lt;code&gt;List&amp;lt;int&amp;gt;&lt;/code&gt;.  &lt;li&gt;The result for each test is a normalized score, where 1.0 means &amp;quot;the best of the &lt;code&gt;int&lt;/code&gt; summers&amp;quot; or &amp;quot;the best of the &lt;code&gt;double&lt;/code&gt; summers&amp;quot; and other scores for that type of summation show how much slower that test ran (i.e. a score of 2.0 would mean it was twice as slow - it got through half as many iterations).  &lt;li&gt;I&amp;#39;m not currently writing out the number of iterations each one completes - that might be interesting to see how much faster it is to sum ints than doubles. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Happy with that? Here are the results...&lt;/p&gt; &lt;div class="code"&gt;-------------------- Doubles --------------------&lt;br /&gt;============ double[] ============&lt;br /&gt;For&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1.00&lt;br /&gt;ForHoistLength&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1.00&lt;br /&gt;ForEach&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1.00&lt;br /&gt;IEnumerableForEach 11.47&lt;br /&gt;Enumerable.Sum&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 11.57&lt;br /&gt;&lt;br /&gt;============ List&amp;lt;double&amp;gt; ============&lt;br /&gt;For&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1.99&lt;br /&gt;ForHoistLength&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1.44&lt;br /&gt;ForEach&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3.19&lt;br /&gt;IEnumerableForEach 18.78&lt;br /&gt;Enumerable.Sum&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 18.61&lt;br /&gt;&lt;br /&gt;-------------------- Ints --------------------&lt;br /&gt;============ int[] ============&lt;br /&gt;For&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1.00&lt;br /&gt;ForHoistLength&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2.03&lt;br /&gt;ForEach&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1.36&lt;br /&gt;IEnumerableForEach 15.22&lt;br /&gt;Enumerable.Sum&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 15.73&lt;br /&gt;&lt;br /&gt;============ List&amp;lt;int&amp;gt; ============&lt;br /&gt;For&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2.82&lt;br /&gt;ForHoistLength&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3.49&lt;br /&gt;ForEach&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4.78&lt;br /&gt;IEnumerableForEach 25.71&lt;br /&gt;Enumerable.Sum&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 26.03 &lt;/div&gt; &lt;p&gt;I found the results interesting to say the least. Observations:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;When summing a double[] any of the obvious ways are good.  &lt;li&gt;When summing an int[] there&amp;#39;s a &lt;em&gt;slight&lt;/em&gt; benefit to using a for loop, but &lt;em&gt;don&amp;#39;t&lt;/em&gt; try to optimise it yourself - I believe the JIT recognizes the for loop pattern and removes array bounds checking, but not when the length is hoisted. Note the lack of difference when summing doubles - I &lt;em&gt;suspect&lt;/em&gt; that this is because the iteration part is more significant when summing ints because integer addition is blindingly fast. This is important - adding integers is about as little work as you&amp;#39;re liikely to do in a loop; if you&amp;#39;re doing any &lt;em&gt;real&lt;/em&gt; work (even as trivial as adding two doubles together) the difference between for and foreach is negligible  &lt;li&gt;Our IEnumerableForEach method has pretty much the same performance as Enumerable.Sum - which isn&amp;#39;t really surprising, as it&amp;#39;s basically the same code. (At some point I might include Marc Gravell&amp;#39;s generic operators to see how they do.)  &lt;li&gt;Using a general IEnumerable&amp;lt;T&amp;gt; instead of the specific List&amp;lt;T&amp;gt; makes a pretty huge difference to the performance - I assume this is because the JIT inlines the List&amp;lt;T&amp;gt; code, and it doesn&amp;#39;t need to create an object because List&amp;lt;T&amp;gt;.Enumerator is a value type. (The enumerator will get boxed in the general version, I believe.)  &lt;li&gt;When using a for loop over a list, hosting the length in the for loop &lt;em&gt;helped&lt;/em&gt; in the double version, but &lt;em&gt;hindered&lt;/em&gt; in the int version. I&amp;#39;ve no idea why this happens.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;If anyone fancies running this on their own box and letting me know if they get very different results, that would be really interesting. Likewise let me know if you want me to add any more tests into the mix.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1667108" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>Benchmarking made easy</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/26/benchmarking-made-easy.aspx</link><pubDate>Mon, 26 Jan 2009 20:57:55 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1666084</guid><dc:creator>skeet</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1666084</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1666084</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/01/26/benchmarking-made-easy.aspx#comments</comments><description>&lt;p&gt;While I was answering a &lt;a href="http://stackoverflow.com/questions/472191/c-for-vs-foreach"&gt;Stack Overflow question&lt;/a&gt; on the performance implications of using a for loop instead of a foreach loop (or vice versa) I promised to blog about the results - particularly as I was getting different results to some other posters.&lt;/p&gt; &lt;p&gt;On Saturday I started writing the bigger benchmark (which I will post about in the fullness of time) and used a technique that I&amp;#39;d used when answering a different question: have a single timing method and pass it a delegate, expected input and expected output. You can ask a delegate for the associated method and thus find out its name (for normal methods, anyway - anonymous functions won&amp;#39;t give you anything useful, of course) so that&amp;#39;s all the information you really need to run the test.&lt;/p&gt; &lt;p&gt;I&amp;#39;ve often shied away from using delegates for benchmarking on the grounds of it interfering with the results - including the code inline with the iteration and timing obviously has a bit less overhead. However, the CLR is so fast at delegate invocation these days that it&amp;#39;s really not an issue for benchmarks where each iteration does any real work at all.&lt;/p&gt; &lt;p&gt;It&amp;#39;s still a pain to have to write that testing infrastructure each time, however. A very long time ago I wrote a &lt;a href="http://www.yoda.arachsys.com/csharp/benchmark.html"&gt;small attribute-based framework&lt;/a&gt;. It worked well enough, but I&amp;#39;ve found myself ignoring it - I&amp;#39;ve barely used it despite writing many, many benchmarks (mostly for newsgroup, blog and Stack Overflow posts) over the course of the years. I&amp;#39;m hoping that the new framework will prove more practical.&lt;/p&gt; &lt;p&gt;There are a few core concepts and (as always) a few assumptions:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;A &lt;em&gt;benchmark test&lt;/em&gt; is a function which takes a single value and returns a single value. This is expressed generically, of course, so you can make the input and output whatever type you like. A test also has a descriptive name, although this can often be inferred as the name of the function itself. The function will be called many times, the exact number being automatically determined.  &lt;li&gt;A &lt;em&gt;test suite&lt;/em&gt; is a collection of benchmark tests and another descriptive name, as well as the input to supply to each test and the expected output.  &lt;li&gt;A &lt;em&gt;benchmark result&lt;/em&gt; has a duration and an iteration count, as well as the descriptive name of the test which was run to produce the result. Results can be scaled so that either the duration or the iteration count matches another result. Likewise a result has a score, which is simply the duration (in ticks, but it&amp;#39;s pretty arbitrary) divided by the iteration count. Again, the score can be retrieved in a scaled fashion, using a specified result as a &amp;quot;standard&amp;quot; with a scaled score of 1.0. Lower is always better.  &lt;li&gt;A &lt;em&gt;result suite&lt;/em&gt; is simply the result of running a test suite. A result suite can be scaled, which is equivalent to building a new result suite with scaled copies of each original result. The result suite contains the smarts to display the results.  &lt;li&gt;Running a test consists of two phases. First, we guess &lt;em&gt;roughly&lt;/em&gt; how fast the function is. We run 1 iteration, then 2, then 4, then 8 etc - until it takes at least 3 seconds. At that point we scale up the number of iterations so that the real test will last around 30 seconds. This is the one we record. The final iteration of each set is tested for correctness based on the expected output. Currently the 3 and 30 second targets are hard-coded; I could perhaps make them parameters somewhere, but I don&amp;#39;t want to overcomplicate things.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Now for the interesting bit (from my point of view, anyway): I decided that this would be a perfect situation to try playing with a functional style. As a result, &lt;em&gt;everything&lt;/em&gt; in the framework is immutable. When you &amp;quot;add&amp;quot; a test to a test suite, it returns a new test suite with the extra test. Running the test suite returns the result suite; scaling the result suite returns a new result suite; scaling a result returns a new result etc.&lt;/p&gt; &lt;p&gt;The one downside of this (beyond a bit of inefficiency in list copying) is that C# collection initializers only work with mutable collections. They also only work with direct constructor calls, whereas generic type inference &lt;em&gt;doesn&amp;#39;t&lt;/em&gt; apply to constructors. In the end, the &amp;quot;static generic factory method&amp;quot; combined with simple Add method calls yields quite nice results, even though I can&amp;#39;t use a collection initializer:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="ValueType"&gt;double&lt;/span&gt;[] array = ...; &lt;span class="InlineComment"&gt;// Code to generate random array of doubles&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; results = TestSuite.Create(&lt;span class="String"&gt;&amp;quot;Array&amp;quot;&lt;/span&gt;, array, array.Sum())&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(ArrayFor)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(ArrayForEach)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(Enumerable.Sum, &lt;span class="String"&gt;&amp;quot;LINQ Enumerable.Sum&amp;quot;&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .RunTests()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ScaleByBest(ScalingMode.VaryDuration);&lt;br /&gt;&lt;br /&gt;results.Display(ResultColumns.NameAndDuration); &lt;/div&gt; &lt;p&gt;This is a pretty small amount of extra code to write, beyond the code we actually want to benchmark (the ArrayFor and ArrayForEach methods in particular). No looping by iteration count, no guessing at the number of iterations and rerunning it until it lasts a reasonable amount of time, etc.&lt;/p&gt; &lt;p&gt;My only regret is that I haven&amp;#39;t written this in a test-driven way. There are currently no unit tests at all. Such is the way of projects that start off as &amp;quot;let&amp;#39;s just knock something together&amp;quot; and end up being rather bigger than originally intended.&lt;/p&gt; &lt;p&gt;At some point I&amp;#39;ll make it all downloadable from my main C# page, in normal source form, binary form, and also a &amp;quot;single source file&amp;quot; form so you can compile your benchmark with just &lt;code&gt;csc /o+ /debug- Bench*.cs&lt;/code&gt; to avoid checking the assembly filename each time you use it. For the moment, here&amp;#39;s a &lt;a href="http://pobox.com/~skeet/csharp/blogfiles/BenchmarkHelper.zip"&gt;zip of the source code&lt;/a&gt; and a &lt;a href="http://pobox.com/~skeet/csharp/blogfiles/BenchmarkDemo.cs"&gt;short sample program&lt;/a&gt;, should you find them useful. Obviously it&amp;#39;s early days - there&amp;#39;s a lot more that I &lt;em&gt;could&lt;/em&gt; add. Feedback would help!&lt;/p&gt; &lt;p&gt;Next time (hopefully fairly soon) I&amp;#39;ll post the for/foreach benchmark and results.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1666084" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>Designing LINQ operators</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx</link><pubDate>Fri, 23 Jan 2009 23:40:39 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1665190</guid><dc:creator>skeet</dc:creator><slash:comments>16</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1665190</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1665190</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/01/23/designing-linq-operators.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;ve started a small project (I&amp;#39;ll post a link when I&amp;#39;ve actually got something worthwhile to show) with some extra LINQ operators in - things which I think are missing from LINQ to Objects, basically. (I hope to include many of the ideas from an &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/10/23/what-other-enumerable-extension-methods-would-you-like-to-see.aspx"&gt;earlier blog post&lt;/a&gt;.) That, and a few Stack Overflow questions where I&amp;#39;ve effectively written extra LINQ operators and compared them with other solutions, have made me think about the desirable properties of a LINQ operator - or at least the things you should think about when implementing one. My thoughts so far:&lt;/p&gt; &lt;h3&gt;Lazy/eager execution&lt;/h3&gt; &lt;p&gt;If you&amp;#39;re returning a sequence (i.e. another IEnumerable&amp;lt;T&amp;gt; or similar) the execution should almost certainly be lazy, but the parameter checking should be eager. Unfortunately with the &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx"&gt;limitations of the (otherwise wonderful) C# iterator blocks&lt;/a&gt;, this usually means breaking the method into two, like this:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; Where(&lt;span class="Keyword"&gt;this&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; source,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Func&amp;lt;T, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; predicate)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Eagerly executed&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;if&lt;/span&gt; (source == &lt;span class="Keyword"&gt;null&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="String"&gt;&amp;quot;source&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;if&lt;/span&gt; (predicate == &lt;span class="Keyword"&gt;null&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="String"&gt;&amp;quot;predicate&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; WhereImpl(source, predicate);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; WhereImpl(IEnumerable&amp;lt;T&amp;gt; source,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Func&amp;lt;T, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; predicate)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Lazily executed&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;foreach&lt;/span&gt; (T element &lt;span class="Statement"&gt;in&lt;/span&gt; source)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;if&lt;/span&gt; (predicate(element))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;yield&lt;/span&gt;&amp;nbsp;&lt;span class="Statement"&gt;return&lt;/span&gt; element;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;Obviously aggregates and conversions (Max, ToList etc) are generally eager anyway, within normal LINQ to Objects. (Just about everything in Push LINQ is lazy. They say pets look like their owners...)&lt;/p&gt; &lt;h3&gt;Streaming/buffering&lt;/h3&gt; &lt;p&gt;One of my favourite features of LINQ to Objects (and one which doesn&amp;#39;t get nearly the publicity of deferred execution) is that many of the operators &lt;em&gt;stream&lt;/em&gt; the data. In other words, they only consume data when they absolutely have to, and they yield data as soon as they can. This means you can process vast amounts of data with very little memory usage, so long as you use the right operators. Of course, not every operator can stream (reversing requires buffering, for example) but where it&amp;#39;s possible, it&amp;#39;s really handy.&lt;/p&gt; &lt;p&gt;Unfortunately, the streaming/buffering nature of operators isn&amp;#39;t well documented in MSDN - and sometimes it&amp;#39;s completely wrong. As I&amp;#39;ve &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/09/09/logging-enumeration-flow.aspx"&gt;noted before&lt;/a&gt;, the &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.intersect.aspx"&gt;docs for Enumerable.Intersect&lt;/a&gt; claim that it reads the &lt;em&gt;whole&lt;/em&gt; of both sequences (&lt;em&gt;first&lt;/em&gt; then &lt;em&gt;second&lt;/em&gt;) before yielding any data. In fact it reads and buffers the whole of &lt;em&gt;second&lt;/em&gt;, then streams &lt;em&gt;first&lt;/em&gt;, yielding intersecting elements as it goes. I strongly encourage new LINQ operators to document their streaming/buffering behaviour (accurately!). This will limit future changes in the implementation admittedly (Intersect can be implemented in a manner where both inputs are streamed, for example) but in this case I think the extra guarantees provided by the documentation make up for that restriction.&lt;/p&gt; &lt;h3&gt;Once-only evaluation&lt;/h3&gt; &lt;p&gt;When I said that reversing requires buffering earlier on, I was sort of lying. Here&amp;#39;s an implementation of Reverse which doesn&amp;#39;t buffer any data anywhere:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; StreamingReverse&amp;lt;T&amp;gt;(&lt;span class="Keyword"&gt;this&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; source)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Error checking omitted for brevity&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ValueType"&gt;int&lt;/span&gt; count = source.Count();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;for&lt;/span&gt; (&lt;span class="ValueType"&gt;int&lt;/span&gt; i = count-1; i &amp;gt;= 0; i--)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;yield&lt;/span&gt;&amp;nbsp;&lt;span class="Statement"&gt;return&lt;/span&gt; source.ElementAt(i);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;If we assume we can read the sequence as often as we like, then we never need to buffer anything - just treat it as a random-access list. I hope I don&amp;#39;t have to tell you that&amp;#39;s a really, &lt;em&gt;really&lt;/em&gt; bad idea. Leaving aside the blatant inefficiency even for sequences like lists which are cheap to iterate over, some sequences are inherently once-only (think about reading from a network stream) and some are inherently costly to iterate over (think about lines in a big log file - or the result of an ordering).&lt;/p&gt; &lt;p&gt;I suspect that developers using LINQ operators &lt;em&gt;assume&lt;/em&gt; that they&amp;#39;ll only read the input data once. That&amp;#39;s a good assumption - wherever possible, we ought to make sure that it&amp;#39;s correct, and if we absolutely can&amp;#39;t help evaluating a sequence twice (and I can&amp;#39;t remember any times when I&amp;#39;ve really wanted to do that) we should document it in large, friendly letters.&lt;/p&gt; &lt;h3&gt;Mind your complexity&lt;/h3&gt; &lt;p&gt;In some ways, this falls out of &amp;quot;try to stream, and try to only read once&amp;quot; - if you&amp;#39;re not storing any data and you&amp;#39;re only reading each item once, it&amp;#39;s quite hard to come up with an operator which &lt;em&gt;isn&amp;#39;t&lt;/em&gt; just O(n) for a single sequence. It is worth thinking about though - particularly as most of the LINQ operators &lt;em&gt;can&lt;/em&gt; work with large amounts of data. For example, to find the smallest element in a sequence you can &lt;em&gt;either&lt;/em&gt; sort the whole sequence and take the first element of the result &lt;em&gt;or&lt;/em&gt; you can keep track of a &amp;quot;current minimum&amp;quot; and iterate through the whole sequence. Clearly the latter saves a lot of complexity (and doesn&amp;#39;t require buffering) - so don&amp;#39;t just take the first idea that comes into your head. (Or at least, start with that and then think how you could improve it.)&lt;/p&gt; &lt;p&gt;Again, documenting the complexity of the operator is a good idea, and call particular attention to anything which is &lt;em&gt;unintuitively&lt;/em&gt; expensive.&lt;/p&gt; &lt;h3&gt;Conclusion&lt;/h3&gt; &lt;p&gt;Okay, so there&amp;#39;s nothing earth-shattering here. But the more I use LINQ to answer Stack Overflow questions, and the more I invent new operators in the &lt;em&gt;spirit&lt;/em&gt; of the existing ones, the more powerful I think it is. It&amp;#39;s amazing how powerful it can be, and how ridiculously simple the code (sometimes) looks afterwards. It&amp;#39;s not like the operator implementation is usually hard, either - it&amp;#39;s just a matter of thinking of the right concepts. I&amp;#39;m going to try to follow these principles when I implement my extra operator library, and I hope you&amp;#39;ll bear them in mind too, should you ever feel that LINQ to Objects doesn&amp;#39;t have &lt;em&gt;quite&lt;/em&gt; the extension method you need...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1665190" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>You don't have to use query expressions to use LINQ</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/01/07/you-don-t-have-to-use-query-expressions-to-use-linq.aspx</link><pubDate>Wed, 07 Jan 2009 17:32:24 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1658790</guid><dc:creator>skeet</dc:creator><slash:comments>18</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1658790</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1658790</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/01/07/you-don-t-have-to-use-query-expressions-to-use-linq.aspx#comments</comments><description>&lt;p&gt;LINQ is clearly gaining a fair amount of traction, given the number of posts I see about it on &lt;a href="http://stackoverflow.com"&gt;Stack Overflow&lt;/a&gt;. However, I&amp;#39;ve noticed an interesting piece of coding style: a lot of developers are using query expressions for &lt;em&gt;every&lt;/em&gt; bit of LINQ they write, however trivial.&lt;/p&gt; &lt;p&gt;Now, don&amp;#39;t get the wrong idea - I love query expressions as a helpful piece of syntactic sugar. For instance, I&amp;#39;d always pick the query expression form over the &amp;quot;dot notation&amp;quot; form for something like this:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; query = &lt;span class="Linq"&gt;from&lt;/span&gt; file &lt;span class="Statement"&gt;in&lt;/span&gt; Directory.GetFiles(logDirectory, &lt;span class="String"&gt;&amp;quot;*.log&amp;quot;&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;from&lt;/span&gt; line &lt;span class="Statement"&gt;in&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt; LineReader(file)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;let&lt;/span&gt; entry = &lt;span class="Keyword"&gt;new&lt;/span&gt; LogEntry(line)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; entry.Severity == Severity.Error&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;select&lt;/span&gt; file + &lt;span class="String"&gt;&amp;quot;: &amp;quot;&lt;/span&gt; + entry.Message; &lt;/div&gt; &lt;p&gt;(Yes, it&amp;#39;s yet another log entry example - it&amp;#39;s one of my favourite demos of LINQ, and particularly Push LINQ.) The equivalent code using just the extension methods would be pretty ugly, especially given the various range variables and transparent identifiers involved.&lt;/p&gt; &lt;p&gt;However, look at &lt;em&gt;these&lt;/em&gt; two queries instead:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; query = &lt;span class="Linq"&gt;from&lt;/span&gt; person &lt;span class="Statement"&gt;in&lt;/span&gt; people&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; person.Salary &amp;gt; 10000m&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;select&lt;/span&gt; person;&lt;br /&gt;&lt;br /&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; dotNotation = people.Where(person =&amp;gt; person.Salary &amp;gt; 10000m); &lt;/div&gt; &lt;p&gt;In this case, we&amp;#39;re just making a single method call. Why bother with three lines of query expression? If the query becomes more complicated later, it can easily be converted into a query expression at that point. The two queries are &lt;em&gt;exactly&lt;/em&gt; the same, even though the syntax is different.&lt;/p&gt; &lt;p&gt;My guess is that there&amp;#39;s a &amp;quot;black magic&amp;quot; fear of LINQ - many developers know how to write query expressions, but aren&amp;#39;t confident about what they&amp;#39;re converted into (or even the basics of what the translation process is like in the first place). Most of the C# 3.0 and LINQ books that I&amp;#39;ve read &lt;em&gt;do&lt;/em&gt; cover query expression translation to a greater or lesser extent, but it&amp;#39;s rarely given much prominence.&lt;/p&gt; &lt;p&gt;I suspect the black magic element is reinforced by the inherent &amp;quot;will it work?&amp;quot; factor of LINQ to SQL - you get to write the query in your favourite language, but you may well not be confident in it working until you&amp;#39;ve tried it; there will always be plenty of little gotchas which can&amp;#39;t be picked up at compile time. With LINQ to Objects, there&amp;#39;s a lot more certainty (at least in my experience). However, the query expression translation &lt;em&gt;shouldn&amp;#39;t&lt;/em&gt; be part of what developers are wary of. It&amp;#39;s clearly defined in the spec (not that I&amp;#39;m suggesting that all developers should learn it via the spec) and benefits from being relatively dumb and therefore easy to predict.&lt;/p&gt; &lt;p&gt;So next time you&amp;#39;re writing a query expression, take a look at it afterwards - if it&amp;#39;s simple, try writing it without the extra syntactic sugar. It may just be sweet enough on its own.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1658790" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>Value types and parameterless constructors</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/12/10/value-types-and-parameterless-constructors.aspx</link><pubDate>Wed, 10 Dec 2008 17:55:40 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1656405</guid><dc:creator>skeet</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1656405</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1656405</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/12/10/value-types-and-parameterless-constructors.aspx#comments</comments><description>&lt;p&gt;There have been a couple of questions on StackOverflow about value types and parameterless constructors:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://stackoverflow.com/questions/203695"&gt;Structure vs Class in C#&lt;/a&gt;  &lt;li&gt;&lt;a href="http://stackoverflow.com/questions/333829"&gt;Why can’t I define a default constructor for a struct in .NET&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I learned quite a bit when answering both of these. When a &lt;a href="http://stackoverflow.com/questions/354136"&gt;further question&lt;/a&gt; about the default value of a type (particularly with respect to generics) came up, I thought it would be worth delving into a bit more depth. Very little of this is actually relevant most of the time, but it&amp;#39;s interesting nonetheless.&lt;/p&gt; &lt;p&gt;I won&amp;#39;t go over most of the details I discovered in &lt;a href="http://stackoverflow.com/questions/203695/structure-vs-class-in-c#204009"&gt;my answer to the first question&lt;/a&gt;,&amp;nbsp; but if you&amp;#39;re interested in the IL generated by the statement &amp;quot;x = new Guid();&amp;quot; then have a look there for more details.&lt;/p&gt; &lt;p&gt;Let&amp;#39;s start off with the first and most important thing I&amp;#39;ve learned about value types recently:&lt;/p&gt; &lt;h3&gt;Yes, you &lt;em&gt;can&lt;/em&gt; write a parameterless constructor for a value type in .NET&lt;/h3&gt; &lt;p&gt;I very carefully wrote &amp;quot;in .NET&amp;quot; there - &amp;quot;in C#&amp;quot; would have been incorrect. I had always believed that the CLI spec prohibited value types from having parameterless constructors. (The C# spec used the terminology in a slightly different way - it treats &lt;em&gt;all&lt;/em&gt; value types as having a parameterless constructor. This makes the language more consistent for the most part, but it does give rise to some interesting behaviour which we&amp;#39;ll see later on.)&lt;/p&gt; &lt;p&gt;It turns out that if you write your value type in IL, you can provide your own parameterless constructor with custom code without ilasm complaining at all. It&amp;#39;s possible that other languages targeting the CLI allow you to do this as well, but as I don&amp;#39;t know any, I&amp;#39;ll stick to IL. Unfortunately I don&amp;#39;t know IL terribly well, so I thought I&amp;#39;d just start off with some C# and go from there:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;struct&lt;/span&gt; Oddity&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Oddity(&lt;span class="ValueType"&gt;int&lt;/span&gt; removeMe)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Oddity constructor called&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;I compiled that into its own class library, and then disassembled it with &lt;code&gt;ildasm /out:Oddity.il Oddity.dll&lt;/code&gt;. After changing the constructor to be parameterless, removing a few comments, and removing some compiler-generated assembly attributes) I ended up with this IL:&lt;/p&gt;&lt;pre&gt;.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )  
  .ver 2:0:0:0
}
.assembly Oddity
{
  .hash algorithm 0x00008004
  .ver 0:0:0:0
}
.module Oddity.dll
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003
.corflags 0x00000001

.class public sequential ansi sealed beforefieldinit Oddity
       extends [mscorlib]System.ValueType
{
  .pack 0
  .size 1
  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldstr      &amp;quot;Oddity constructor called&amp;quot;
    IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
    IL_000b:  nop
    IL_000c:  ret
  }
}
&lt;/pre&gt;
&lt;p&gt;I reassembled this with &lt;code&gt;ilasm /dll /out:Oddity.dll Oddity.il&lt;/code&gt;. So far, so good. We have a value type with a custom constructor in a class library. It doesn&amp;#39;t do anything particularly clever - it just logs that it&amp;#39;s been called. That&amp;#39;s enough for our test program.&lt;/p&gt;
&lt;h2&gt;When does the parameterless constructor get called?&lt;/h2&gt;
&lt;p&gt;There are various things one &lt;i&gt;could&lt;/i&gt; investigate about parameterless constructors, but I&amp;#39;m mostly interested in when they get called. The test application is reasonably simple, but contains lots of cases - each writes to the console what it&amp;#39;s about to do, then does something which &lt;i&gt;might&lt;/i&gt; call the constructor. Without further ado:&lt;/p&gt;
&lt;div class="code"&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System.Runtime.CompilerServices;&lt;br /&gt;&lt;br /&gt;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Test&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; Oddity staticField;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Oddity instanceField;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;Declaring local variable&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Oddity localDeclarationOnly;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// No variables within the value, so we can use it&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// without inializing anything&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;Boxing&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;object&lt;/span&gt; o = localDeclarationOnly;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Just make sure it&amp;#39;s really done it&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(o.ToString());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;new Oddity() - set local variable&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Oddity local = &lt;span class="Keyword"&gt;new&lt;/span&gt; Oddity();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;Create instance of Test - contains instance variable&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Test t = &lt;span class="Keyword"&gt;new&lt;/span&gt; Test();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;new Oddity() - set instance field&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t.instanceField = &lt;span class="Keyword"&gt;new&lt;/span&gt; Oddity();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;new Oddity() - set static field&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; staticField = &lt;span class="Keyword"&gt;new&lt;/span&gt; Oddity();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;new Oddity[10]&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; o = &lt;span class="Keyword"&gt;new&lt;/span&gt; Oddity[10];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;Passing argument to method&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MethodWithParameter(local);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GenericMethod&amp;lt;Oddity&amp;gt;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GenericMethod2&amp;lt;Oddity&amp;gt;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;Activator.CreateInstance(typeof(Oddity))&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Activator.CreateInstance(&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(Oddity));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;Activator.CreateInstance&amp;lt;Oddity&amp;gt;()&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Activator.CreateInstance&amp;lt;Oddity&amp;gt;();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [MethodImpl(MethodImplOptions.NoInlining)]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; MethodWithParameter(Oddity oddity)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// No need to do anything&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; GenericMethod&amp;lt;T&amp;gt;() &lt;span class="Linq"&gt;where&lt;/span&gt; T : &lt;span class="Keyword"&gt;new&lt;/span&gt;()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;default(T) in generic method with new() constraint&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T t = &lt;span class="Modifier"&gt;default&lt;/span&gt;(T);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;new T() in generic method with new() constraint&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t = &lt;span class="Keyword"&gt;new&lt;/span&gt; T();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; GenericMethod2&amp;lt;T&amp;gt;() &lt;span class="Linq"&gt;where&lt;/span&gt; T : &lt;span class="ValueType"&gt;struct&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;default(T) in generic method with struct constraint&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T t = &lt;span class="Modifier"&gt;default&lt;/span&gt;(T);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Report(&lt;span class="String"&gt;&amp;quot;new T() in generic method with struct constraint&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t = &lt;span class="Keyword"&gt;new&lt;/span&gt; T();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Report(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; text)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(text);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt;
&lt;p&gt;And here are the results:&lt;/p&gt;
&lt;div class="output"&gt;Declaring local variable&lt;br /&gt;Boxing&lt;br /&gt;Oddity&lt;br /&gt;new Oddity() - set local variable&lt;br /&gt;Oddity constructor called&lt;br /&gt;Create instance of Test - contains instance variable&lt;br /&gt;new Oddity() - set instance field&lt;br /&gt;Oddity constructor called&lt;br /&gt;new Oddity() - set static field&lt;br /&gt;Oddity constructor called&lt;br /&gt;new Oddity[10]&lt;br /&gt;Passing argument to method&lt;br /&gt;default(T) in generic method with new() constraint&lt;br /&gt;new T() in generic method with new() constraint&lt;br /&gt;default(T) in generic method with struct constraint&lt;br /&gt;new T() in generic method with struct constraint&lt;br /&gt;Activator.CreateInstance(typeof(Oddity))&lt;br /&gt;Oddity constructor called&lt;br /&gt;Activator.CreateInstance&amp;lt;Oddity&amp;gt;()&lt;br /&gt;Oddity constructor called &lt;/div&gt;
&lt;p&gt;So, to split these out: &lt;/p&gt;
&lt;h2&gt;Operations which &lt;i&gt;do&lt;/i&gt; call the constructor&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;new Oddity() - whatever we&amp;#39;re storing the result in. This isn&amp;#39;t much of a surprise. What may surprise you is that it gets called even if you compile Test.cs against the original Oddity.dll (without the custom parameterless constructor) and then just rebuild Oddity.dll. 
&lt;li&gt;&lt;code&gt;Activator.CreateInstance&amp;lt;T&amp;gt;()&lt;/code&gt; and &lt;code&gt;Activator.CreateInstance(Type)&lt;/code&gt;. I wouldn&amp;#39;t be particular surprised by this either way. &lt;/li&gt;&lt;/ul&gt;
&lt;h2&gt;Operations which &lt;i&gt;don&amp;#39;t&lt;/i&gt; call the constructor&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Just declaring a variable, whether local, static or instance 
&lt;li&gt;Boxing 
&lt;li&gt;Creating an array - good job, as this could be a real performance killer 
&lt;li&gt;Using &lt;code&gt;default(T) in a generic method - this one didn&amp;#39;t surprise me&lt;/code&gt; 
&lt;li&gt;Using &lt;code&gt;new T()&lt;/code&gt; in a generic method - this one really &lt;i&gt;did&lt;/i&gt; surprise me. Not only is it counterintuitive, but in IL it just calls &lt;code&gt;Activator.CreateInstance&amp;lt;T&amp;gt;()&lt;/code&gt;. What&amp;#39;s the difference between this and calling &lt;code&gt;Activator.CreateInstance&amp;lt;Oddity&amp;gt;()&lt;/code&gt;? I really don&amp;#39;t understand.&lt;/li&gt;&lt;/ul&gt;
&lt;h2&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;Well, I&amp;#39;m still glad that C# doesn&amp;#39;t let us define our own parameterless constructors for value types, given the behaviour. The main reason for using it - as far as I&amp;#39;ve seen - it to make sure that the &amp;quot;default value&amp;quot; for a type is sensible. Given that it&amp;#39;s possible to get a usable value of the type without the constructor being called, this wouldn&amp;#39;t work anyway. Writing such a constructor would be like making a value type mutable - almost always a bad idea.&lt;/p&gt;
&lt;p&gt;However, it&amp;#39;s nice to know it&amp;#39;s &lt;em&gt;possible&lt;/em&gt;, just on the grounds that learning new things is always a good thing. And at least next time someone asks a similar question, I&amp;#39;ll have somewhere to point them...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1656405" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Wacky+Ideas/default.aspx">Wacky Ideas</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>Redesigning System.Object/java.lang.Object</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/12/05/redesigning-system-object-java-lang-object.aspx</link><pubDate>Fri, 05 Dec 2008 17:52:44 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1655908</guid><dc:creator>skeet</dc:creator><slash:comments>31</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1655908</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1655908</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/12/05/redesigning-system-object-java-lang-object.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;ve had quite a few discussions with a colleague about some failures of Java and .NET. The issue we keep coming back to is the root of the inheritance tree. There&amp;#39;s no doubt in my mind that &lt;em&gt;having&lt;/em&gt; a tree with a single top-level class is a good thing, but it&amp;#39;s grown a bit too big for its boots.&lt;/p&gt; &lt;p&gt;Pretty much everything in this post applies to both .NET and Java, sometimes with a few small changes. Where it might be unclear, I&amp;#39;ll point out the changes explicitly - otherwise I&amp;#39;ll just use the .NET terminology.&lt;/p&gt; &lt;h3&gt;What&amp;#39;s in &lt;a href="http://msdn.microsoft.com/en-us/library/system.object.aspx"&gt;System.Object&lt;/a&gt;?&lt;/h3&gt; &lt;p&gt;Before we work out what we might be able to change, let&amp;#39;s look at what we&amp;#39;ve got. I&amp;#39;m only talking about instance methods. At the moment:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;A &lt;a href="http://msdn.microsoft.com/en-us/library/system.object.object.aspx"&gt;parameterless constructor&lt;/a&gt;  &lt;li&gt;A &lt;a href="http://msdn.microsoft.com/en-us/library/system.object.finalize.aspx"&gt;finalizer&lt;/a&gt;  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx"&gt;GetType()&lt;/a&gt;  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx"&gt;GetHashCode()&lt;/a&gt;  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.object.equals.aspx"&gt;Equals(Object)&lt;/a&gt;  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx"&gt;ToString()&lt;/a&gt;  &lt;li&gt;(&lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.wait.aspx"&gt;Monitor.Wait&lt;/a&gt;)  &lt;li&gt;(&lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.pulse.aspx"&gt;Monitor.Pulse&lt;/a&gt;)  &lt;li&gt;(&lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.pulseall.aspx"&gt;Monitor.PulseAll&lt;/a&gt;)  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx"&gt;MemberwiseClone&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;h3&gt;Life-cycle and type identity&lt;/h3&gt; &lt;p&gt;There are three members which I believe really need to be left alone.&lt;/p&gt; &lt;p&gt;We need a parameterless constructor because (at least with the current system of chaining constructors to each other) we have to have &lt;em&gt;some&lt;/em&gt; constructor, and I can&amp;#39;t imagine what parameter we might want to give it. I certainly find it hard to believe there&amp;#39;s a particular piece of state which really deserves to be a part of &lt;em&gt;every&lt;/em&gt; object but which we&amp;#39;re currently missing.&lt;/p&gt; &lt;p&gt;I really don&amp;#39;t care that much about finalizers. Should the finalizer be part of Object itself, or should it just get handled automatically by the CLR if and only if it&amp;#39;s defined somewhere in the inheritance chain? Frankly, who cares. No doubt it makes a big difference to the implementation somewhere, but that&amp;#39;s not my problem. All I care about when it comes to finalizers is that when I have to write them it&amp;#39;s as easy as possible to do it properly, and that I don&amp;#39;t have to write them very often in the first place. (With &lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle.aspx"&gt;SafeHandle&lt;/a&gt;, it should be a pretty rare occurrence in .NET, even when you&amp;#39;re dealing directly with unmanaged resources.)&lt;/p&gt; &lt;p&gt;GetType() or (getClass() in Java) is pretty important. I can&amp;#39;t see any particular alternative to having this within Object, unless you make it a static method somewhere else with an Object parameter. In fact, that would have the advantage of freeing up the name for use within your own classes. The functionality is sufficiently important (and really does apply to every object) that I think it&amp;#39;s worth keeping.&lt;/p&gt; &lt;h3&gt;Comparison methods&lt;/h3&gt; &lt;p&gt;Okay, time to get controversial. I don&amp;#39;t think every object should have to be able to compare itself with another object. Of course, most types don&amp;#39;t really support this anyway - we just end up with reference equality by default.&lt;/p&gt; &lt;p&gt;The trouble with comparisons is that everyone&amp;#39;s got a different idea of what makes something equal. There are some types where it really is obvious - there&amp;#39;s only one natural comparison. Integers spring to mind. There are other types which have multiple natural equality comparisons - floating point numbers (exact, within an absolute epsilon, and within a relative epsilon) and strings (ordinal, culture sensitive and/or case sensitive) are examples of this. Then there are composite types where you may or may not care about certain aspects - when comparing URLs, do I care about case? Do I care about fragments? For http, if the port number is explicitly specified as 80, is that different to a URL which is still http but leaves the port number implicit?&lt;/p&gt; &lt;p&gt;.NET represents these reasonably well already, with the &lt;a href="http://msdn.microsoft.com/en-us/library/ms131187.aspx"&gt;IEquatable&amp;lt;T&amp;gt;&lt;/a&gt; interface saying &amp;quot;I know how to compare myself with an instance of type T, and how to produce a hashcode for myself&amp;quot; and &lt;a href="http://msdn.microsoft.com/en-us/library/ms132151.aspx"&gt;IEqualityComparer&amp;lt;T&amp;gt;&lt;/a&gt; interface saying &amp;quot;I know how to compare two instances of T, and how to produce a hashcode for one instance of T.&amp;quot; Now suppose we didn&amp;#39;t have the (nongeneric!) Equals() method and GetHashCode() in System.Object. Any type which had a natural equality comparison would still let you compare it for equality by implementing IEquatable&amp;lt;T&amp;gt;.Equals - but anything else would either force you to use reference equality or an implementation of IEqualityComparer&amp;lt;T&amp;gt;.&lt;/p&gt; &lt;p&gt;Some of the principle consumers of equality comparisons are collections - particularly dictionaries (which is why it&amp;#39;s so important that the interfaces should include hashcode generation). With the current way that .NET generics work, it would be tricky to have a constraint on a &lt;em&gt;constructor&lt;/em&gt; such that if you only specified the types, it would only work if the key type implemented IEquatable&amp;lt;T&amp;gt;, but it&amp;#39;s easy enough to do with static methods (on a non-generic type). Alternatively you could specify any type and an appropriate IEqualityComparer&amp;lt;T&amp;gt; to use for the keys. We&amp;#39;d need an IdentityComparer&amp;lt;T&amp;gt; to work just with references (and provide the equivalent functionaliy to Object.GetHashCode) but that&amp;#39;s not hard - and it would be &lt;em&gt;absolutely&lt;/em&gt; obvious what the comparison was when you built the dictionary.&lt;/p&gt; &lt;h3&gt;Monitors and threading&lt;/h3&gt; &lt;p&gt;This is possibly my biggest gripe. The fact that every object has a monitor associated with it was a mistake in Java, and was unfortunately copied in .NET. This promotes the bad practice of locking on &amp;quot;this&amp;quot; and on types - both of which are typically publicly accessible references. I believe that unless a reference is exposed explicitly for the purpose of locking (like &lt;a href="http://msdn.microsoft.com/en-us/library/system.collections.icollection.syncroot.aspx"&gt;ICollection.SyncRoot&lt;/a&gt;) then you should avoid locking on any reference which other code knows about. I typically have a private read-only variable for locking purposes. If you&amp;#39;re following these guidelines, it makes no sense to be able to lock on absolutely any reference - it would be better to make the Monitor class instantiable, and make Wait/Pulse/PulseAll instance members. (In Java this would mean creating a new class and moving Object.wait/notify/notifyAll members to that class.)&lt;/p&gt; &lt;p&gt;This would lead to cleaner, more readable code in my view. I&amp;#39;d also do away with the &amp;quot;lock&amp;quot; statement in C#, making Monitor.Enter return a token implementing IDisposable - so &amp;quot;using&amp;quot; statements would replace locks, freeing up a keyword &lt;em&gt;and&lt;/em&gt; giving the flexibility of having multiple overloads of Monitor.Enter. Arguably if one were redesigning all of this anyway, it would be worth looking at whether or not monitors should really be reentrant. Any time you &lt;em&gt;use&lt;/em&gt; lock reentrancy, you&amp;#39;re probably not thinking hard enough about the design. Now there&amp;#39;s a nice overgeneralisation with which to end this section...&lt;/p&gt; &lt;h3&gt;String representations&lt;/h3&gt; &lt;p&gt;This is an interesting one. I&amp;#39;m genuinely on the fence here. I find ToString() (and the fact that it&amp;#39;s called implicitly in many circumstances) hugely useful, but it feels like it&amp;#39;s attempting to satisfy three different goals:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Giving a developer-readable representation when logging and debugging&lt;/li&gt; &lt;li&gt;Giving a user-readable representation as part of a formatted message in a UI&lt;/li&gt; &lt;li&gt;Giving a machine-readable format (although this is relatively rare for anything other than numeric types)&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;It&amp;#39;s interesting to note that Java and .NET differ as to which of these to use for numbers - Java plumps for &amp;quot;machine-readable&amp;quot; and .NET goes for &amp;quot;human-readable in the current thread&amp;#39;s culture&amp;quot;. Of course it&amp;#39;s clearer to explicitly specify the culture on both platforms.&lt;/p&gt; &lt;p&gt;The trouble is that &lt;em&gt;very&lt;/em&gt; often, it&amp;#39;s not immediately clear which of these has been implemented. This leads to guidelines such as &amp;quot;don&amp;#39;t use ToString() other than for logging&amp;quot; on the grounds that at least if it&amp;#39;s implemented inappropriately, it&amp;#39;ll only be a log file which ends up with difficult-to-understand data.&lt;/p&gt; &lt;p&gt;Should this usage be explicitly stated - perhaps even codified in the name: &amp;quot;ToDebugString&amp;quot; or something similar? I will leave this for smarter minds to think about, but I think there&amp;#39;s enough value in the method to make it worth keeping.&lt;/p&gt; &lt;h3&gt;MemberwiseClone&lt;/h3&gt; &lt;p&gt;Again, I&amp;#39;m not sure on this one. It would perhaps be better as a static (generic!) method somewhere in a class whose name indicated &amp;quot;this is for sneaky runtime stuff&amp;quot;. After all, it constructs a new object without calling a constructor, and other funkiness. I&amp;#39;m less bothered by this than the other items though.&lt;/p&gt; &lt;h3&gt;Conclusion&lt;/h3&gt; &lt;p&gt;To summarise, in an ideal world:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Equals and GetHashCode would disappear from Object. Types would have to explicitly say that they could be compared&lt;/li&gt; &lt;li&gt;Wait/Pulse/PulseAll would become instance methods in Monitor, which would be instantiated every time you want a lock.&lt;/li&gt; &lt;li&gt;ToString &lt;em&gt;might&lt;/em&gt; be renamed to give clearer usage guidance.&lt;/li&gt; &lt;li&gt;MemberwiseClone &lt;em&gt;might&lt;/em&gt; be moved to a different class.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Obviously it&amp;#39;s far too late for either Java or .NET to make these changes, but it&amp;#39;s always interesting to dream. Any more changes you&amp;#39;d like to see? Or violent disagreements with any of the above?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1655908" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Java/default.aspx">Java</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Wacky+Ideas/default.aspx">Wacky Ideas</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>The Snippy Reflector add-in</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/11/23/the-snippy-reflector-add-in.aspx</link><pubDate>Sun, 23 Nov 2008 10:25:53 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1654846</guid><dc:creator>skeet</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1654846</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1654846</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/11/23/the-snippy-reflector-add-in.aspx#comments</comments><description>&lt;p&gt;Those of you who&amp;#39;ve read C# in Depth will know about Snippy - a little tool which makes it easy to build complete programs from small snippets of code.&lt;/p&gt; &lt;p&gt;I&amp;#39;m delighted to say that reader &lt;a href="http://jasonhaley.com"&gt;Jason Haley&lt;/a&gt; has taken the &lt;a href="http://csharpindepth.com/Downloads.aspx"&gt;source code for Snippy&lt;/a&gt; and built an add-in for &lt;a href="http://www.red-gate.com/products/reflector/"&gt;Reflector&lt;/a&gt;. This will make it &lt;em&gt;much&lt;/em&gt; simpler to answer questions like &lt;a href="http://stackoverflow.com/questions/203695/structure-vs-class-in-c"&gt;this one about struct initialization&lt;/a&gt;, where you really want to the IL generated for a snippet. Here&amp;#39;s a screenshot to show what it does:&lt;/p&gt; &lt;p&gt;&lt;img src="http://pobox.com/~skeet/csharp/blogfiles/SnippyReflector.png" alt="" /&gt;&lt;/p&gt; &lt;p&gt;This is really cool - if you want to dabble to see what the C# compiler does in particular situations, check it out. It comes as &lt;a href="http://jasonhaley.com/files/Reflector.Snippy.dll"&gt;just the DLL&lt;/a&gt;, or a &lt;a href="http://jasonhaley.com/files/Reflector.Snippy.zip"&gt;zipped version&lt;/a&gt;. Thanks for putting in all this work, Jason :) &lt;p&gt;Update: Jason now has his own (more detailed) &lt;a href="http://jasonhaley.com/blog/archive/2008/11/23/142521.aspx"&gt;blog entry&lt;/a&gt; too.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1654846" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Books/default.aspx">Books</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>Copenhagen C# talk videos now up</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/11/19/copenhagen-c-talk-videos-now-up.aspx</link><pubDate>Wed, 19 Nov 2008 07:17:31 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1654528</guid><dc:creator>skeet</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1654528</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1654528</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/11/19/copenhagen-c-talk-videos-now-up.aspx#comments</comments><description>&lt;p&gt;&lt;/p&gt; &lt;p&gt;The videos from my &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/10/30/returning-from-copenhagen.aspx"&gt;one day talk&lt;/a&gt; about C# in Copenhagen are now on the &lt;a href="http://www.msdncommunity.dk/udviklere/webcasts"&gt;MSDN community site&lt;/a&gt;. There are eight sessions, varying between about 25 minutes and 50 minutes in length. I haven&amp;#39;t had time to watch them yet, but when I do I&amp;#39;ll submit brief summaries so you can quickly get to the bits you&amp;#39;re most interested in. (As far as I&amp;#39;m aware, they&amp;#39;re only available via Silverlight, which I realise isn&amp;#39;t going to be convenient for everyone.)&lt;/p&gt; &lt;p&gt;Feedback is very welcome.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1654528" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Speaking+engagements/default.aspx">Speaking engagements</category></item><item><title>.NET 4.0's game-changing feature? Maybe contracts...</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/11/06/net-4-0-s-game-changing-feature-maybe-contracts.aspx</link><pubDate>Thu, 06 Nov 2008 01:38:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1653210</guid><dc:creator>skeet</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1653210</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1653210</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/11/06/net-4-0-s-game-changing-feature-maybe-contracts.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; As Chris Nahr pointed out, there&amp;#39;s a &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/11/introduction-to-code-contracts-melitta-andersen.aspx"&gt;blog post by Melitta Andersen of the BCL team&lt;/a&gt; explaining this in more detail.&lt;/p&gt;
&lt;p&gt;Obviously I&amp;#39;ve been looking at the proposed C# 4.0 features pretty carefully, and I promise I&amp;#39;ll blog more about them at some later date - but yesterday I watched a PDC video which blew me away.&lt;/p&gt;
&lt;p&gt;As ever, a new version of .NET means more than just language changes - Justin van Patten has written an &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx"&gt;excellent blog post&lt;/a&gt; about what to expect in the core of thee framework. There are nice things in there - tuples and &lt;code&gt;BigInteger&lt;/code&gt;, for example - but it was code contracts that really caught my eye.&lt;/p&gt;
&lt;p&gt;Remember &lt;a href="http://research.microsoft.com/SpecSharp/"&gt;Spec#&lt;/a&gt;? Well, as far as I can tell the team behind it realised that people don&amp;#39;t really want to have to learn a new language - but if the goodness of Design By Contract can be put into a &lt;em&gt;library&lt;/em&gt;, then everyone can use it. Enter &lt;a href="http://research.microsoft.com/contracts"&gt;CodeContracts&lt;/a&gt;...&lt;/p&gt;
&lt;p&gt;Actual examples are relatively few and far between at the moment, but the basic idea is that you write your contracts at the start of methods - not in attributes, presumably because that&amp;#39;s too limiting in terms of what you can express - and then a post-build tool will &amp;quot;understand&amp;quot; those contracts, find potential issues, and do a bit of code-rewriting where appropriate (e.g. to move the post-condition testing to the end points of the method). Object invariants can also be expressed as separate methods.&lt;/p&gt;
&lt;p&gt;Rather than guess at the syntax in this blog post I highly recommend you watch the &lt;a href="http://channel9.msdn.com/pdc2008/TL51/"&gt;PDC 2008 video&lt;/a&gt; on both this and &lt;a href="http://research.microsoft.com/pex/"&gt;Pex&lt;/a&gt; (an intelligent code explorer and test generator). The teams have clearly thought through a lot of significant issues:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Contracts can be enforced at runtime, or stripped out for release builds. (I&amp;#39;ll be interested to see whether I can keep the pre-condition checks in the release build, just removing invariants and post-conditions etc.)&lt;/li&gt;
&lt;li&gt;If you&amp;#39;re stripping out the contracts, you can still have them in a separate assembly - so if you supply a library to someone, they can still have all the Design by Contract goodness available, and see where they&amp;#39;re potentially violating your preconditions&lt;/li&gt;
&lt;li&gt;Contracts will be automatically documented in the generated XML documentation file (although this has yet to be implemented, I believe)&lt;/li&gt;
&lt;li&gt;Interfaces can be associated with contract classes where the contracts are expressed. (They couldn&amp;#39;t be in the interface, as they require method bodies.)&lt;/li&gt;
&lt;li&gt;Pex will be able to generate tests in MS Test, NUnit and MbUnit. (Hooray! This got a massive cheer at PDC.)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now I should point out that I haven&amp;#39;t tried any of this - I&amp;#39;ve just watched a video which was very slick and obviously used a well-tested scenario. If this genuinely works, however, I think it could change the way mainstream developers approach coding just as LINQ is changing the way we see data. (Obviously there&amp;#39;s nothing fundamentally new about DbC - but there&amp;#39;s a difference between it existing and it being mainstream.)&lt;/p&gt;
&lt;p&gt;I&amp;#39;m really, really excited about this :) Definitely time to boot up the VPC image when I get a moment...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1653210" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>C# 4.0: dynamic&lt;T&gt; ?</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/30/c-4-0-dynamic-lt-t-gt.aspx</link><pubDate>Thu, 30 Oct 2008 22:04:10 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652571</guid><dc:creator>skeet</dc:creator><slash:comments>29</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1652571</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1652571</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/10/30/c-4-0-dynamic-lt-t-gt.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;ve not played with the VS2010 CTP much yet, and I&amp;#39;ve only looked briefly at the documentation and blogs about the new C# 4.0 &lt;code&gt;dynamic&lt;/code&gt; type, but a thought occurred to me: why not have the option of making it generic as a way of saying &amp;quot;I will dynamically support this set of operations&amp;quot;?&lt;/p&gt; &lt;p&gt;As an example of what I mean, suppose you have an interface &lt;code&gt;IMessageRouter&lt;/code&gt; like this:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessageRouter&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ValueType"&gt;void&lt;/span&gt; Send(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; message, &lt;span class="ReferenceType"&gt;string&lt;/span&gt; destination);&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;(This is an arbitrary example, by the way. The idea isn&amp;#39;t specifically more suitable for message routing than anything else.)&lt;/p&gt; &lt;p&gt;I may have various implementations, written in various languages (or COM) which support the &lt;code&gt;Send&lt;/code&gt; method with those parameters. Some of those implementations actually implement &lt;code&gt;IMessageRouter&lt;/code&gt; but some don&amp;#39;t. I&amp;#39;d like to be able to do the following:&lt;/p&gt; &lt;div class="code"&gt;dynamic&amp;lt;IMessageRouter&amp;gt; router = GetRouter();&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// This is fine (but still invoked dynamically)&lt;/span&gt;&lt;br /&gt;router.Send(&lt;span class="String"&gt;&amp;quot;message&amp;quot;&lt;/span&gt;, &lt;span class="String"&gt;&amp;quot;skeet@pobox.com&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;span class="InlineComment"&gt;// Compilation error: no such overload&lt;/span&gt;&lt;br /&gt;router.Send(&lt;span class="String"&gt;&amp;quot;message&amp;quot;&lt;/span&gt;, &lt;span class="String"&gt;&amp;quot;skeet@pobox.com&amp;quot;&lt;/span&gt;, 20); &lt;/div&gt; &lt;p&gt;Intellisense would work, and we&amp;#39;d still have some of the benefits of static typing but without the implementations having to know about your interface. Of course, it would be quite easy to create an implementation of the interface which did exactly this - but now imagine that instead of &lt;code&gt;IMessageRouter&lt;/code&gt; we had &lt;code&gt;MessageRouter&lt;/code&gt; - a concrete class. In this case the compiler would still restrict the caller to the public API of the class, but it wouldn&amp;#39;t have to be the real class. No checking would be performed by the compiler that your dynamic type &lt;i&gt;actually&lt;/i&gt; supported the operations - given that we&amp;#39;re talking about dynamic invocation, that would be impossible to do. It would instead be an &amp;quot;opt-in&amp;quot; restriction the client places on themselves. It could also potentially help with performance - if the binding involved realised that the &lt;i&gt;actual&lt;/i&gt; type of the dynamic object natively implemented the interface or was/derived from the class, then no real dynamic calls need be made; just route all directly. &lt;/p&gt; &lt;p&gt;This may all sound a bit fuzzy - I&amp;#39;m extremely sleepy, to be honest - but I think it&amp;#39;s a potentially interesting idea. Thoughts? &lt;/p&gt; &lt;h2&gt;Update&lt;/h2&gt;Apparently this post wasn&amp;#39;t as clear as it might be. I&amp;#39;m quite happy to keep the currently proposed dynamic type idea &lt;em&gt;as well&lt;/em&gt; - I&amp;#39;d like this as an &lt;em&gt;additional&lt;/em&gt; way of using dynamic objects.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652571" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Wacky+Ideas/default.aspx">Wacky Ideas</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>DeveloperDeveloperDeveloper: Registration now open (hurry!)</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/22/developerdeveloperdeveloper-registration-now-open-hurry.aspx</link><pubDate>Wed, 22 Oct 2008 09:53:34 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1651598</guid><dc:creator>skeet</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1651598</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1651598</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/10/22/developerdeveloperdeveloper-registration-now-open-hurry.aspx#comments</comments><description>&lt;p&gt;The &lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032393874&amp;amp;Culture=en-GB"&gt;registration page for DeveloperDeveloperDeveloper Day 2008&lt;/a&gt; (Reading, November 22nd) is now open. In the past this has been heavily oversubscribed, so if you want to come you&amp;#39;ll need to register quickly.&lt;/p&gt; &lt;p&gt;I&amp;#39;ve got a speaking slot in the afternoon: implementing LINQ to Objects in 60 minutes. As always, I&amp;#39;m very much looking forward to it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1651598" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>Mapping from a type to an instance of that type</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/08/mapping-from-a-type-to-an-instance-of-that-type.aspx</link><pubDate>Wed, 08 Oct 2008 20:24:09 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650188</guid><dc:creator>skeet</dc:creator><slash:comments>19</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1650188</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1650188</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/10/08/mapping-from-a-type-to-an-instance-of-that-type.aspx#comments</comments><description>&lt;p&gt;A &lt;a href="http://stackoverflow.com/questions/178255/serviceprovider-cache-etc-done-with-generics-without-cast"&gt;question&lt;/a&gt; came up on Stack Overflow yesterday which I&amp;#39;ve had to deal with myself before now. There are times when it&amp;#39;s helpful to have one value per type, and that value should be an instance of that type. To express it in pseudo-code, you want an &lt;code&gt;IDictionary&amp;lt;typeof(T), T&amp;gt;&lt;/code&gt; except with T varying across all possible types. Indeed, this came up in Protocol Buffers at least once, I believe.&lt;/p&gt; &lt;p&gt;.NET generics don&amp;#39;t have any way of expressing this, and you end up with boxing and a cast. I decided to encapsulate this (in &lt;a href="http://pobox.com/~skeet/csharp/miscutil"&gt;MiscUtil&lt;/a&gt; of course, although it&amp;#39;s not in a released version yet) so that I could have all the nastiness in a single place, leaving the client code relatively clean. The client code makes calls to generic methods which either take an instance of the type argument or return one. It&amp;#39;s a really simple class, but a potentially useful one:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="XmlComment"&gt;/// Map from types to instances of those types, e.g. int to 10 and&lt;/span&gt;&lt;br /&gt;&lt;span class="XmlComment"&gt;/// string to &amp;quot;hi&amp;quot; within the same dictionary. This cannot be done&lt;/span&gt;&lt;br /&gt;&lt;span class="XmlComment"&gt;/// without casting (and boxing for value types) as .NET cannot&lt;/span&gt;&lt;br /&gt;&lt;span class="XmlComment"&gt;/// represent this relationship with generics in their current form.&lt;/span&gt;&lt;br /&gt;&lt;span class="XmlComment"&gt;/// This class encapsulates the nastiness in a single place.&lt;/span&gt;&lt;br /&gt;&lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; DictionaryByType&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; IDictionary&amp;lt;Type, &lt;span class="ReferenceType"&gt;object&lt;/span&gt;&amp;gt; dictionary = &lt;span class="Keyword"&gt;new&lt;/span&gt; Dictionary&amp;lt;Type, &lt;span class="ReferenceType"&gt;object&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// Maps the specified type argument to the given value. If&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// the type argument already has a value within the dictionary,&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// ArgumentException is thrown.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Add&amp;lt;T&amp;gt;(T value)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dictionary.Add(&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(T), value);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// Maps the specified type argument to the given value. If&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// the type argument already has a value within the dictionary, it&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// is overwritten.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Put&amp;lt;T&amp;gt;(T value)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dictionary[&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(T)] = value;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// Attempts to fetch a value from the dictionary, throwing a&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// KeyNotFoundException if the specified type argument has no&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// entry in the dictionary.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; T Get&amp;lt;T&amp;gt;()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; (T) dictionary[&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(T)];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// Attempts to fetch a value from the dictionary, returning false and&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// setting the output parameter to the default value for T if it&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// fails, or returning true and setting the output parameter to the&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// fetched value if it succeeds.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;bool&lt;/span&gt; TryGet&amp;lt;T&amp;gt;(&lt;span class="MethodParameter"&gt;out&lt;/span&gt; T value)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;object&lt;/span&gt; tmp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;if&lt;/span&gt; (dictionary.TryGetValue(&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(T), &lt;span class="MethodParameter"&gt;out&lt;/span&gt; tmp))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = (T) tmp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;true&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = &lt;span class="Modifier"&gt;default&lt;/span&gt;(T);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;false&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;It doesn&amp;#39;t implement any of the common collection interfaces, because it would have to do so in a way which exposed the nastiness. I&amp;#39;m tempted to make it implement &lt;code&gt;IEnumerable&amp;lt;KeyValuePair&amp;lt;Type, object&amp;gt;&amp;gt;&lt;/code&gt; but even that&amp;#39;s somewhat unpleasant and unlikely to be useful. Easy to add at a later date if necessary.&lt;/p&gt; &lt;p&gt;(I know the XML documentation leaves something to be desired. One day I&amp;#39;ll learn how to really do it properly - currently I fumble around if I&amp;#39;m trying to refer to other types etc within the docs.)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650188" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Protocol+Buffers/default.aspx">Protocol Buffers</category></item><item><title>Why boxing doesn't keep me awake at nights</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/08/why-boxing-doesn-t-keep-me-awake-at-nights.aspx</link><pubDate>Wed, 08 Oct 2008 19:42:02 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650177</guid><dc:creator>skeet</dc:creator><slash:comments>26</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1650177</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1650177</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/10/08/why-boxing-doesn-t-keep-me-awake-at-nights.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;m currently reading the (generally excellent) &lt;a href="http://www.amazon.com/CLR-via-Second-Pro-Developer/dp/0735621632"&gt;CLR via C#&lt;/a&gt;, and I&amp;#39;ve recently hit the section on boxing. Why is it that authors feel they have to scaremonger about the effects boxing can have on performance?&lt;/p&gt; &lt;p&gt;Here&amp;#39;s a piece of code from the book:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;sealed&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Program {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Int32 v = 5;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Create an unboxed value type variable.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="PreProcessorDirective"&gt;#if INEFFICIENT&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// When compiling the following line, v is boxed&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// three times, wasting time and memory&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;{0}, {1}, {2}&amp;quot;&lt;/span&gt;, v, v, v);&lt;br /&gt;&lt;span class="PreProcessorDirective"&gt;#else&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// The lines below have the same result, execute&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// much faster, and use less memory&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Object o = v;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// No boxing occurs to compile the following line.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;{0}, {1}, {2}&amp;quot;&lt;/span&gt;, o, o, o);&lt;br /&gt;&lt;span class="PreProcessorDirective"&gt;#endif&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;In the text afterwards, he reiterates the point: &lt;/p&gt; &lt;blockquote&gt;This second version executes &lt;i&gt;much&lt;/i&gt; faster and allocates less memory from the heap. &lt;/blockquote&gt; &lt;p&gt;This seemed like an overstatement to me, so I thought I&amp;#39;d try it out. Here&amp;#39;s my test application: &lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System.Diagnostics;&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Test&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;const&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;int&lt;/span&gt; Iterations = 10000000;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Stopwatch sw = Stopwatch.StartNew();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;for&lt;/span&gt; (&lt;span class="ValueType"&gt;int&lt;/span&gt; i=0; i &amp;lt; Iterations; i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&lt;span class="PreProcessorDirective"&gt;#if&lt;/span&gt; CONSOLE_WITH_BOXING&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;{0} {1} {2}&amp;quot;&lt;/span&gt;, i, i, i);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;span class="PreProcessorDirective"&gt;#elif&lt;/span&gt; CONSOLE_NO_BOXING&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;object&lt;/span&gt; o = i;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;{0} {1} {2}&amp;quot;&lt;/span&gt;, o, o, o);&lt;br /&gt;&lt;span class="PreProcessorDirective"&gt;#elif&lt;/span&gt; CONSOLE_STRINGS&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; s = i.ToString();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;{0} {1} {2}&amp;quot;&lt;/span&gt;, s, s, s);&lt;br /&gt;&lt;span class="PreProcessorDirective"&gt;#elif&lt;/span&gt; FORMAT_WITH_BOXING&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt;.Format(&lt;span class="String"&gt;&amp;quot;{0} {1} {2}&amp;quot;&lt;/span&gt;, i, i, i);&lt;br /&gt;&lt;span class="PreProcessorDirective"&gt;#elif&lt;/span&gt; FORMAT_NO_BOXING&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;object&lt;/span&gt; o = i;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt;.Format(&lt;span class="String"&gt;&amp;quot;{0} {1} {2}&amp;quot;&lt;/span&gt;, o, o, o);&lt;br /&gt;&lt;span class="PreProcessorDirective"&gt;#elif&lt;/span&gt; FORMAT_STRINGS&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; s = i.ToString();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt;.Format(&lt;span class="String"&gt;&amp;quot;{0} {1} {2}&amp;quot;&lt;/span&gt;, s, s, s);&lt;br /&gt;&lt;span class="PreProcessorDirective"&gt;#elif&lt;/span&gt; CONCAT_WITH_BOXING&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt;.Concat(i, &lt;span class="String"&gt;&amp;quot; &amp;quot;&lt;/span&gt;, i, &lt;span class="String"&gt;&amp;quot; &amp;quot;&lt;/span&gt;, i);&lt;br /&gt;&lt;span class="PreProcessorDirective"&gt;#elif&lt;/span&gt; CONCAT_NO_BOXING&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;object&lt;/span&gt; o = i;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt;.Concat(o, &lt;span class="String"&gt;&amp;quot; &amp;quot;&lt;/span&gt;, o, &lt;span class="String"&gt;&amp;quot; &amp;quot;&lt;/span&gt;, o);&lt;br /&gt;&lt;span class="PreProcessorDirective"&gt;#elif&lt;/span&gt; CONCAT_STRINGS&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; s = i.ToString();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt;.Concat(s, &lt;span class="String"&gt;&amp;quot; &amp;quot;&lt;/span&gt;, s, &lt;span class="String"&gt;&amp;quot; &amp;quot;&lt;/span&gt;, s);&lt;br /&gt;&lt;span class="PreProcessorDirective"&gt;#endif&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Stop();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.Error.WriteLine(&lt;span class="String"&gt;&amp;quot;{0}ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;I compiled the code with one symbol defined each time, with optimisations and without debug information, and ran it from a command line, writing to &lt;code&gt;nul&lt;/code&gt; (i.e. no disk or actual console activity). Here are the results: &lt;/p&gt; &lt;table&gt;  &lt;tr&gt; &lt;th&gt;Symbol&lt;/th&gt; &lt;th&gt;Results (ms)&lt;/th&gt; &lt;th&gt;Average (ms)&lt;/th&gt;&lt;/tr&gt;  &lt;tr&gt; &lt;td&gt;CONSOLE_WITH_BOXING&lt;/td&gt; &lt;td align="right"&gt;33054&lt;/td&gt; &lt;td align="right"&gt;33444&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;33898&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;33381&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;CONSOLE_NO_BOXING&lt;/td&gt; &lt;td align="right"&gt;34638&lt;/td&gt; &lt;td align="right"&gt;33451&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;32423&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;33294&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;CONSOLE_STRINGS&lt;/td&gt; &lt;td align="right"&gt;29259&lt;/td&gt; &lt;td align="right"&gt;28337&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;29071&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;26683&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;FORMAT_WITH_BOXING&lt;/td&gt; &lt;td align="right"&gt;17143&lt;/td&gt; &lt;td align="right"&gt;17210&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;18100&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;16389&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;FORMAT_NO_BOXING&lt;/td&gt; &lt;td align="right"&gt;15814&lt;/td&gt; &lt;td align="right"&gt;15657&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;15936&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;15222&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;FORMAT_STRINGS&lt;/td&gt; &lt;td align="right"&gt;9178&lt;/td&gt; &lt;td align="right"&gt;8999&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;9077&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;8742&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;CONCAT_WITH_BOXING&lt;/td&gt; &lt;td align="right"&gt;12056&lt;/td&gt; &lt;td align="right"&gt;12563&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;14304&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;11329&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;CONCAT_NO_BOXING&lt;/td&gt; &lt;td align="right"&gt;11949&lt;/td&gt; &lt;td align="right"&gt;12240&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;13145&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;11628&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;CONCAT_STRINGS&lt;/td&gt; &lt;td align="right"&gt;5833&lt;/td&gt; &lt;td align="right"&gt;5936&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;6263&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="right"&gt;5713&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt; &lt;p&gt;So, what do we learn from this? Well, a number of things:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;As ever, microbenchmarks like this are pretty variable. I tried to do this on a &amp;quot;quiet&amp;quot; machine, but as you can see the results varied quite a lot. (Over two seconds between best and worst for a particular configuration at times!)  &lt;li&gt;The difference due to boxing with the original code in the book is basically inside the &amp;quot;noise&amp;quot;  &lt;li&gt;The dominant factor of the statement is writing to the console, even when it&amp;#39;s not actually writing to anything real  &lt;li&gt;The next most important factor is whether we convert to string once or three times  &lt;li&gt;The next most important factor is whether we use String.Format or Concat  &lt;li&gt;The least important factor is boxing&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Now I don&amp;#39;t want anyone to misunderstand me - I agree that boxing is less efficient than not boxing, where there&amp;#39;s a choice. Sometimes (as here, in my view) the &amp;quot;more efficient&amp;quot; code is slightly less readable - and the efficiency benefit is often negligible compared with other factors. Exactly the same thing happened in &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/08/01/book-review-accelerated-c-2008-by-trey-nash.aspx"&gt;Accelerated C# 2008&lt;/a&gt;, where a call to &lt;code&gt;Math.Pow(x, 2)&lt;/code&gt; was the dominant factor in a program again designed to show the efficiency of avoiding boxing.&lt;/p&gt; &lt;p&gt;The performance scare of boxing is akin to that of &lt;a href="http://pobox.com/~skeet/csharp/exceptions2.html"&gt;exceptions&lt;/a&gt;, although I suppose it&amp;#39;s more likely that boxing could cause a real performance concern in an otherwise-well-designed program. It used to be a much more common issue, of course, before generics gave us collections which don&amp;#39;t require boxing/unboxing to add/fetch data.&lt;/p&gt; &lt;p&gt;In short: yes, boxing has a cost. But please look at it in context, and if you&amp;#39;re going to start making claims about how much faster code will run when it avoids boxing, at least provide an example where it actually contributes significantly to the overall execution cost.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650177" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Books/default.aspx">Books</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx</link><pubDate>Mon, 06 Oct 2008 20:51:08 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649917</guid><dc:creator>skeet</dc:creator><slash:comments>22</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1649917</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1649917</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#comments</comments><description>&lt;p&gt;I suspect this has been done several times before, but on my way home this evening I considered what would be needed for the reverse of nullable value types - non-nullable reference types. I&amp;#39;ve had a play with an initial implementation in &lt;a href="http://pobox.com/~skeet/csharp/miscutil"&gt;MiscUtil&lt;/a&gt; (not in the currently released version) and it basically boils down (after removing comments, equality etc) to this:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;struct&lt;/span&gt; NonNullable&amp;lt;T&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; T : &lt;span class="ReferenceType"&gt;class&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; T value;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; NonNullable(T value)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;if&lt;/span&gt; (value == &lt;span class="Keyword"&gt;null&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="String"&gt;&amp;quot;value&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Keyword"&gt;this&lt;/span&gt;.value = value;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; T Value&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;if&lt;/span&gt; (value == &lt;span class="Keyword"&gt;null&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt; NullReferenceException();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; value;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;implicit&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;operator&lt;/span&gt; NonNullable&amp;lt;T&amp;gt;(T value)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt; NonNullable&amp;lt;T&amp;gt;(value);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;implicit&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;operator&lt;/span&gt; T(NonNullable&amp;lt;T&amp;gt; wrapper)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; wrapper.Value;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;Currently I&amp;#39;ve got both conversion operators as implicit, which isn&amp;#39;t quite as safe/obvious as it is with nullable value types, but it does make life simpler. Obviously this is experimental code more than anything else, so I may change my mind later.&lt;/p&gt; &lt;p&gt;The simple use case is something like this (along with testing error cases):&lt;/p&gt; &lt;div class="code"&gt;[Test]&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Demo()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SampleMethod(&lt;span class="String"&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;); &lt;span class="InlineComment"&gt;// No problems&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;try&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SampleMethod(&lt;span class="Keyword"&gt;null&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.Fail(&lt;span class="String"&gt;&amp;quot;Expected exception&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;catch&lt;/span&gt; (ArgumentNullException)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Expected&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;try&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SampleMethod(&lt;span class="Keyword"&gt;new&lt;/span&gt; NonNullable&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.Fail(&lt;span class="String"&gt;&amp;quot;Expected exception&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;catch&lt;/span&gt; (NullReferenceException)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Expected&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; SampleMethod(NonNullable&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; text)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Shouldn&amp;#39;t get here with usual conversions, but could do&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// through default construction. The conversion to string&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// will throw an exception anyway, so we&amp;#39;re guaranteed&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// that foo is non-null afterwards.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; foo = text;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.IsNotNull(foo);&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;I don&amp;#39;t intend to use this within MiscUtil at the moment. Note that it&amp;#39;s a struct, which has a few implications:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;It has the same memory footprint as the normal reference type. No GC pressure and no extra dereferencing. This is basically the main reason for making it a struct.  &lt;li&gt;It ends up being boxed to a new object. Boo hiss - ideally the boxing conversion would box it to the wrapped reference, and you could &amp;quot;unbox&amp;quot; (not really unboxing, of course) from a non-null reference to an instance of the struct.  &lt;li&gt;It&amp;#39;s possible to create instances wrapping null references, by calling the parameterless constructor or using the default value of fields etc. This is very annoying. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The good news is that it&amp;#39;s self-documenting and easy to use - the conversions will happen where required, doing appropriate checking (reasonably cheaply). The bad news is the lack of support from either the CLR (boxing behaviour above) or language (I&amp;#39;d love to specify &lt;code&gt;string! text&lt;/code&gt; as the parameter in the sample method above.) I suspect that if Microsoft were to do this properly, they&amp;#39;d want to avoid all of the problems above - but I&amp;#39;m not sure how... &lt;/p&gt; &lt;p&gt;Anyway, just a little experiment, but an interesting one - any thoughts? &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649917" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>Formatting strings</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx</link><pubDate>Mon, 06 Oct 2008 10:45:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649885</guid><dc:creator>skeet</dc:creator><slash:comments>19</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1649885</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1649885</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/formatting-strings.aspx#comments</comments><description>&lt;p&gt;A while ago I wrote &lt;a href="http://pobox.com/~skeet/csharp/stringbuilder.html"&gt;an article about &lt;code&gt;StringBuilder&lt;/code&gt;&lt;/a&gt; and a reader mailed me to ask about the efficiency of using &lt;code&gt;String.Format&lt;/code&gt; instead. This reminded me of a bone I have to pick with the BCL.&lt;/p&gt;
&lt;p&gt;Whenever we make a call to &lt;code&gt;String.Format&lt;/code&gt;, it has to parse the format string. That doesn&amp;#39;t sound too bad, but string formatting can be used a heck of a lot - and the format is almost always hard-coded in some way. It may be loaded from a resource file instead of being embedded directly in the source code, but it&amp;#39;s not going to change after the application has started.&lt;/p&gt;
&lt;p&gt;I put together a very crude benchmark which joins two strings together, separating them with just a space. The test uses &lt;code&gt;String.Format&lt;/code&gt; first, and then concatenation. (I&amp;#39;ve tried it both ways round, however, and the results are the same.)&lt;/p&gt;
&lt;div class="code"&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System.Diagnostics;&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Test&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;const&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;int&lt;/span&gt; Iterations=10000000;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;const&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;int&lt;/span&gt; PieceSize=10;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; first = GenerateRandomString();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; second = GenerateRandomString();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ValueType"&gt;int&lt;/span&gt; total=0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Stopwatch sw = Stopwatch.StartNew();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;for&lt;/span&gt; (&lt;span class="ValueType"&gt;int&lt;/span&gt; i=0; i &amp;lt; Iterations; i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; x = String.Format(&lt;span class="String"&gt;&amp;quot;{0} {1}&amp;quot;&lt;/span&gt;, first, second);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; total += x.Length;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Stop();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Format: {0}&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GC.Collect();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw = Stopwatch.StartNew();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;for&lt;/span&gt; (&lt;span class="ValueType"&gt;int&lt;/span&gt; i=0; i &amp;lt; Iterations; i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Equivalent to first + &amp;quot; &amp;quot; + second&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; x = String.Concat(first, &lt;span class="String"&gt;&amp;quot; &amp;quot;&lt;/span&gt;, second);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; total += x.Length;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sw.Stop();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Concat: {0}&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;if&lt;/span&gt; (total != Iterations * 2 * (PieceSize*2 + 1))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Incorrect total: {0}&amp;quot;&lt;/span&gt;, total);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Random rng = &lt;span class="Keyword"&gt;new&lt;/span&gt; Random();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; GenerateRandomString()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ValueType"&gt;char&lt;/span&gt;[] ret = &lt;span class="Keyword"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;char&lt;/span&gt;[PieceSize];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;for&lt;/span&gt; (&lt;span class="ValueType"&gt;int&lt;/span&gt; j=0; j &amp;lt; ret.Length; j++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ret[j] = (&lt;span class="ValueType"&gt;char&lt;/span&gt;) (&amp;#39;A&amp;#39; + rng.Next(26));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;(ret);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt;
&lt;p&gt;And the results (on my very slow Eee)...&lt;/p&gt;
&lt;div class="output"&gt;
Format: 14807&lt;br /&gt;
Concat: 3567
&lt;/div&gt;
&lt;p&gt;As you can see, &lt;code&gt;Format&lt;/code&gt; takes significantly longer than &lt;code&gt;Concat&lt;/code&gt;. I strongly suspect that this is largely due to having to parse the format string on each iteration. That won&amp;#39;t be the whole of the cost - &lt;code&gt;String&lt;/code&gt; needs to examine the format specifier for each string as well, in case there&amp;#39;s padding, etc - but again that could potentially be optimised.&lt;/p&gt;
&lt;p&gt;I propose a &lt;code&gt;FormatString&lt;/code&gt; class with a pair of &lt;code&gt;Format&lt;/code&gt; methods, one of which takes a culture and one of which doesn&amp;#39;t. We could then hoist our format strings out of the code itself and make them &lt;code&gt;static readonly&lt;/code&gt; variables referring to format strings. I&amp;#39;m not saying it would do a &lt;i&gt;huge&lt;/i&gt; amount to aid performance, but it could shave off a little time here and there, as well as making it even more obvious what the format string is used for.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649885" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>Lessons learned from Protocol Buffers, part 4: static interfaces</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-4-static-interfaces.aspx</link><pubDate>Fri, 29 Aug 2008 18:50:32 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646206</guid><dc:creator>skeet</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1646206</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1646206</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-4-static-interfaces.aspx#comments</comments><description>&lt;p&gt;&lt;b&gt;Warning:&lt;/b&gt; During this entire post, I will use the word &lt;i&gt;static&lt;/i&gt; to mean &amp;quot;relating to a type instead of an instance&amp;quot;. This isn&amp;#39;t a &lt;a href="http://blogs.msdn.com/ericlippert/archive/tags/Static+Methods/default.aspx"&gt;strictly accurate use&lt;/a&gt; but I believe it&amp;#39;s what most developers actually think of when they hear the word.&lt;/p&gt; &lt;p&gt;A few members of the interfaces in Protocol Buffers have no logical reason to act on instances of their types. The message interface has members to return the message&amp;#39;s type descriptor (the PB equivalent of &lt;code&gt;System.Type&lt;/code&gt;), the default instance for the message type, and a builder for the message type. The builder interface copies the first two of these, and also has a method to create a builder for a particular field. None of these touch any instance data.&lt;/p&gt; &lt;p&gt;In most cases this doesn&amp;#39;t actually cause any difficulties - we usually have an instance available when we&amp;#39;re in the PB library code, and the generated types have static properties for the default instance and the type descriptor anyway. Even so, it feels messy to have interface members which rely only on the &lt;i&gt;type&lt;/i&gt; of the implementation and not on any of the actual &lt;i&gt;data&lt;/i&gt; of the instance.&lt;/p&gt; &lt;p&gt;I&amp;#39;ve wondered before now about the possibility of having static members in interfaces - usually when thinking about plug-in architectures - but there&amp;#39;s always been the problem of working out how to specify the type on which to call the members. Variables and other expressions usually refer to values rather than types, and &lt;code&gt;System.Type&lt;/code&gt; doesn&amp;#39;t help as it provides no compile-time knowledge of the type being referred to.&lt;/p&gt; &lt;p&gt;There&amp;#39;s one big exception to this, however: generic type parameters. I don&amp;#39;t know why it had never occurred to me before, but this is a great fit for the ability to safely call static methods on types which are unknown at compile-time. Furthermore, it could provide a great way of enforcing the presence of constructors with appropriate signatures, and even operators. Before I get too far ahead of myself, let&amp;#39;s tie the simple case to a concrete example.&lt;/p&gt; &lt;h3&gt;Creating builders from nothing&lt;/h3&gt; &lt;p&gt;In my &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-3-generic-type-relationships.aspx"&gt;previous post&lt;/a&gt; I gave an example of a method which ideally wanted to return a new message given a &lt;code&gt;CodedInputStream&lt;/code&gt; and an &lt;code&gt;ExtensionRegistry&lt;/code&gt; (both types within Protocol Buffers, the details of which are unimportant to this example). The current code looks like this:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl&amp;lt;TMessage2, TBuilder&amp;gt; (Func&amp;lt;TBuilder&amp;gt; builderBuilder,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodedInputStream input,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExtensionRegistry registry) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage2, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage2 : TMessage, IMessage&amp;lt;TMessage2, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder builder = builderBuilder();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;}&lt;/div&gt; &lt;p&gt;For the purposes of this discussion I&amp;#39;ll simplify it a little, making it a generic method in a non-generic type.&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl&amp;lt;TMessage, TBuilder&amp;gt; (Func&amp;lt;TBuilder&amp;gt; builderBuilder,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodedInputStream input,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExtensionRegistry registry) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder builder = builderBuilder();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;The first parameter is a function which will return us a builder. We can&amp;#39;t simply add a &lt;code&gt;new()&lt;/code&gt; constraint to &lt;code&gt;TBuilder&lt;/code&gt; as not all geenrated builders will have a public constructor. However, we &lt;i&gt;do&lt;/i&gt; know that the &lt;code&gt;TMessage&lt;/code&gt; type has a &lt;code&gt;CreateBuilder()&lt;/code&gt; method because it implements &lt;code&gt;IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;/code&gt;. Unfortunately we don&amp;#39;t have an instance of &lt;code&gt;TMessage&lt;/code&gt; to call &lt;code&gt;CreateBuilder()&lt;/code&gt; on! Really, we&amp;#39;d like to be able to change the code to this: &lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl&amp;lt;TMessage, TBuilder&amp;gt; (CodedInputStream input, ExtensionRegistry registry) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder builder = &lt;b&gt;TMessage.CreateBuilder();&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;That&amp;#39;s currently impossible, but only because we can&amp;#39;t specify static methods in interfaces. Suppose we could write:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; TBuilder CreateBuilder();&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Other methods as before&lt;/span&gt;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Wouldn&amp;#39;t that be useful? The value would almost entirely be for generic types or methods where the type parameter is constrained to specify the relevant interface, but that could arguably still be very handy.  &lt;h3&gt;Operators and constructors&lt;/h3&gt; &lt;p&gt;At this point hopefully the idea I mentioned earlier of being able to specify operators and constructors is quite obvious. For instance, we could make all the existing numeric types implement &lt;code&gt;IArithmetic&amp;lt;T&amp;gt;&lt;/code&gt; (where &lt;code&gt;T&lt;/code&gt; was the same type, e.g. &lt;code&gt;int : IArithmetic&amp;lt;int&amp;gt;&lt;/code&gt;):&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IArithmetic&amp;lt;T&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; T &lt;span class="Keyword"&gt;operator&lt;/span&gt; +(T left, T right);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; T &lt;span class="Keyword"&gt;operator&lt;/span&gt; -(T left, T right);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; T &lt;span class="Keyword"&gt;operator&lt;/span&gt; /(T left, T right);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; T &lt;span class="Keyword"&gt;operator&lt;/span&gt; *(T left, T right);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// Used in LINQ to Objects, for example:&lt;/span&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; T Sum&amp;lt;T&amp;gt;(&lt;span class="Keyword"&gt;this&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; source) &lt;span class="Linq"&gt;where&lt;/span&gt; T : IArithmetic&amp;lt;T&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; T total = &lt;span class="Modifier"&gt;default&lt;/span&gt;(T);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;foreach&lt;/span&gt; (T element &lt;span class="Statement"&gt;in&lt;/span&gt; source)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; total += element; &lt;span class="InlineComment"&gt;// Interface says we can do total + element&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; total;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;Plug-ins for a particular program could implement &lt;code&gt;IPlugin&lt;/code&gt;:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IPlugin&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt; (PluginHost host);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Normal plug-in members here&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; Title { get; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// Used within a PluginHost like this...&lt;/span&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt; T CreatePlugin&amp;lt;T&amp;gt;() &lt;span class="Linq"&gt;where&lt;/span&gt; T : IPlugin&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; T plugin = &lt;span class="Keyword"&gt;new&lt;/span&gt; T(&lt;span class="Keyword"&gt;this&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; log.Info(&lt;span class="String"&gt;&amp;quot;Loaded plugin {0}&amp;quot;&lt;/span&gt;, plugin.Title);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; plugin;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;In fact, I&amp;#39;d imagine it would make sense to define a whole family of &lt;code&gt;IConstructable&lt;/code&gt; interfaces, along the same lines as the &lt;code&gt;Func&lt;/code&gt; and &lt;code&gt;Action&lt;/code&gt; delegate families:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IConstructable&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt;();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IConstructable&amp;lt;T&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt;(T arg)&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IConstructable&amp;lt;T1, T2&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt;(T1 arg1, T2 arg2);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// etc&lt;/span&gt; &lt;/div&gt; &lt;h3&gt;Inheritance raises its ugly head&lt;/h3&gt; &lt;p&gt;There&amp;#39;s a fly in the ointment here. Normally, if a base type implements an interface, that means a type derived from it will effectively implement the interface too. That ceases to hold in all cases. You can get away with it for straightforward static methods/properties, in the same way that people often write code such as &lt;code&gt;UTF8Encoding.UTF8&lt;/code&gt; when they really just mean &lt;code&gt;Encoding.UTF8&lt;/code&gt;. However, it doesn&amp;#39;t work for constructors - they aren&amp;#39;t inherited, so you can&amp;#39;t guarantee that &lt;code&gt;Banana&lt;/code&gt; has a parameterless constructor just because &lt;code&gt;Fruit&lt;/code&gt; does. &lt;/p&gt; &lt;p&gt;This is not only a problem for one concrete class deriving from another, but also for abstract implementations of interfaces. This happens a lot in Protocol Buffers; often the interface is partially implemented by the abstract class, with the final &amp;quot;leaf&amp;quot; classes in the inheritance tree implementing outstanding members and occasionally overriding earlier implementations for the sake of efficiency. Should we be able to specify an abstract static method in the abstract class, making sure that there&amp;#39;s an appropriate implementation by the time we hit a concrete class? As it happens that would be useful elsewhere in the Protocol Buffer library, but I&amp;#39;ll admit it&amp;#39;s slightly messy. I suspect there are ways round all of these issues, even if they might sometimes involve restricting the feature to particular, common use cases. However, every niggle would add its own piece of complexity. &lt;/p&gt; &lt;p&gt;There may well be other issues which would prove challenging - and other interesting aspects such as what explicit interface implementation would mean, if anything, in the context of static members. Language experts may well be able to reel off great lists of problems - I&amp;#39;d be very interested to here the ensuing discussions. &lt;/p&gt; &lt;h3&gt;Conclusion&lt;/h3&gt; &lt;p&gt;I believe that static interface members could prove very useful in generic algorithms, particularly if operators and constructors were allowed as well as the existing member types available in interfaces. There are significant sticking points to be carefully considered, and I wouldn&amp;#39;t like to prejudge the outcome of such deliberations in terms of whether the feature would be useful enough to merit the additional language complexity involved. It feels odd to implement an interface member which is effectively only of use when the implementing type is being used as the type argument for a generic type or method, but as developers learn to think more generically that may be less of a restriction than it currently seems. &lt;/p&gt; &lt;p&gt;This post is the last in my current batch talking about Protocol Buffers. There may well be more, but it&amp;#39;s unlikely now that the Protocol Buffer port is almost entirely complete. Most of these posts have involved generics, and the current limitations of what C# allows us to express in them. I do not intend to give the impression that I&amp;#39;m dissatisfied with C# - I&amp;#39;ve just found it interesting to take a look at what lies beyond the current boundaries of the language. The one aspect of these posts which I &lt;i&gt;would&lt;/i&gt; definitely like to see addresses is that of covariant return types - the Java implementation of Protocol Buffers is significantly simpler in many ways &lt;i&gt;purely&lt;/i&gt; due to this one point.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646206" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Google/default.aspx">Google</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Protocol+Buffers/default.aspx">Protocol Buffers</category></item><item><title>Lessons learned from Protocol Buffers, part 3: generic type relationships</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-3-generic-type-relationships.aspx</link><pubDate>Fri, 29 Aug 2008 18:48:48 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646204</guid><dc:creator>skeet</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1646204</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1646204</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-3-generic-type-relationships.aspx#comments</comments><description>&lt;p&gt;In &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/08/20/lessons-learned-from-protocol-buffers-part-2-self-referential-generic-types.aspx"&gt;part 2&lt;/a&gt; of this series we saw how the message and builder interfaces were self-referential in order to allow the implementation types to be part of the API. That&amp;#39;s one sort of relationship, but in this post we&amp;#39;ll see how the two interfaces relate to each other. If you remember from &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/08/20/lessons-learned-from-protocol-buffers-part-1-messages-builders-and-immutability.aspx"&gt;part 1&lt;/a&gt; every generated message type has a corresponding builder type. As it happens, this is implemented with a nested type, so if you had a &lt;code&gt;Person&lt;/code&gt; message, the generated types would be &lt;code&gt;Person&lt;/code&gt; and &lt;code&gt;Person.Builder&lt;/code&gt; (in a specified namespace, of course).&lt;/p&gt; &lt;p&gt;Without any interfaces involved, this would be very simple. The types would just look like this (with more members, of course):&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Person&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; Builder CreateBuilder() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Builder CreateBuilderForType() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Builder&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Builder() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Person Build() { ... }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;You may well be wondering why there are two methods for creating a builder. The static method is convenient for code which knows it&amp;#39;s dealing with the &lt;code&gt;Person&lt;/code&gt; message. The instance method ends up being part of the message interface, which makes it useful for code which can work with any message. In addition, the constructor for &lt;code&gt;Person.Builder&lt;/code&gt; is accessible in the C# version. In the original Java code the only way of creating a builder is via the methods in the message class; I decided to remove this restriction for the sake of making the oh-so-readable object initializer syntax available in C# 3.&lt;/p&gt; &lt;h3&gt;Redesigning the interfaces to refer to each other&lt;/h3&gt; &lt;p&gt;In part 2 we created self-referential interfaces for the message and builder interfaces which looked like this:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IBuilder&amp;lt;TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;The constraints on the type parameters allow us to make the API very specific, and we can use the same trick again when we relate the builder and message types together. The step where we introduce a new type parameter to each of them is straightforward:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage, TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IBuilder&amp;lt;TMessage, TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Unfortunately without any restrictions on the &amp;quot;foreign&amp;quot; type parameter in each interface, we don&amp;#39;t get enough information to make everything work. We need to tie the two types together more tightly, like this:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;}&amp;nbsp; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;To make this concrete for &lt;code&gt;Person&lt;/code&gt; and &lt;code&gt;Person.Builder&lt;/code&gt; we end up with implementations like this:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Person : IMessage&amp;lt;Person, Builder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; Builder CreateBuilder() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Builder CreateBuilderForType() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Builder : IBuilder&amp;lt;Person, Builder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Builder() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Person Build() { ... }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;This works, but it&amp;#39;s really ugly. Any generic methods wanting to take a &lt;code&gt;TMessage&lt;/code&gt; type parameter implementing &lt;code&gt;IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;/code&gt; have to also have a &lt;code&gt;TBuilder&lt;/code&gt; type parameter, and the two constraints need to be expressed each time. It&amp;#39;s a real pain. In fact, I&amp;#39;ve got an &lt;code&gt;IMessage&amp;lt;TMessage&amp;gt;&lt;/code&gt; interface which contains almost nothing in it (and which the more generic interface extends). This allows me to get hold of the message type (and use it in the API), inferring the builder type by reflection. That&amp;#39;s a pain too, frankly. It&amp;#39;s a particular nuisance because when I &lt;i&gt;do&lt;/i&gt; infer the builder type, I haven&amp;#39;t actually got any compile-time constraint the lets any other code know that it&amp;#39;s the right builder type for the message type. In one specific case it&amp;#39;s led to this horrific method (in a type generic in &lt;code&gt;TMessage&lt;/code&gt;:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl&amp;lt;TMessage2, TBuilder&amp;gt; (Func&amp;lt;TBuilder&amp;gt; builderBuilder,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodedInputStream input,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExtensionRegistry registry) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage2, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage2 : TMessage, IMessage&amp;lt;TMessage2, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder builder = builderBuilder();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;}&lt;/div&gt; &lt;p&gt;Fortunately this is hidden from public view - and the only reason to do it at all is to enable a pleasant API of &lt;code&gt;MessageStreamIterator&amp;lt;TMessage&amp;gt; : IEnumerable&amp;lt;TMessage&amp;gt; where TMessage : IMessage&amp;lt;TMessage&amp;gt;&lt;/code&gt;. The result of the evil method above is exactly what the caller is likely to want, otherwise I wouldn&amp;#39;t put up with it. However, that sort of excuse has been coming up far too much in the PB implementation, so I&amp;#39;ve had a quick think about what could be done about it.&lt;/p&gt; &lt;h3&gt;Contemplating a more expressive language&lt;/h3&gt; &lt;p&gt;I should really prefix this section by saying that I&amp;#39;m not actually &lt;em&gt;suggesting&lt;/em&gt; this as a way forward for C# or .NET. (I suspect it would take more work in the CLR as well as just in the language; I don&amp;#39;t know enough about CLR generics to say for sure, but I&amp;#39;d be surprised if this were feasible.) I haven&amp;#39;t encountered many situations where I&amp;#39;ve wanted anything like this, and the extra complexity in the language would be quite high, I suspect. Suppose an interface could contain extra type parameters, including constraints, in the body of the interface:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Attention"&gt;// Purely imaginary syntax!&lt;/span&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TBuilder&amp;gt;, TBuilder.TMessage : TMessage&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Normal methods, which could use TBuilder&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IBuilder&amp;lt;TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;TMessage&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage&amp;gt;, TMessage.TBuilder : TBuilder&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Normal methods, which could use TMessage&lt;/span&gt;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;There are various ways in which the interface implementation could indicate the type of TBuilder. The syntax itself isn&amp;#39;t particularly interesting - it&amp;#39;s the extra information which is conveyed which is the important bit. I&amp;#39;ve dithered between this being a step forward and it not. At first glance it looks no better than having both type parameters in the interface declaration, but I believe it would genuinely make a difference. For instance, the above evil method could be written as:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl(Func&amp;lt;TMessage.TBuilder&amp;gt; builderBuilder,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodedInputStream input,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExtensionRegistry registry) &lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TMessage.TBuilder builder = builderBuilder();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;}&amp;nbsp; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;This time there&amp;#39;s no need for the method to be generic, because the type is already generic in the message type. Furthermore, we can &lt;i&gt;call&lt;/i&gt; this method with no reflection. All other APIs which have previously had to be specify two type parameters can now just specify the one. Apart from anything else, this leaves more scope for type inference in generic methods - passing &lt;i&gt;either&lt;/i&gt; a message &lt;i&gt;or&lt;/i&gt; a builder to a generic method happens occasionally, but it&amp;#39;s very rare to pass in both.&lt;/p&gt; &lt;p&gt;We&amp;#39;ve essentially expressed the relationship between the message type and the builder type a little more explicitly, so that we can guarantee it exists (and use it) at compile time. That&amp;#39;s at the heart of the problem to start with - without a second type parameter in the initial interface declaration, in the current language there&amp;#39;s no way of expressing a close relationship with another type. &lt;/p&gt; &lt;h3&gt;Conclusion&lt;/h3&gt; &lt;p&gt;I don&amp;#39;t think it would be fair to say that C# really lets us down here - it happens not to support a pretty rare scenario, and that&amp;#39;s fair enough. I&amp;#39;d be interested to know whether any other languages allow the same sort of concepts to be expressed more pleasantly. The ugly solution I&amp;#39;ve presented here does at least work, and it&amp;#39;s nearly invisible to most users, who are likely to just reference the concrete generated types. I&amp;#39;m not happy with the verbosity which has become necessary in many places, but it&amp;#39;s in a good cause. It&amp;#39;s interesting to note that the Java API doesn&amp;#39;t use this sort of doubly-generic relationship: again, covariant return types allow the concrete message and builder types to express their APIs directly and still implement a more general interface at the same time.&lt;/p&gt; &lt;p&gt;In the next part I&amp;#39;ll look at another possibility which would make interfaces and generics a more powerful combination: static interface methods.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646204" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Google/default.aspx">Google</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Protocol+Buffers/default.aspx">Protocol Buffers</category></item><item><title>Lessons learned from Protocol Buffers, part 2: self-referential generic types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/20/lessons-learned-from-protocol-buffers-part-2-self-referential-generic-types.aspx</link><pubDate>Wed, 20 Aug 2008 20:37:27 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1645269</guid><dc:creator>skeet</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1645269</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1645269</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/08/20/lessons-learned-from-protocol-buffers-part-2-self-referential-generic-types.aspx#comments</comments><description>&lt;p&gt;In the &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/08/20/lessons-learned-from-protocol-buffers-part-1-messages-builders-and-immutability.aspx"&gt;first part&lt;/a&gt; of this series we saw that a message type and its builder are closely related. The tricky bit comes when we want to define an interface describing messages and builders. Although some members clearly depend on the data being built (the first and last name in the person example above, for instance) others apply to &lt;em&gt;all&lt;/em&gt; messages or &lt;em&gt;all&lt;/em&gt; builders. For instance, a message can always provide you with a suitable builder, and a builder always allows you to build it to create the actual message. Likewise the message and builder types also have methods which return other instances of themselves - you can ask any message for the default message of the same type, or clone a builder. Many common builder methods effectively return &lt;code&gt;this&lt;/code&gt; (i.e. the same builder) - but the declared return type needs to be the concrete type involved, not just the interface, otherwise you couldn&amp;#39;t then use the returned builder to set properties without casting.&lt;/p&gt; &lt;p&gt;(Aside: some of the members of the common interface would be more pleasant if they could be declared statically. We&amp;#39;ll look at that later in the series.)&lt;/p&gt; &lt;p&gt;We have two slightly different issues here: defining the interface to allow members to return the concrete types, and tying builders and messages together. This post will just talk about the first of these issues. Enjoy the luxury of only having to think about one type parameter at a time - it won&amp;#39;t last long.&lt;/p&gt; &lt;h3&gt;First encounters of the self-referential kind&lt;/h3&gt; &lt;p&gt;I first came across a generic constraint which referred to itself back in the early days of Java 5. Here&amp;#39;s the declaration for &lt;code&gt;java.lang.Enum&lt;/code&gt;:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;abstract&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Enum&amp;lt;E &lt;span class="Modifier"&gt;extends&lt;/span&gt; Enum&amp;lt;E&amp;gt;&amp;gt;&lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Assuming you&amp;#39;re more comfortable in C#, I&amp;#39;ll translate that into C# syntax: &lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;abstract&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Enum&amp;lt;T&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; T : Enum&amp;lt;T&amp;gt; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;The constraint is easier read than understood. Any concrete, constructed class deriving from this will be an &amp;quot;enum of &lt;em&gt;something&lt;/em&gt;&amp;quot; where &lt;em&gt;something&lt;/em&gt; itself an &amp;quot;enum of &lt;em&gt;something&lt;/em&gt;&amp;quot;.&lt;/p&gt; &lt;p&gt;Now, Java puts additional restrictions on the &lt;code&gt;Enum&lt;/code&gt; class (it&amp;#39;s like &lt;code&gt;System.Delegate&lt;/code&gt; in C# - you can&amp;#39;t explicitly derive from it yourself; you have to let the compiler do it for you). However, the syntax is perfectly valid in &amp;quot;normal&amp;quot; code. Typically when you encounter this kind of type constraint, you satisfy it in new classes by using the same class as the type argument for &lt;code&gt;T&lt;/code&gt;. So, in the &lt;code&gt;Enum&lt;/code&gt; example we might have:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;sealed&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Currency : Enum&amp;lt;Currency&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Code&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;sealed&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Status : Enum&amp;lt;Status&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Code&lt;/span&gt;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;There&amp;#39;s nothing to actually &lt;i&gt;stop&lt;/i&gt; you from declaring &lt;code&gt;class Status : Enum&amp;lt;Currency&amp;gt;&lt;/code&gt; - it&amp;#39;s not just not normally useful. Likewise you &lt;em&gt;can&lt;/em&gt; leave the derived type as a generic one, but again that&amp;#39;s atypical. I don&amp;#39;t know any way to enforce the usual implementation - short of building into the language, as Java did - but it&amp;#39;s generally not a problem.&lt;/p&gt; &lt;h3&gt;Back to Protocol Buffers&lt;/h3&gt; &lt;p&gt;So why is this useful? Well, moving on from enums let&amp;#39;s look at the builder interface in Protocol Buffers. Here&amp;#39;s part of it - somewhat simplified, admittedly:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IBuilder&amp;lt;TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder Clear();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder Clone();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder ClearField(FieldDescriptor field);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder AddRepeatedField(FieldDescriptor field, &lt;span class="ReferenceType"&gt;object&lt;/span&gt; value);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder SetUnknownFields(UnknownFieldSet unknownFields);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder MergeUnknownFields(UnknownFieldSet unknownFields);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder MergeFrom(ByteString data);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder MergeFrom(CodedInputStream input);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder MergeFrom(CodedInputStream input, ExtensionRegistry registry);&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;None of those methods mention the actual message directly - for that we need another type parameter, as we&amp;#39;ll see in the next post - but all of them return a &lt;code&gt;TBuilder&lt;/code&gt;. As it happens, the interface documentation requires that all the methods return the same reference back, just as &lt;code&gt;StringBuilder&lt;/code&gt; methods do, but you could equally create an interface around immutable types, expecting each operation to return a new value. For instance, you could create an &lt;code&gt;IArithmetic&amp;lt;T&amp;gt;&lt;/code&gt; interface such that &lt;code&gt;int&lt;/code&gt; could implement &lt;code&gt;IArithmetic&amp;lt;int&amp;gt;&lt;/code&gt;, &lt;code&gt;double&lt;/code&gt; could implement &lt;code&gt;IArithmetic&amp;lt;double&amp;gt;&lt;/code&gt; etc. You can then chain multiple operations together, e.g. 5.Add(10).Multiply(2) and know that you&amp;#39;re always within the world of integers. &lt;/p&gt; &lt;p&gt;It&amp;#39;s important to note that the return type of each of the methods in our builder interface is &lt;code&gt;TBuilder&lt;/code&gt;, not &lt;code&gt;IBuilder&amp;lt;TBuilder&amp;gt;&lt;/code&gt;. I point this out mostly because the latter is what I originally had. After all, it&amp;#39;s often best to expose fairly general return types. That works fine while you&amp;#39;re only using operations within the interface, but often clients know more detail about the concrete type and want to use that information. For instance, you might want to be able to write: &lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;Person.Builder builder = ...; &lt;span class="InlineComment"&gt;// Get a builder from somewhere&lt;/span&gt;&lt;br /&gt;builder = builder.Clear()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .SetFirstName(&lt;span class="String"&gt;&amp;quot;Fred&amp;quot;&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .SetLastName(&lt;span class="String"&gt;&amp;quot;Jones&amp;quot;&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Clone(); &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Here &lt;code&gt;SetFirstName()&lt;/code&gt; and &lt;code&gt;SetLastName()&lt;/code&gt; aren&amp;#39;t members of the interface, but &lt;code&gt;Clear()&lt;/code&gt; and &lt;code&gt;Clone()&lt;/code&gt; are. We can mix and match like this (and finally reassign the &lt;code&gt;builder&lt;/code&gt; variable) because the interface is as strongly typed as it is. Code which only knows about the interface can still do whatever it likes, because it knows that &lt;code&gt;TBuilder&lt;/code&gt; implements &lt;code&gt;IBuilder&amp;lt;TBuilder&amp;gt;&lt;/code&gt;. In particular, that means it&amp;#39;s fine for some of the interface to be implemented by an abstract class - in Protocol Buffers there can be quite a deep inheritance tree for messages and builders, and a lot of the methods (particularly the merging ones) can be written in terms of the others. (Yes, that suggests that an extension method might be appropriate - but leaving it in the interface allows for particular implementations to override the general one, which can be important for optimisation. There&amp;#39;s also the matter of making the whole thing play nicely for people who are still stuck with .NET 2.0 and Visual Studio 2005.)&lt;/p&gt; &lt;h3&gt;A small diversion via Java&lt;/h3&gt; &lt;p&gt;It&amp;#39;s interesting to note that while my C# port is larely a port of the Java code, there are significant differences around how generics are used. This is understandable given how different generics are in .NET and Java. (My preference being heavily towards the .NET side - but there are moments when Java has its advantages.) However, one aspect of Java which is used to great effect is covariant return types. In the Java protocol buffers, the &lt;code&gt;Message&lt;/code&gt; and &lt;code&gt;Builder&lt;/code&gt; interfaces aren&amp;#39;t generic at all. For instance, the equivalent of the earlier part of the builder interface is just this:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; Builder&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Builder clear();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Builder clone();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Builder clearField(FieldDescriptor field);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Builder addRepeatedField(FieldDescriptor field, &lt;span class="ReferenceType"&gt;object&lt;/span&gt; value);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Builder setUnknownFields(UnknownFieldSet unknownFields);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Builder mergeUnknownFields(UnknownFieldSet unknownFields);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Builder mergeFrom(ByteString data);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Builder mergeFrom(CodedInputStream input);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Builder mergeFrom(CodedInputStream input, ExtensionRegistry registry);&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Does that mean we can&amp;#39;t chain operations together any more, mixing and matching &amp;quot;concrete-type-specific&amp;quot; methods (such as the setters for first name and last name) with the interface methods? Not at all - because where &lt;code&gt;Person.Builder&lt;/code&gt; implements (say) &lt;code&gt;clear()&lt;/code&gt; it can do it like this: &lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;Person.Builder clear()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Implementation&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;this&lt;/span&gt;;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;At that point, everything which knows at compile-time that it&amp;#39;s calling &lt;code&gt;Person.Builder.clear()&lt;/code&gt; knows that it returns a &lt;code&gt;Person.Builder&lt;/code&gt; - whereas code which only knows that it&amp;#39;s calling the interface method only knows that it will return some implementation of the interface. (Apologies for the naming here - it&amp;#39;s unfortunate from a clarity standpoint that both the interface and the implementation is called &lt;code&gt;Builder&lt;/code&gt;, but I thought it would be worth being faithful to the real code on this point.)&lt;/p&gt; &lt;p&gt;It&amp;#39;s just about possible to do this in C# as well, with explicit interface implementation. Again, I went that way to start with - and it was a disaster. In the intermediate abstract classes I was having to cast to the interface sometimes, not cast at other times, declare new abstract protected methods of &lt;code&gt;ClearImpl&lt;/code&gt; etc. It was simply awful. I&amp;#39;ve gone back to my school of thought which is that explicit interface implementation is handy when it&amp;#39;s absolutely required (or where you &lt;i&gt;deliberately&lt;/i&gt; want to make it hard to call certain members), but should be largely avoided.&lt;/p&gt; &lt;p&gt;In fact, I do have a non-generic interface for both messages and builders, but where types would be involved I&amp;#39;ve renamed the methods to things like &lt;code&gt;WeakClear&lt;/code&gt; and &lt;code&gt;WeakBuild&lt;/code&gt;. These &lt;code&gt;Weak*&lt;/code&gt; methods are only defined in terms of the non-generic interfaces, and are mostly used in cases where we really don&amp;#39;t know at compile time what kind of message we&amp;#39;re dealing with, even in a generic sense. Life would, however, be &lt;em&gt;much&lt;/em&gt; simpler if only we had covariant return types in C#.&lt;/p&gt; &lt;h3&gt;Conclusion&lt;/h3&gt; &lt;p&gt;Self-referential generic types shouldn&amp;#39;t be used more widely than they really need to be - they can be tough to get your head round. However, they can be useful when you want to maintain a strongly typed API which needs to talk in terms of itself. One redeeming feature of the complexity in Protocol Buffers is that most of it is in the implementation: users of &lt;code&gt;Person&lt;/code&gt; and &lt;code&gt;Person.Builder&lt;/code&gt; really don&amp;#39;t need to know or care about the interfaces for most of the time. So long as they use a strongly typed expression to start with, they&amp;#39;ll keep that strong typing and be presented with appropriate members to call as if the interfaces and intermediate abstract classes didn&amp;#39;t even exist. It&amp;#39;s an API which gets out of your way when you&amp;#39;re not interested in it, which is always a nice sign.&lt;/p&gt; &lt;p&gt;While trying a number of schemes I&amp;#39;ve learned that there can often be a lot of subtly different options available, and their benefits and drawbacks aren&amp;#39;t always obvious until you try them. Oh, and covariant return types would be very welcome, and explicit interface implementation should generally be avoided where possible :)&lt;/p&gt; &lt;p&gt;Next time I&amp;#39;ll reveal a bit more about the &lt;em&gt;real&lt;/em&gt; interfaces in my PB port. Bear in mind that messages need to know about their builders, and vice versa...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1645269" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Protocol+Buffers/default.aspx">Protocol Buffers</category></item><item><title>Lessons learned from Protocol Buffers, part 1: messages, builders and immutability</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/20/lessons-learned-from-protocol-buffers-part-1-messages-builders-and-immutability.aspx</link><pubDate>Wed, 20 Aug 2008 16:31:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1645223</guid><dc:creator>skeet</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1645223</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1645223</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/08/20/lessons-learned-from-protocol-buffers-part-1-messages-builders-and-immutability.aspx#comments</comments><description>&lt;p&gt;My port of the &lt;a href="http://code.google.com/p/protobuf/"&gt;Protocol Buffers&lt;/a&gt; project has proved pretty interesting. I thought I&amp;#39;d share some of the lessons I&amp;#39;ve learned along the way, as well as some of the frustrations at concepts I still can&amp;#39;t express in C#. &lt;/p&gt; &lt;p&gt;This was originally all going to be in one post, but I&amp;#39;m becoming acutely aware of how long some posts can grow. I don&amp;#39;t know about you, but I find very long blog posts quite intimidating, so I&amp;#39;ve decided to split them up into individual topics. You&amp;#39;ll still probably need to read the posts in order to understand them though - and this introductory post is the most important one in that respect. &lt;/p&gt; &lt;h3&gt;Messages and Builders&lt;/h3&gt; &lt;p&gt;The Protocol Buffers project (or PB for short) is basically another serialization technology, putting emphasis on efficiency, platform neutrality, and backward/forward compatibility. The normal set of steps in using PB is something like this:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Write a &lt;code&gt;.proto&lt;/code&gt; file describing your data in terms of &lt;em&gt;messages.&lt;/em&gt;  &lt;li&gt;Run &lt;code&gt;protoc&lt;/code&gt; to generate C# (and Java/C++ if you so wish).  &lt;li&gt;In your application, use the &lt;em&gt;builder&lt;/em&gt; associated with the message type to create an instance of a message.  &lt;li&gt;Serialize the data to a stream.  &lt;li&gt;At some other point in the application (or a different app) deserialize the data.&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;The idea is that builders are mutable, while the messages they build are immutable. You can use builders either with Set* methods which return the same builder again, or properties which can be used within object initializers. For example:  &lt;p&gt; &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Syntax available in C# 2&lt;/span&gt;&lt;br /&gt;Person john = &lt;span class="Keyword"&gt;new&lt;/span&gt; Person.Builder()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .SetFirstName(&lt;span class="String"&gt;&amp;quot;John&amp;quot;&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .SetLastName(&lt;span class="String"&gt;&amp;quot;Doe&amp;quot;&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Build();&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// Using an object initializer&lt;/span&gt;&lt;br /&gt;Person jane = &lt;span class="Keyword"&gt;new&lt;/span&gt; Person.Builder &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { FirstName=&lt;span class="String"&gt;&amp;quot;Jane&amp;quot;&lt;/span&gt;, LastName=&lt;span class="String"&gt;&amp;quot;Doe&amp;quot;&lt;/span&gt; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Build(); &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Of course, you don&amp;#39;t have to do all the building in one expression, it&amp;#39;s just a handy option in many cases.&lt;/p&gt; &lt;p&gt;As you can see, the builder is generated as a nested type of the message. That&amp;#39;s handy, as it means the builder has access to the private members of the message. To avoid lots of data copying we employ &lt;a href="http://blogs.msdn.com/ericlippert/archive/2007/11/13/immutability-in-c-part-one-kinds-of-immutability.aspx"&gt;popsicle immutability&lt;/a&gt; - the builder directly manipulates the message until it&amp;#39;s built, at which point it makes sure that nothing will change it afterwards. If that makes you uncomfortable in terms of it not being &amp;quot;true&amp;quot; immutability, I sympathise - but I also give &lt;code&gt;String&lt;/code&gt; as a counterexample; &lt;code&gt;StringBuilder&lt;/code&gt; works in exactly this way, modifying a string directly until it exposes it to the outside world.&lt;/p&gt; &lt;p&gt;Other than the copying - and the fact that all the code exists explicitly, and the caller has to know about the builder - this is quite similar to the &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx"&gt;suggestion I made about C# immutability&lt;/a&gt; a while ago. One point which makes it all simpler is that every data type in Protocol Buffers is itself immutable - so we don&amp;#39;t need to worry about deep copies and the like.&lt;/p&gt; &lt;p&gt;Unfortunately the current implementation doesn&amp;#39;t support collection initializers - if you have a repeated field in your message, you have to call &lt;code&gt;Add*&lt;/code&gt; to populate it. The &lt;code&gt;Add*&lt;/code&gt; methods return the builder just like the &lt;code&gt;Set*&lt;/code&gt; methods, so you can still do it all in one expression, but it&amp;#39;s not terribly neat. Using a collection initializer compiles, but fails at execution time because the properties for repeated fields always return immutable lists. This is by design, to stop callers from creating a builder, fetching the list property, calling &lt;code&gt;Build&lt;/code&gt; and then adding to the list. A better solution (and one which I plan to implement soon) is to have a &lt;code&gt;PopsicleList&amp;lt;T&amp;gt;&lt;/code&gt; which is initially mutable but which will become immutable at the appropriate time (i.e. when &lt;code&gt;Build()&lt;/code&gt; is called). At that point we&amp;#39;ll be able to write: &lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;Person jane = &lt;span class="Keyword"&gt;new&lt;/span&gt; Person.Builder &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { FirstName=&lt;span class="String"&gt;&amp;quot;Jane&amp;quot;&lt;/span&gt;, LastName=&lt;span class="String"&gt;&amp;quot;Doe&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Friends = { &lt;span class="String"&gt;&amp;quot;Tom&amp;quot;&lt;/span&gt;, &lt;span class="String"&gt;&amp;quot;&amp;quot;&lt;/span&gt;, &lt;span class="String"&gt;&amp;quot;Harry&amp;quot;&lt;/span&gt; } }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Build();&amp;nbsp; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;There&amp;#39;s quite a lot more to messages and builders than this - things like the reflection-like API to query properties of the message based on fields in the the message descriptor - but what I&amp;#39;ve described so far ought to be enough for most of what I want to talk about, most of which relates to generics. In the next part, I&amp;#39;ll talk about self-referential generic types.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1645223" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Protocol+Buffers/default.aspx">Protocol Buffers</category></item></channel></rss>