<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Jon Skeet: Coding Blog</title><link>http://msmvps.com/blogs/jon_skeet/default.aspx</link><description>C#, .NET, Java, software development etc
**This is my personal blog. The views expressed on these pages are mine alone and not those of my employer.**</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/JonSkeetCodingBlog" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item><title>Where do you benefit from dynamic typing?</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/-skiprAdosg/where-do-you-benefit-from-dynamic-typing.aspx</link><pubDate>Tue, 17 Nov 2009 07:31:48 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1740168</guid><dc:creator>skeet</dc:creator><slash:comments>40</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1740168</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1740168</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/11/17/where-do-you-benefit-from-dynamic-typing.aspx#comments</comments><description>&lt;p&gt;Disclaimer: I don&amp;#39;t want this to become a flame war in the comments. I&amp;#39;m coming from a position of ignorance, and well aware of it. While I&amp;#39;d like this post to provoke &lt;em&gt;thought&lt;/em&gt;, it&amp;#39;s not meant to be provocative in the common use of the term.&lt;/p&gt;  &lt;p&gt;Chapter 14 of C# in Depth is about dynamic typing in C#. A couple of reviewers have justifiably said that I&amp;#39;m fairly keen on the mantra of &amp;quot;don&amp;#39;t use dynamic typing unless you need it&amp;quot; – and that possibly I&amp;#39;m doing dynamic typing a disservice by not pointing out more of its positive aspects. I completely agree, and I&amp;#39;d love to be more positive – but the problem is that I&amp;#39;m not (yet) convinced about why dynamic typing is something I &lt;em&gt;would&lt;/em&gt; want to embrace.&lt;/p&gt;  &lt;p&gt;Now I want to start off by making something clear: this is meant to be about dynamic typing. Often advocates for dynamically typed languages will mention:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Read-eval-print_loop"&gt;REPL&lt;/a&gt; (read-eval-print-loop) abilities which allow for a very fast feedback loop while experimenting&lt;/li&gt;    &lt;li&gt;Terseness – the lack of type names everywhere makes code shorter&lt;/li&gt;    &lt;li&gt;Code evaluated at execution time (so config files can be scripts etc)&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I don&amp;#39;t count any of these as benefits of dynamic typing per se. They&amp;#39;re benefits which often come alongside dynamic typing, but they&amp;#39;re not dependent on dynamic typing. The terseness argument is the one most closely tied to their dynamic nature, but various languages with powerful type inference show that being statically typed doesn&amp;#39;t mean having to specify type names everywhere. (C#&amp;#39;s &lt;em&gt;var&lt;/em&gt; keyword is a very restricted form of type inference, compared with – say – that of F#.)&lt;/p&gt;  &lt;p&gt;What I&amp;#39;m talking about is binding being performed at execution time and &lt;em&gt;only&lt;/em&gt; at execution time. That allows for:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Duck typing&lt;/li&gt;    &lt;li&gt;Dynamic reaction to previously undeclared messages&lt;/li&gt;    &lt;li&gt;Other parts of dynamic typing I&amp;#39;m unaware of (how could there not be any?)&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;What I&amp;#39;m interested in is how often these are used within &lt;em&gt;real world&lt;/em&gt; (rather than demonstration) code. It may well be that I&amp;#39;m suffering from &lt;a href="http://c2.com/cgi/wiki?BlubParadox"&gt;Blub&amp;#39;s paradox&lt;/a&gt; – that I can&amp;#39;t see the valid uses of these features simply because I haven&amp;#39;t used them enough. Just to be clear, I&amp;#39;m not saying that I &lt;em&gt;never&lt;/em&gt; encounter problems where I would welcome dynamic typing – but I don&amp;#39;t run into them every day, whereas I get help from the compiler every day.&lt;/p&gt;  &lt;p&gt;Just as an indicator of how set in my statically typed ways I am, at the recent Stack Overflow DevDays event in London, Michael Sparks went through &lt;a href="http://norvig.com/spell-correct.html"&gt;Peter Norvig&amp;#39;s spelling corrector&lt;/a&gt;. It&amp;#39;s a neat piece of code (and yes, I&amp;#39;ll finish that port some time) but I kept finding it hard to understand simply because the types weren&amp;#39;t spelled out. Terseness can certainly be beneficial, but in this case I would personally have found it simpler if the variable and method types had been explicitly declared.&lt;/p&gt;  &lt;p&gt;So, for the dynamic typing fans (and I&amp;#39;m sure several readers will come into that category):&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;How often do you take advantage of dynamic typing in a way that really wouldn&amp;#39;t be feasible (or would be very clunky) in a statically typed language?&lt;/li&gt;    &lt;li&gt;Is it usually the same single problem which crops up regularly, or do you find a wide variety of problems benefit from dynamic typing?&lt;/li&gt;    &lt;li&gt;When you declare a variable (or first assign a value to a variable, if your language doesn&amp;#39;t use explicit declarations) how often do you really either not know its type or want to use some aspect of it which wouldn&amp;#39;t typically have been available in a statically typed environment?&lt;/li&gt;    &lt;li&gt;What balance do you find in your use of duck typing (the same method/member/message has already been declared on multiple types, but there&amp;#39;s no common type or interface) vs truly dynamic reaction based on introspection of the message within code (e.g. building a query based on the name of the method, such as FindBooksByAuthor(&amp;quot;Josh Bloch&amp;quot;))?&lt;/li&gt;    &lt;li&gt;What aspects of dynamic typing do I appear to be completely unaware of?&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Hopefully someone will be able to turn the light bulb on for me, so I can be more genuinely enthusiastic about dynamic typing, and perhaps even diversify from my comfort zone of C#…&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1740168" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/-skiprAdosg" height="1" width="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/General/default.aspx">General</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Books/default.aspx">Books</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/11/17/where-do-you-benefit-from-dynamic-typing.aspx</feedburner:origLink></item><item><title>Just how spiky is your traffic?</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/2f-L3IGvFUM/just-how-spiky-is-your-traffic.aspx</link><pubDate>Mon, 16 Nov 2009 19:48:19 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1740046</guid><dc:creator>skeet</dc:creator><slash:comments>9</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1740046</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1740046</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/11/16/just-how-spiky-is-your-traffic.aspx#comments</comments><description>&lt;p&gt;No, this isn&amp;#39;t the post about dynamic languages I promise. That will come soon. This is just a quick interlude. This afternoon, while answering a question on Stack Overflow&lt;sup&gt;1&lt;/sup&gt; about the difference between using an array and a Dictionary&amp;lt;string, string&amp;gt; (where each string was actually the string representation of an integer) I posted the usual spiel about preferring readable code to micro-optimisation. The response in a comment - about the performance aspect - was:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Well that&amp;#39;s not so easily said for a .com where performance on a site that receives about 1 million hits a month relies on every little ounce of efficiency gains you can give it.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;A million hits a month, eh? That sounds quite impressive, until you actually break it down. Let&amp;#39;s take a month of 30 days - that has 30 * 24 * 60 * 60 = 2,592,000 seconds&lt;sup&gt;2&lt;/sup&gt;. In other words, a million hits a month is less than one hit every two seconds. Not so impressive. At Google we tend to measure traffic in QPS (queries per second, even if they&amp;#39;re not really queries - the search terminology becomes pervasive) so this is around 0.39 QPS. Astonished that someone would make such a claim in favour of micro-optimisation at that traffic level, I &lt;a href="http://twitter.com/jonskeet/status/5767353745"&gt;tweeted about it&lt;/a&gt;. Several of the replies were along the lines of &amp;quot;yeah, but traffic&amp;#39;s not evenly distributed.&amp;quot; That&amp;#39;s entirely true. Let&amp;#39;s see how high we can make the traffic without going absurd though.&lt;/p&gt;  &lt;p&gt;Let&amp;#39;s suppose this is a site which is only relevant on weekdays - that cuts us down to 20 days in the month. Now let&amp;#39;s suppose it&amp;#39;s only relevant for one hour per day - it&amp;#39;s something people look at when they get to work, and most of the users are in one time zone. That&amp;#39;s a pretty massive way of spiking. We&amp;#39;ve gone down from 30 full days of traffic to 20 hours - or 20 * 60 * 60 = 72000 seconds, giving 14 QPS. Heck, let&amp;#39;s say the peak of the spike is double that - a whopping 28 QPS.&lt;/p&gt;  &lt;p&gt;Three points about this:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;28 QPS is still not a huge amount of traffic.&lt;/li&gt;    &lt;li&gt;If you&amp;#39;re &lt;em&gt;really&lt;/em&gt; interested in handling peak traffic of ~28 QPS without latency becoming huge, it&amp;#39;s worth quoting &lt;em&gt;that&lt;/em&gt; figure rather than &amp;quot;a million hits a month&amp;quot; because the latter is somewhat irrelevant, and causes us to make wild (and probably wildly inaccurate) guesses about your load distribution.&lt;/li&gt;    &lt;li&gt;If you&amp;#39;re going to bring the phrase &amp;quot;a .com&amp;quot; into the picture, attempting to make it sound particularly important, you really shouldn&amp;#39;t be thinking about hosting your web site on one server - so the QPS gets diluted again.&lt;/li&gt;    &lt;li&gt;Even at 28 QPS, the sort of difference that would be made here is tiny. A quick microbenchmark (with all the associated caveats) showed that on my laptop (hardly a server-class machine) I could build the dictionary and index into it 3 times 2.8 &lt;em&gt;million&lt;/em&gt; times in about 5 seconds. If every request needed to do that 100 times, then the cost of doing it 28 requests per second on my laptop would still only be 0.5% of that second - not a really significant benefit, despite the hugely exaggerated estimates of how often we needed to do that.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;There are various other ways in which it&amp;#39;s not a great piece of code, but the charge against premature optimization still stands. You &lt;em&gt;don&amp;#39;t&lt;/em&gt; need to get every little ounce of efficiency out of your code. Chances are, if you start guessing at where you can get efficiency, you&amp;#39;re going to be wrong. Measure, measure, measure - profile, profile, profile. Once you&amp;#39;ve done all of that and proved that a change reducing clarity has a significant benefit, go for it - but until then, write the most readable code you can. Likewise work out your performance goals in a &lt;em&gt;meaningful&lt;/em&gt; fashion before you worry too much - and hits per months isn&amp;#39;t a meaningful figure.&lt;/p&gt;  &lt;p&gt;Performance is important - too important to be guessed about instead of measured.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;hr /&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; I&amp;#39;m not linking to it because the Streisand effect would render this question more important than it really is. I&amp;#39;m sure you can find it if you really want to, but that&amp;#39;s not the point of the post.&lt;/p&gt;  &lt;p&gt;&lt;sup&gt;2&lt;/sup&gt; Anyone who wants to nitpick and talk about months which are a bit longer or shorter than that due to daylight saving time changes (despite still being 30 days) can implement that logic for me in Noda Time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1740046" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/2f-L3IGvFUM" height="1" width="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/General/default.aspx">General</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Stack+Overflow/default.aspx">Stack Overflow</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/11/16/just-how-spiky-is-your-traffic.aspx</feedburner:origLink></item><item><title>Noda Time gets its own blog</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/fb_U7wx7VZE/noda-time-gets-its-own-blog.aspx</link><pubDate>Fri, 13 Nov 2009 19:54:48 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1739567</guid><dc:creator>skeet</dc:creator><slash:comments>3</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1739567</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1739567</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/11/13/noda-time-gets-its-own-blog.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;ve decided it&amp;#39;s probably not a good idea to make general Noda Time posts on my personal blog. I&amp;#39;ll still post anything that&amp;#39;s particularly interesting in a &amp;quot;general coding&amp;quot; kind of way here, even if I discover it in Noda Time, but I thought it would be good for the project to have a &lt;a href="http://noda-time.blogspot.com/"&gt;blog of its very own&lt;/a&gt;, which other team members can post to.&lt;/p&gt;  &lt;p&gt;I still have plenty of things I want to blog about here. Next up is likely to be a request for help: I want someone to tell me why I should love the &amp;quot;dynamic&amp;quot; bit of dynamic languages. Stay tuned for more details :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1739567" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/fb_U7wx7VZE" height="1" width="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/General/default.aspx">General</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Noda+Time/default.aspx">Noda Time</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/11/13/noda-time-gets-its-own-blog.aspx</feedburner:origLink></item><item><title>Noda Time is born</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/LTH3ixYfiZQ/noda-time-is-born.aspx</link><pubDate>Fri, 06 Nov 2009 18:02:31 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1738052</guid><dc:creator>skeet</dc:creator><slash:comments>44</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1738052</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1738052</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/11/06/noda-time-is-born.aspx#comments</comments><description>&lt;p&gt;There was an amazing response to &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2009/11/05/what-s-in-a-name-again.aspx"&gt;yesterday&amp;#39;s post&lt;/a&gt; – not only did readers come up with plenty of names, but lots of people volunteered to help. As a result, I&amp;#39;m feeling under a certain amount of pressure for this project to actually take shape.&lt;/p&gt;  &lt;p&gt;The final name chosen is Noda Time. We now have a &lt;a href="http://code.google.com/p/noda-time/"&gt;Google Code Project&lt;/a&gt; and a &lt;a href="http://groups.google.com/group/noda-time"&gt;Google Group&lt;/a&gt; (/mailing list). Now we just need some code…&lt;/p&gt;  &lt;p&gt;I figured it would be worth explaining a bit more about my vision for the project. Obviously I&amp;#39;m only one contributor, and I&amp;#39;m expecting everyone to add there own views, but this can act as a starting point.&lt;/p&gt;  &lt;p&gt;I want this project to be more than just a way of getting better date and time handling on .NET. I want it to be a shining example of how to build, maintain and deploy an open source .NET library. As some of you know, I have a few other open source projects on the go, and they have different levels of polish. Some have downloadable binaries, some don&amp;#39;t. They all have just-about-enough-to-get-started documentation, but not nearly enough, really. They have widely varying levels of test coverage. Some are easier to build than others, depending on what platform you&amp;#39;re using.&lt;/p&gt;  &lt;p&gt;In some ways, I&amp;#39;m expecting the code to be the easy part of Noda Time. After all, the implementation is there already – we&amp;#39;ll have plenty of interesting design decisions to make in order to marry the concepts of Joda Time with the conventions of .NET, but that shouldn&amp;#39;t be too hard. Here are the trickier things, which need discussion, investigation and so forth:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;What platforms do we support? Here&amp;#39;s my personal suggested list:&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;.NET 4.0&lt;/li&gt;      &lt;li&gt;.NET 3.5&lt;/li&gt;      &lt;li&gt;.NET 2.0SP1 (require the service pack for DateTimeOffset)&lt;/li&gt;      &lt;li&gt;Mono (versions TBD)&lt;/li&gt;      &lt;li&gt;Silverlight 2, 3 and 4&lt;/li&gt;      &lt;li&gt;Compact Framework 2.0 and 3.5 &lt;/li&gt;   &lt;/ul&gt;    &lt;li&gt;What do we ship, and how do we handle different platforms? For example, can we somehow use &lt;a href="http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx"&gt;Code Contracts&lt;/a&gt; to give developers a better experience on .NET 4.0 without making it really hard to build for other versions of .NET? Can we take advantage of the availability of TimeZoneInfo in .NET 3.5 and still build fairly easily for earlier versions? Do developers want debug or release binaries? Can we build against the client profile of .NET 3.5/4.0?&lt;/li&gt;    &lt;li&gt;What should we use to build? I&amp;#39;ve previously used &lt;a href="http://nant.sourceforge.net/"&gt;NAnt&lt;/a&gt; for the overall build process and MSBuild for the code building part. While this has worked quite well, I&amp;#39;m nervous of the dependency on &lt;a href="http://nantcontrib.sourceforge.net/"&gt;NAnt-Contrib&lt;/a&gt; library for the &amp;lt;msbuild&amp;gt; task, and generally being dependent on a build project whose last release was a beta nearly two years ago. Are there better alternatives?&lt;/li&gt;    &lt;li&gt;How should documentation be created and distributed?&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Is &lt;a href="http://www.sandcastledocs.com/"&gt;Sandcastle&lt;/a&gt; the best way of building docs? How easy is it to get it running so that any developer can build the docs at any time? (I&amp;#39;ve previously tried a couple of times, and failed miserable.)&lt;/li&gt;      &lt;li&gt;Would &lt;a href="http://www.mono-project.com/Monodoc"&gt;Monodoc&lt;/a&gt; be a better approach? &lt;/li&gt;      &lt;li&gt;How should non-API documentation be handled? Is the wiki which comes with the Google Code project good enough? Do we need to somehow suck the wiki into an offline format for distribution with the binaries?&lt;/li&gt;   &lt;/ul&gt;    &lt;li&gt;What do we need to do in order to work in low-trust environments, and how easily can we test that?&lt;/li&gt;    &lt;li&gt;What do we do about signing? Ship with a &amp;quot;public&amp;quot; snk file which anyone can build with, but have a private version which the team uses to validate a &amp;quot;known good&amp;quot; release? Or just have the private key and use deferred signing?&lt;/li&gt;    &lt;li&gt;While the library itself will support i18n for things like date/time formatting, do we need to apply it to &amp;quot;developer only&amp;quot; messages such as exceptions?&lt;/li&gt;    &lt;li&gt;I&amp;#39;m used to testing with NUnit and Rhino.Mocks, but they&amp;#39;re not the last word in testing on .NET – what should we use, and why? What about coverage?&lt;/li&gt;    &lt;li&gt;Do we need any dependencies (e.g. logging)? If so, how do we handle versioning of those dependencies? How are we affected by various licences?&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;These are all interesting topics, but they&amp;#39;re not really specific to Noda Time. Information about them is available all over the place, but that&amp;#39;s just the problem – it&amp;#39;s all over the place. I would like there to be some sort of documentation saying, &amp;quot;These are the decisions you need to think about, here are the options we chose for Noda Time, and this is why we did so.&amp;quot; I don&amp;#39;t know what form that documentation will take yet, but I&amp;#39;m considering an ebook.&lt;/p&gt;  &lt;p&gt;As you can tell, I&amp;#39;m aiming pretty high with this project – especially as I won&amp;#39;t even be using Google&amp;#39;s 20% time on it. However, there&amp;#39;s little urgency in it for me personally. I want to work out how to do things &lt;em&gt;right&lt;/em&gt; rather than how to do them &lt;em&gt;quickly&lt;/em&gt;. If it takes me a bit of time to document various decisions, and the code itself ships later, so be it… it&amp;#39;ll make the next project that much speedier.&lt;/p&gt;  &lt;p&gt;I&amp;#39;m expecting a &lt;em&gt;lot&lt;/em&gt; of discussion in the group, and no doubt some significant disagreements. I&amp;#39;m expecting to have to ask a bunch of questions on Stack Overflow, revealing just how ignorant I am on a lot of the topics above (and more). I think it&amp;#39;ll be worth it though. I think it&amp;#39;s worth setting a goal:&lt;/p&gt;  &lt;p&gt;In one year, I want this to be a first-class project which is the natural choice for any developers wanting to do anything more than the simplest of date/time handling on .NET. In one year, I want to have a guide to developing open source class libraries on .NET which tells you everything you need to know other than how to write the code itself.&lt;/p&gt;  &lt;p&gt;A year may seem like a long time, but I&amp;#39;m sure everyone who has expressed an interest in the project has significant other commitments – I know I do. Getting there in a year is going to be a stretch – but I&amp;#39;m expecting it to be a very enlightening journey.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1738052" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/LTH3ixYfiZQ" height="1" width="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/Noda+Time/default.aspx">Noda Time</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/11/06/noda-time-is-born.aspx</feedburner:origLink></item><item><title>What's in a name (again)?</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/53PGI7ZyQhA/what-s-in-a-name-again.aspx</link><pubDate>Thu, 05 Nov 2009 20:01:05 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1737867</guid><dc:creator>skeet</dc:creator><slash:comments>54</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1737867</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1737867</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/11/05/what-s-in-a-name-again.aspx#comments</comments><description>&lt;p&gt;I have possibly foolishly decided to stop resisting the urge to port &lt;a href="http://joda-time.sf.net"&gt;Joda Time&lt;/a&gt; to .NET. For those of you who are unaware, &amp;quot;use Joda Time&amp;quot; is almost always the best answer to any question involving &amp;quot;how do I achieve X with java.util.Date/Calendar?&amp;quot; It&amp;#39;s a Java library for handling dates and times, and it rocks. There is a plan to include a somewhat redesigned version in some future edition of Java (&lt;a href="https://jsr-310.dev.java.net/"&gt;JSR-310&lt;/a&gt;) but it&amp;#39;s uncertain whether this will ever happen.&lt;/p&gt;  &lt;p&gt;Now, .NET only gained the ability to work with time zones other than UTC and the local time zone (using only managed code) – it has a bit of catching up to do. It&amp;#39;s generally easier to work with the .NET BCL than the Java built-in libraries, but it&amp;#39;s still not a brilliant position to be in. I think .NET deserves good date/time support, and as no-one else appears to be porting Joda Time, I&amp;#39;m going to do it. (A few people have already volunteered to help. I don&amp;#39;t know how easily we&amp;#39;ll be able to divvy up the work, but we&amp;#39;ll see. I suspect the core may need to be done first, and then people can jump in to implement different chronologies etc. As a side-effect, I may try to use this project as a sort of case in terms of porting, managing an open source project, and properly implementing a .NET library with useful versioning etc.)&lt;/p&gt;  &lt;p&gt;The first two problems, however, are to do with naming. First, the project name. Contenders include:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Joda Time.NET (sounds like it would be an absolutely direct port; while I intend to port all the tricky bits directly, it&amp;#39;s going to be an idiomatic port with appropriate .NET bits. It&amp;#39;s also a bit of a mouthful.) &lt;/li&gt;    &lt;li&gt;Noda Time (as suggested in the comments and in email)&lt;/li&gt;    &lt;li&gt;TonyTime (after &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2009/11/02/omg-ponies-aka-humanity-epic-fail.aspx"&gt;Tony the Pony&lt;/a&gt;) &lt;/li&gt;    &lt;li&gt;CoffeeTime &lt;/li&gt;    &lt;li&gt;TeaTime &lt;/li&gt;    &lt;li&gt;A progression of BreakfastTime, CoffeeTime, LunchTime, TeaTime, DinnerTime and SupperTime for different versions (not a serious contender) &lt;/li&gt;    &lt;li&gt;ParsleySageRosemaryAndThyme (not a serious contender) &lt;/li&gt;    &lt;li&gt;A few other silly ones too &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I suspect I&amp;#39;m going to go for CoffeeTime, but we&amp;#39;ll see.&lt;/p&gt;  &lt;p&gt;The second problem is going to prove more awkward. I want to mostly copy the names given in Joda Time – aside from anything else, it&amp;#39;ll make it familiar to anyone who uses Joda Time in Java (such as me). Now one of the most commonly used classes in Joda is &amp;quot;&lt;a href="http://joda-time.sourceforge.net/api-release/org/joda/time/DateTime.html"&gt;DateTime&lt;/a&gt;&amp;quot;. Using that name in my port would be a Bad Idea. Shadowing a name in the &lt;a href="http://msdn.microsoft.com/en-us/library/system.aspx"&gt;System&lt;/a&gt; namespace is likely to lead to very disgruntled users who may prove hard to regruntle before they abandon the library.&lt;/p&gt;  &lt;p&gt;So what do I do? Go for the subtly different DateAndTime? Tie it to the library with CoffeeDateTime? Change it to Instant? (It&amp;#39;ll derive from AbstractInstant anyway – assuming I keep the same hierarchy instead of moving to a composition model and value types.)&lt;/p&gt;  &lt;p&gt;Obviously this is a decision which the &amp;quot;team&amp;quot; can make, when we&amp;#39;ve got one… but it feels like a decision which is lurking round the corner in a hostile way.&lt;/p&gt;  &lt;p&gt;What I find interesting is that these are two very different naming problems: one is trying to name something in a relatively arbitrary way – I know I want something reasonably short and memorable for the overall name, but beyond that it doesn&amp;#39;t matter too much. The other is trying to nail a very specific name which really &lt;em&gt;has&lt;/em&gt; to convey its meaning clearly… but where the obvious name is already taken. Also interestingly, neither is a particularly good example of my most common issue with naming: attempting to come up with a two or three word noun for something that actually needs a whole sentence to describe it adequately.&lt;/p&gt;  &lt;p&gt;Oh well – we&amp;#39;ll see what happens. In another blog post I&amp;#39;ll suggest some of the goals I have in terms of what I&amp;#39;m hoping to learn from the project, and how I&amp;#39;d like it to progress. In other words, expect a work of complete fiction…&lt;/p&gt;  &lt;p&gt;If you&amp;#39;re interested in helping out with the project, please &lt;a href="mailto:skeet@pobox.com"&gt;mail me directly&lt;/a&gt; (rather than adding comments here) and as soon as I&amp;#39;ve set the project up, I&amp;#39;ll invite you to the mailing list.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;UPDATE: &lt;/strong&gt;I&amp;#39;ve already got a few interested names, which is great. Rather than be dictatorial about this, I&amp;#39;ll put it to a vote of the people who are willing to help out on it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1737867" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/53PGI7ZyQhA" height="1" width="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/General/default.aspx">General</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/11/05/what-s-in-a-name-again.aspx</feedburner:origLink></item><item><title>Revisiting randomness</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/YAI4R0RLDSY/revisiting-randomness.aspx</link><pubDate>Wed, 04 Nov 2009 07:40:15 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1737577</guid><dc:creator>skeet</dc:creator><slash:comments>18</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1737577</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1737577</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/11/04/revisiting-randomness.aspx#comments</comments><description>&lt;p&gt;Almost every Stack Overflow question which includes the words &amp;quot;random&amp;quot; and &amp;quot;repeated&amp;quot; has the same basic answer. It&amp;#39;s one of the most common &amp;quot;gotchas&amp;quot; in .NET, Java, and no doubt other platforms: creating a new random number generator without specifying a seed will depend on the current instant of time. The current time as measured by the computer doesn&amp;#39;t change very often compared with how often you can create and use a random number generator – so code which repeatedly creates a new instance of Random and uses it once will end up showing a lot of repetition.&lt;/p&gt;  &lt;p&gt;One common solution is to use a static field to store a single instance of Random and reuse it. That&amp;#39;s okay in Java (where Random is thread-safe) but it&amp;#39;s not so good in .NET – if you use the same instance repeatedly from .NET, you can corrupt the internal data structures.&lt;/p&gt;  &lt;p&gt;A long time ago, I created a &lt;a href="http://pobox.com/~skeet/csharp/miscutil/usage/staticrandom.html"&gt;StaticRandom class&lt;/a&gt; in &lt;a href="http://pobox.com/~skeet/csharp/miscutil"&gt;MiscUtil&lt;/a&gt; – essentially, it was just a bunch of static methods (to mirror the instance methods found in Random) wrapping a single instance of Random and locking appropriately. This allows you to just call StaticRandom.Next(1, 7) to roll a die, for example. However, it has a couple of problems:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;It doesn&amp;#39;t scale well in a multi-threaded environment. When I originally wrote it, I benchmarked an alternative approach using [ThreadStatic] and at the time, locking won (at least on my computer, which may well have only had a single core). &lt;/li&gt;    &lt;li&gt;It doesn&amp;#39;t provide any way of getting at an instance of Random, other than by using new Random(StaticRandom.Next()). &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The latter point is mostly a problem because it encourages a style of coding where you just use StaticRandom.Next(…) any time you want a random number. This is undoubtedly convenient in some situations, but it goes against the idea of &lt;a href="http://stackoverflow.com/questions/1667516/doesnt-passing-in-parameters-that-should-be-known-implicitly-violate-encapsulati/1667590#1667590"&gt;treating a source of randomness as a service or dependency&lt;/a&gt;. It makes it harder to get repeatability and to see what needs that dependency.&lt;/p&gt;  &lt;p&gt;I could have just added a method generating a new instance into the existing class, but I decided to play with a different approach – going back to per-thread instances, but this time using the &lt;a href="http://msdn.microsoft.com/en-us/library/dd642243(VS.100).aspx"&gt;ThreadLocal&amp;lt;T&amp;gt; class&lt;/a&gt; introduced in .NET 4.0. Here&amp;#39;s the resulting code:&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.Threading;     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Namespace"&gt;namespace&lt;/span&gt; RandomDemo     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// Convenience class for dealing with randomness.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; ThreadLocalRandom     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// Random number generator used to generate seeds,&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// which are then used to create new random number&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// generators on a per-thread basis.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Random globalRandom = &lt;span class="Keyword"&gt;new&lt;/span&gt; Random();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;object&lt;/span&gt; globalLock = &lt;span class="Keyword"&gt;new&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;object&lt;/span&gt;();     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// Random number generator &lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; ThreadLocal&amp;lt;Random&amp;gt; threadRandom = &lt;span class="Keyword"&gt;new&lt;/span&gt; ThreadLocal&amp;lt;Random&amp;gt;(NewRandom);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// Creates a new instance of Random. The seed is derived &lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// from a global (static) instance of Random, rather &lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// than time.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt; Random NewRandom()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;lock&lt;/span&gt; (globalLock)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; Random(globalRandom.Next());     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// Returns an instance of Random which can be used freely&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// within the current thread.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt; Random Instance { get { &lt;span class="Statement"&gt;return&lt;/span&gt; threadRandom.Value; } }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;See &amp;lt;see cref=&amp;quot;Random.Next()&amp;quot; /&amp;gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;int&lt;/span&gt; Next()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; Instance.Next();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;See &amp;lt;see cref=&amp;quot;Random.Next(int)&amp;quot; /&amp;gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;int&lt;/span&gt; Next(&lt;span class="ValueType"&gt;int&lt;/span&gt; maxValue)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; Instance.Next(maxValue);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;See &amp;lt;see cref=&amp;quot;Random.Next(int, int)&amp;quot; /&amp;gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;int&lt;/span&gt; Next(&lt;span class="ValueType"&gt;int&lt;/span&gt; minValue, &lt;span class="ValueType"&gt;int&lt;/span&gt; maxValue)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; Instance.Next(minValue, maxValue);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;See &amp;lt;see cref=&amp;quot;Random.NextDouble()&amp;quot; /&amp;gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;double&lt;/span&gt; NextDouble()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; Instance.NextDouble();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;See &amp;lt;see cref=&amp;quot;Random.NextBytes(byte[])&amp;quot; /&amp;gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; NextBytes(&lt;span class="ValueType"&gt;byte&lt;/span&gt;[] buffer)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Instance.NextBytes(buffer);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;The user can still call the static Next(…) methods if they want, but they can also get at the thread-local instance of Random by calling ThreadLocalRandom.Instance – or easily create a new instance with ThreadLocalRandom.NewRandom(). (The fact that NewRandom uses the global instance rather than the thread-local one is an implementation detail really; it happens to be convenient from the point of view of the ThreadLocal&amp;lt;T&amp;gt; constructor. It wouldn&amp;#39;t be terribly hard to change this.)&lt;/p&gt;  &lt;p&gt;Now it&amp;#39;s easy to write a method which needs randomness (e.g. to shuffle a deck of cards) and give it a Random parameter, then call it using the thread-local instance:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Shuffle(Random rng)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ...     &lt;br /&gt;}     &lt;br /&gt;...     &lt;br /&gt;deck.Shuffle(ThreadLocalRandom.Instance); &lt;/div&gt;  &lt;p&gt;The Shuffle method is then easier to test and debug, and expresses its dependency explicitly.&lt;/p&gt;  &lt;h3&gt;Performance&lt;/h3&gt;  &lt;p&gt;I tested the &amp;quot;old&amp;quot; and &amp;quot;new&amp;quot; implementations in a very simple way – for varying numbers of threads, I called Next() a fixed number of times (from each thread) and timed how long it took for all the threads to finish. I&amp;#39;ve also tried a .NET-3.5-compatible version using ThreadStatic instead of ThreadLocal&amp;lt;T&amp;gt;, and running the original code and the ThreadStatic version on .NET 3.5 as well.&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td align="right"&gt;Threads&lt;/td&gt;        &lt;td align="right"&gt;StaticRandom (4.0b2)&lt;/td&gt;        &lt;td align="right"&gt;ThreadLocalRandom (4.0b2)&lt;/td&gt;        &lt;td align="right"&gt;ThreadStaticRandom (4.0b2)&lt;/td&gt;        &lt;td align="right"&gt;StaticRandom(3.5)&lt;/td&gt;        &lt;td align="right"&gt;ThreadStaticRandom (3.5)&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td align="right"&gt;1&lt;/td&gt;        &lt;td align="right"&gt;11582&lt;/td&gt;        &lt;td align="right"&gt;6016&lt;/td&gt;        &lt;td align="right"&gt;10150&lt;/td&gt;        &lt;td align="right"&gt;10373&lt;/td&gt;        &lt;td align="right"&gt;16049&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td align="right"&gt;2&lt;/td&gt;        &lt;td align="right"&gt;24667&lt;/td&gt;        &lt;td align="right"&gt;7214&lt;/td&gt;        &lt;td align="right"&gt;9043&lt;/td&gt;        &lt;td align="right"&gt;25062&lt;/td&gt;        &lt;td align="right"&gt;17257&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td align="right"&gt;3&lt;/td&gt;        &lt;td align="right"&gt;38095&lt;/td&gt;        &lt;td align="right"&gt;10295&lt;/td&gt;        &lt;td align="right"&gt;14771&lt;/td&gt;        &lt;td align="right"&gt;36827&lt;/td&gt;        &lt;td align="right"&gt;25625&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td align="right"&gt;4&lt;/td&gt;        &lt;td align="right"&gt;49402&lt;/td&gt;        &lt;td align="right"&gt;13435&lt;/td&gt;        &lt;td align="right"&gt;19116&lt;/td&gt;        &lt;td align="right"&gt;47882&lt;/td&gt;        &lt;td align="right"&gt;34415&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;A few things to take away from this:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The numbers were slightly erratic; somehow it was quicker to do twice the work with ThreadStaticRandom on .NET 4.0b2! This isn&amp;#39;t the perfect benchmarking machine; we&amp;#39;re interested in trends rather than absolute figures. &lt;/li&gt;    &lt;li&gt;Locking hasn&amp;#39;t changed much in performance between framework versions &lt;/li&gt;    &lt;li&gt;ThreadStatic has improved &lt;em&gt;massively&lt;/em&gt; between .NET 3.5 and 4.0 &lt;/li&gt;    &lt;li&gt;Even on 3.5, ThreadStatic wins over a global lock as soon as there&amp;#39;s contention &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The only downside to the ThreadLocal&amp;lt;T&amp;gt; version is that it requires .NET 4.0 - but the ThreadStatic version works pretty well too.&lt;/p&gt;  &lt;p&gt;It&amp;#39;s worth bearing in mind that of course this is testing the highly unusual situation where there&amp;#39;s a &lt;em&gt;lot&lt;/em&gt; of contention in the global lock version. The performance difference in the single-threaded version where the lock is always uncontended is still present, but very small.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;After reading the comments and thinking further, I would indeed get rid of the static methods elsewhere. Also, for the purposes of dependency injection, I agree that it&amp;#39;s a good idea to have a factory interface &lt;em&gt;where that&amp;#39;s not overkill&lt;/em&gt;. The factory implementation could use either the ThreadLocal or ThreadStatic implementations, or effectively use the global lock version (by having its own instance of Random and a lock). In many cases I&amp;#39;d regard that as overkill, however.&lt;/p&gt;  &lt;p&gt;One other interesting option would be to create a thread-safe instance of Random to start with, which delegated to thread-local &amp;quot;normal&amp;quot; implementations. That would be very useful from a DI standpoint.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1737577" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/YAI4R0RLDSY" height="1" width="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/Parallelisation/default.aspx">Parallelisation</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Stack+Overflow/default.aspx">Stack Overflow</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Benchmarking/default.aspx">Benchmarking</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/11/04/revisiting-randomness.aspx</feedburner:origLink></item><item><title>OMG Ponies!!! (Aka Humanity: Epic Fail)</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/0T3s062Ihdw/omg-ponies-aka-humanity-epic-fail.aspx</link><pubDate>Mon, 02 Nov 2009 22:20:10 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1737163</guid><dc:creator>skeet</dc:creator><slash:comments>62</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1737163</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1737163</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/11/02/omg-ponies-aka-humanity-epic-fail.aspx#comments</comments><description>&lt;p&gt;(Meta note: I tried to fix the layout for this, I really did. But my CSS skills are even worse than Tony&amp;#39;s. If anyone wants to send me a complete sample of how I should have laid this out, I&amp;#39;ll fix it up. Otherwise, this is as good as you&amp;#39;re going to get :)&lt;/p&gt;  &lt;p&gt;Last week at Stack Overflow DevDays, London I presented a talk on how humanity had made life difficult for software developers. There&amp;#39;s now a &lt;a href="http://vimeo.com/7516539"&gt;video&lt;/a&gt; of it on Vimeo - the audio is fairly poor at the very start, but it improves pretty soon. At the very end my video recorder ran out of battery, so you&amp;#39;ve just got my slides (and audio) for that portion. Anyway, here&amp;#39;s my slide deck and what I &lt;em&gt;meant&lt;/em&gt; to say. (A couple of times I forgot exactly which slide was coming next, unfortunately.)&lt;/p&gt;  &lt;p&gt;Click on any thumbnail for a larger view.&lt;/p&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image001.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image001.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Good afternoon. This talk will be a little different from the others we&amp;#39;ve heard today... Joel mentioned on the podcast a long time ago that I&amp;#39;d talk about something &amp;quot;fun and esoteric&amp;quot; – and while I personally find C# 4 fun, I&amp;#39;m not sure that anyone could really call it esoteric. So instead, I thought I&amp;#39;d rant for half an hour about how mankind has made our life so difficult.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image002.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image002.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;By way of introduction, I&amp;#39;m Jon Skeet. You may know me from questions such as &lt;a href="http://meta.stackoverflow.com/questions/9134/jon-skeet-facts"&gt;Jon Skeet Facts&lt;/a&gt;, &lt;a href="http://meta.stackoverflow.com/questions/555/why-does-jon-skeet-never-sleep"&gt;Why does Jon Skeet never sleep?&lt;/a&gt; and a few C# questions here and there. This is Tony the Pony. He&amp;#39;s a developer, but I&amp;#39;m afraid he&amp;#39;s not a very good one.&lt;/p&gt;    &lt;p&gt;(Tony whispers) Tony wants to make it clear that he&amp;#39;s not &lt;i&gt;just&lt;/i&gt; a developer. He has another job, as a magician. Are you any better at magic than development then? (Tony whispers) Oh, I see. He&amp;#39;s not very good at magic either – his repertoire is extremely limited. Basically he&amp;#39;s a one trick pony.&lt;/p&gt;    &lt;p&gt;Anyway, when it comes to software, Tony gets things done, but he&amp;#39;s not terribly smart. He comes unstuck with some of the most fundamental data types we have to work with. It&amp;#39;s really not his fault though – humanity has let him down by making things just way too complicated.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image003.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image003.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;You see, the problem is that developers are already meant to be thinking about difficult things... coming up with a better widget to frobjugate the scarf handle, or whatever business problem they&amp;#39;re thinking about. They&amp;#39;ve really got enough to deal with – the simple things ought to be simple.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image004.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image004.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Unfortunately, time and time again we come up against problems with core elements of software engineering. Any resemblance between this slide and the coding horror logo is truly coincidental, by the way. Tasks which initially sound straightforward become insanely complicated. My aim in this talk is to distribute the blame amongst three groups of people.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image005.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image005.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;First, let&amp;#39;s blame users – or mankind as a whole. Users always have an idea that what they want is easy, even if they can&amp;#39;t really articulate exactly what they &lt;i&gt;do&lt;/i&gt; want. Even if they &lt;i&gt;can&lt;/i&gt; give you requirements, chances are those will conflict – often in subtle ways – with requirements of others. A lot of the time, we wouldn&amp;#39;t even think of these problems as &amp;quot;requirements&amp;quot; – they&amp;#39;re just things that everyone expects to work in &amp;quot;the obvious way&amp;quot;. The trouble is that humanity has come up with all kinds of entirely different &amp;quot;obvious ways&amp;quot; of doing things. Mankind&amp;#39;s model of the universe is a surprisingly complicated one.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image006.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image006.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Next, I want to blame architects. I&amp;#39;m using the word &amp;quot;architect&amp;quot; in a very woolly sense here. I&amp;#39;m trying to describe the people who come up with operating systems, protocols, libraries, standards: things we build our software on top of. These are the people who have carefully considered the complicated model used by real people, stroked their beards, and designed something almost exactly as complicated, but not quite compatible with the original.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image007.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image007.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Finally, I&amp;#39;m going to blame us – common or garden developers. We have four problems: first, we don&amp;#39;t understand the complex model designed by mankind. Second, we don&amp;#39;t understand the complex model designed by the architects. Third, we don&amp;#39;t understand the applications we&amp;#39;re trying to build. Fourth, even when we get the first three bits right individually, we still screw up when we try to put them together.&lt;/p&gt;    &lt;p&gt;For the rest of this talk, I&amp;#39;m going to give three examples of how things go wrong. First, let&amp;#39;s talk about numbers.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image008.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image008.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;You would think we would know how numbers work by now. We&amp;#39;ve all been doing maths since primary school. You&amp;#39;d also think that computers knew how to handle numbers by now – that&amp;#39;s basically what they&amp;#39;re built on. How is it that we can search billions of web pages in milliseconds, but we can&amp;#39;t get simple arithmetic right? How many times are we going to see Stack Overflow questions along the lines of &lt;a href="http://stackoverflow.com/questions/1420752"&gt;&amp;quot;Is double multiplication broken in .NET?&amp;quot;&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;I blame evolution.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image009.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image009.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;We have evolved with 8 fingers and 2 thumbs – a total of 10 digits. This was clearly a mistake. It has led to great suffering for developers. Life would have been a lot simpler if we&amp;#39;d only had eight digits.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image010.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image010.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Admittedly this gives us three bits, which isn&amp;#39;t quite ideal – but having 16 digits (fourteen fingers and two thumbs) or 4 digits (two fingers and two thumbs) could be tricky. At least with eight digits, we&amp;#39;d be able to fit in with binary &lt;i&gt;reasonably&lt;/i&gt; naturally. Now just so you don&amp;#39;t think I&amp;#39;m being completely impractical, there&amp;#39;s another solution – we could have just counted up to eight and ignored our thumbs. Indeed, we could even have used thumbs as parity bits. But no, mankind decided to count to ten, and that&amp;#39;s where all the problems started.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image0011.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image011.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Now, Tony – here&amp;#39;s a little puzzle for you. I want you to take a look at this piece of Java code (turn Tony to face screen). (Tony whispers) What do you mean you don&amp;#39;t know Java? All right, here&amp;#39;s the C# code instead...&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image011a.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image011a.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Is that better? (Tony nods enthusiastically) So, Tony, I want you to tell me the value of d after this line has executed. (Tony whispers)&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image011b.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image011b.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Tony thinks it&amp;#39;s 0.3 Poor Tony. Why on earth would you think that? Oh dear. Sorry, no it&amp;#39;s not.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image011c.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image011c.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;No, you were certainly close, but the exact value is:&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image012.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image012.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;0.299999 - Well, I&amp;#39;m not going to read it all out, but that&amp;#39;s the exact value. And it &lt;i&gt;is&lt;/i&gt; an exact value – the compiler has approximated the 0.3 in the source code to the nearest number which can be exactly represented by a double. It&amp;#39;s not the computer&amp;#39;s fault that we have this bizarre expectation that a number in our source code will be accurately represented internally.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image013.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image013.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Let&amp;#39;s take a look at two more numbers... 5 and a half in both cases. Now it doesn&amp;#39;t &lt;i&gt;look&lt;/i&gt; like these are really different – but they are. Indeed, if I were representing these two numbers in a program, I&amp;#39;d quite possibly use different types for them. The first value is discrete – there&amp;#39;s a single jump from £5.50 to £5.51, and those are exact amounts of money... whereas when we measure the mass of something, we always really mean “to two decimal places” or something similar. Nothing weighs &lt;i&gt;exactly&lt;/i&gt; five and a half kilograms. They&amp;#39;re fundamentally different concepts, they just happen to have the same value. What do you do with them? Well, continuous numbers are often best represented as float/double, whereas discrete decimal numbers are usually best represented using a decimal-based type.&lt;/p&gt;    &lt;p&gt;Now I&amp;#39;ve ignored an awful lot of things about numbers which can also trip us up – signed and unsigned, overflow, not-a-number values, infinities, normalised and denormal numbers, parsing and formatting, all kinds of stuff. But we should move on. Next stop, text.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image14b.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image14b.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Okay, so numbers aren&amp;#39;t as simple as we&amp;#39;d like them to be. Text ought to be easy though, right? I mean, my five year old son can read and write – how hard can it be? One bit of trivia - when I originally copied this out by hand, I missed out &amp;quot;ipsum.&amp;quot; Note to self: if you&amp;#39;re going to copy out &amp;quot;lorem ipsum&amp;quot; the two words you really, &lt;i&gt;really&lt;/i&gt; need to get at least &lt;i&gt;those&lt;/i&gt; words right. Fail.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image015.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image015.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Of course, I&amp;#39;m sure pretty much everyone here knows that text is actually a pain in the neck. Again, I will blame humanity. Here we have two sets of people using completely different characters, speaking different languages, and quite possibly reading in different directions. Apologies if the characters on the right accidentally spell a rude word, by the way - I just picked a few random Kanji characters from the Unicode charts. &lt;em&gt;(As pointed out in the comments, these aren&amp;#39;t actually Kanji characters anyway. They&amp;#39;re Katakana characters. Doh!)&lt;/em&gt; Cultural diversity has screwed over computing, basically.&lt;/p&gt;    &lt;p&gt;However, let&amp;#39;s take the fact that we&amp;#39;ve got lots of characters as a given. Unicode sorts all that out, right? Let&amp;#39;s see. Time for a coding exercise – Tony, I&amp;#39;d like you to write some code to reverse a string. (Tony whispers) No, I&amp;#39;m not going to start up Visual Studio for you. (Tony whispers) You&amp;#39;ve magically written it on the next slide? Okay, let&amp;#39;s take a look.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image016.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image016.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Well, this looks quite promising. We&amp;#39;re taking a string, converting it into a character array, reversing that array, and then building a new string. I&amp;#39;m impressed, Tony – you&amp;#39;ve avoided pointless string concatenation and everything. (Tony is happy.) Unfortunately...&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image016a.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image016a.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;... it&amp;#39;s broken. I&amp;#39;m just going to give &lt;i&gt;one&lt;/i&gt; example of how it&amp;#39;s broken – there are lots of others along the same lines. Let&amp;#39;s reverse one of my favourite musicals...&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image017a.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image017a.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Here&amp;#39;s one way of representing Les Miserables as a Unicode string. Instead of using one code point for the “e acute”, I&amp;#39;ve used a combining character to represent the accent, and then an unaccented ASCII e. Display this in a GUI, and it looks fine... but when we apply Tony&amp;#39;s reversing code...&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image017b.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image017b.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;... the combining character ends up &lt;i&gt;after&lt;/i&gt; the e, so we get an “s acute” instead. Sorry Tony. The Unicode designers with their fancy schemes have failed you.&lt;/p&gt;    &lt;blockquote&gt;     &lt;p&gt;EDIT: In fact, not only have the Unicode designers made things difficult, but so have implementers. You see, I couldn&amp;#39;t remember whether combining characters came before or after base characters, so I wrote a little Windows Forms app to check. That app displayed &amp;quot;Les Mis\u0301erables&amp;quot; as &amp;quot;Les Misérables&amp;quot;. Then, based on the comments below, I checked with the standard – and the &lt;a href="http://unicode.org/faq/char_combmark.html"&gt;Unicode combining marks FAQ&lt;/a&gt; indicates pretty clearly that the base character comes &lt;em&gt;before&lt;/em&gt; the combining character. Further failure points to both me and someone in Microsoft, unless I&amp;#39;m missing something. Thanks to McDowell for pointing this out in the comments. If I ever give this presentation again, I&amp;#39;ll be sure to point it out. WPF gets it right, by the way. Update: this can be fixed in Windows Forms by setting the &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.label.usecompatibletextrendering.aspx"&gt;UseCompatibleTextRendering&lt;/a&gt; property to false (or setting the default to false). Apparently the default is set to false when you create a new WinForms project in VS2008. Shame I tend to write &amp;quot;quick check&amp;quot; programs in a plain text editor…&lt;/p&gt;      &lt;p&gt;Of course the basic point about reversal still holds, but with the correct starting string you&amp;#39;d end up with an acute over the r, not the s.&lt;/p&gt;   &lt;/blockquote&gt;    &lt;p&gt;It&amp;#39;s not like the problems are solely in the realm of non-ASCII characters though. I present to you...&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image018.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image018.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;A line break. Or rather, &lt;i&gt;one&lt;/i&gt; of the representations of a line break. As if the natural cultural diversity of humanity hasn&amp;#39;t caused enough problems, software decided to get involved and have line break diversity. Heck, we&amp;#39;re not even just limited to CR, LF and CRLF – Unicode has its own special line terminator character as well, just for kicks.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image019.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image019.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;To prove this isn&amp;#39;t just a problem for toy examples, here&amp;#39;s something that really bit me, back about 9 or 10 years ago. Here&amp;#39;s some code which tries to do a case-insensitive comparison for the text &amp;quot;MAIL&amp;quot; in Java. Can anyone spot the problem?&lt;/p&gt;    &lt;p&gt;It fails in Turkey. This is reasonably well known now – there&amp;#39;s a page about the “Turkey test” encouraging you to try your applications in a Turkish locale – but at the time it was a mystery to me. If you&amp;#39;re not familiar with this, the problem is that if you upper-case an “i” in Turkish, you end up with an “I” with a dot on it. This code went into production, and we had a customer in Turkey whose server was behaving oddly. As you can imagine, if you&amp;#39;re not aware of that potential problem, it can take a heck of a long time to find that kind of bug.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image020a.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image020a.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Here&amp;#39;s some code from a newsgroup post. It&amp;#39;s somewhat inefficient code to collapse multiple spaces down to a single one. Leaving aside the inefficiency, it looks like it should work. This was before we had String.Contains, so it&amp;#39;s using IndexOf to check whether we&amp;#39;ve got a double space. While we can find two spaces in a row, we&amp;#39;ll replace any occurrence of two spaces with a single space. We&amp;#39;re assigning the result of string.Replace back to the same variable, so that&amp;#39;s avoided one common problem... so how could this fail?&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image020b.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image020b.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;This string will cause that code to go into a tight loop, due to this evil character here. It&amp;#39;s a &amp;quot;zero-width non-joiner&amp;quot; – basically a hint that the two characters either side of it shouldn&amp;#39;t be squashed up too closely together. &lt;code&gt;IndexOf&lt;/code&gt; ignores it, but &lt;code&gt;Replace&lt;/code&gt; doesn&amp;#39;t. Ouch.&lt;/p&gt;    &lt;p&gt;Now I&amp;#39;m not showing these examples to claim I&amp;#39;m some sort of Unicode expert – I&amp;#39;m really, really not. These are just corner cases I &lt;i&gt;happen&lt;/i&gt; to have run into. Just like with numbers, I&amp;#39;ve left out a whole bunch of problems like bidi, encodings, translation, culture-sensitive parsing and the like.&lt;/p&gt;    &lt;p&gt;Given the vast array of writing systems the world has come up with – and variations within those systems – any attempt to model text is going to be complicated. The problems come from the inherent complexity, some additional complexity introduced by things like surrogate pairs, and developers simply not having the time to become experts on text processing. &lt;/p&gt;    &lt;p&gt;So, we fail at both numbers and text. How about time?&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image021.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image021.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;I&amp;#39;m biased when it comes to time-related problems. For the last year or so I&amp;#39;ve been working on the Google&amp;#39;s implementation of ActiveSync, mostly focusing on the calendar side of things. That means I&amp;#39;ve been exposed to more time-based code than most developers... but it&amp;#39;s still a reasonably common area, as you can tell from the number of related questions on Stack Overflow.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image022.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image022.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;To make things slightly simpler, let&amp;#39;s ignore relativity. Let&amp;#39;s pretend that time is linear – after all, most systems are meant to be modelling the human concept of time, which definitely &lt;i&gt;doesn&amp;#39;t&lt;/i&gt; include relativity.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image023.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image023.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Likewise, let&amp;#39;s ignore leap seconds. This isn&amp;#39;t &lt;i&gt;always&lt;/i&gt; a good idea, and there are some wrinkles around library support. For example, Java &lt;i&gt;explicitly&lt;/i&gt; says that java.util.Date and Calendar may or may not account for leap seconds depending on the host support. So, it&amp;#39;s good to know how predictable &lt;i&gt;that&lt;/i&gt; makes our software... I&amp;#39;ve tried reading various explanations of leap seconds, and always ended up with a headache. For the purposes of this talk, I&amp;#39;m going to assert that they don&amp;#39;t exist.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image024.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image024.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Okay, so let&amp;#39;s start with something simple. Tony, what&amp;#39;s the time on this slide? (Tony whispers) Tony doesn&amp;#39;t want to answer. Anyone? &lt;i&gt;(Audience responds.)&lt;/i&gt; Yes, about 5 past 3 on October 28&lt;sup&gt;th&lt;/sup&gt;. So what&amp;#39;s the difference between now and the time on this slide? (Audience response.) No, it&amp;#39;s actually nearly twelve hours... this clock is showing 5 past 3 in the morning. Tony&amp;#39;s answer was actually the right one, in many ways... this slide has a hopeless amount of ambiguity. It&amp;#39;s not as bad as it might be, admittedly. Imagine if it said October 11&lt;sup&gt;th&lt;/sup&gt;... Jeff and Joel would be nearly a month out of sync with the rest of us. And then even if we get the date and the time right, it&amp;#39;s still ambiguous... because of time zones.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image025.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image025.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Ah, time zones. My favourite source of WTFs. I could rant for hours about them – but I&amp;#39;ll try not to. I&amp;#39;d just like to point out a few of the idiosyncrasies I&amp;#39;ve encountered. Let&amp;#39;s start off with the time zones on this slide. Notice anything strange? (Audience or whisper from Tony) Yes, CST is there three times. Once for Central Standard Time in the US – which is UTC-6. It&amp;#39;s also Central Standard Time in Australia – where it&amp;#39;s UTC+9.30. It&amp;#39;s also Central &lt;i&gt;Summer&lt;/i&gt; Time in Australia, where it&amp;#39;s UTC+10.30. I think it takes a special kind of incompetence to use the same acronym in the same place for different offsets.&lt;/p&gt;    &lt;p&gt;Then let&amp;#39;s consider time zones changing. One of the problems I face is having to encode or decode a time zone representation from a single pattern – something like &amp;quot;It&amp;#39;s UTC-3 or -2, and daylight savings are applied from the third Sunday in March to the first Sunday in November&amp;quot;. That&amp;#39;s all very well until the system changes. Some countries give plenty of warning of this... but on October 7&lt;sup&gt;th&lt;/sup&gt; this year, Argentina announced that it wasn&amp;#39;t going to use daylight saving time any more... 11 days before its next transition. The reason? Their dams are 90% full. I only heard about this due to one of my unit tests failing. For various complicated reasons, a unit test which expected to recognise the time zone for Godthab actually thought it was Buenos Aires. So due to rainfall thousands of miles away, my unit test had moved Greenland into Argentina. Fail.&lt;/p&gt;    &lt;p&gt;If you want more time zone incidents, talk to me afterwards. It&amp;#39;s a whole world of pain. I suggest we move away from time zones entirely. In fact, I suggest we adopt a much simpler system of time. I&amp;#39;m proud to present my proposal for &lt;i&gt;coffee time&lt;/i&gt;. This is a system which determines the current time based on the answer to the question: &amp;quot;Is it time for coffee?&amp;quot; This is what the clock looks like:&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image026.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image026.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;This clock is correct all over the world, is very cheap to produce, and is guaranteed to be accurate forever. Batteries not required.&lt;/p&gt;    &lt;p&gt;So where are we?&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image027a.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image027a.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;The real world has failed us. It has concentrated on local simplicity, leading to global complexity. It&amp;#39;s easy to organise a meeting if everyone is in the same time zone – but once you get different continents involved, invariably people get confused. It&amp;#39;s easy to get writing to work uniformly left to right or uniformly right to left – but if you&amp;#39;ve got a mixture, it becomes really hard to keep track of. The diversity which makes humanity such an interesting species is the curse of computing.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image027b.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image027b.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;When computer systems have tried to model this complexity, they&amp;#39;ve failed horribly. Exhibit A: &lt;code&gt;java.util.Calendar&lt;/code&gt;, with its incomprehensible set of precedence rules. Exhibit B: .NET&amp;#39;s date and time API, which until relatively recently didn&amp;#39;t let you represent any time zone other than UTC or the one local to the system.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image027c.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image027c.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;Developers have, collectively, failed to understand both the models and the real world. We only need one exhibit this time: the questions on Stack Overflow. Developers asking questions around double, or Unicode, or dates and times aren&amp;#39;t stupid. They&amp;#39;ve just been concentrating on other topics. They&amp;#39;ve made an assumption that the core building blocks of their trade would be simple, and it turns out they&amp;#39;re not.&lt;/p&gt; &lt;/div&gt;  &lt;div style="display:inline-block;"&gt;&lt;a href="http://csharpindepth.com/Files/DevDays09/Image028.jpg"&gt;&lt;img src="http://csharpindepth.com/Files/DevDays09/thumbnails/Image028.jpg" alt="" /&gt;&lt;/a&gt;     &lt;p&gt;This has all been pretty negative, for which I apologise. I&amp;#39;m not going to claim to have a complete solution to all of this – but I do want to give a small ray of hope. All this complexity can be managed to some extent, if you do three things.&lt;/p&gt;    &lt;p&gt;First, try not to take on more complexity than you need. If you can absolutely guarantee that you won&amp;#39;t need to translate your app, it&amp;#39;ll make your life a lot easier. If you don&amp;#39;t need to deal with different time zones, you can rejoice. Of course, if you write a lot of code under a set of assumptions which then changes, you&amp;#39;re in trouble... but quite often you &lt;i&gt;can&lt;/i&gt; take the &amp;quot;You ain&amp;#39;t gonna need it&amp;quot; approach.&lt;/p&gt;    &lt;p&gt;Next, learn just enough about the problem space so that you know more than your application&amp;#39;s requirements. You don&amp;#39;t need to know everything about Unicode – but you need to be aware of which corner cases might affect your application. You don&amp;#39;t need to know everything about how denormal number representation, but you may well need to know how rounding should be applied in your reports. If your knowledge is just a bit bigger than the code you need to write, you should be able to be reasonably comfortable.&lt;/p&gt;    &lt;p&gt;Pick the right platforms and libraries. Yes, there are some crummy frameworks around. There are also some good ones. What&amp;#39;s the canonical answer to almost any question about &lt;code&gt;java.util.Calendar&lt;/code&gt;? Use &lt;a href="http://joda-time.sf.net/"&gt;Joda Time&lt;/a&gt; instead. There are similar libraries like ICU – written by genuine experts in these thorny areas. The difference a good library can make is absolutely enormous.&lt;/p&gt;    &lt;p&gt;None of this will make you a good developer. Tony&amp;#39;s still likely to mis-spell his &amp;quot;main&amp;quot; method through force of habit. You&amp;#39;re still going to get off by one errors. You&amp;#39;re still going to forget to close database connections. But if you can at least get a handle on &lt;i&gt;some&lt;/i&gt; of the complexity of software engineering, it&amp;#39;s a start.&lt;/p&gt;    &lt;p&gt;Thanks for listening.&lt;/p&gt; &lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1737163" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/0T3s062Ihdw" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Speaking+engagements/default.aspx">Speaking engagements</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Stack+Overflow/default.aspx">Stack Overflow</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/11/02/omg-ponies-aka-humanity-epic-fail.aspx</feedburner:origLink></item><item><title>Contract classes and nested types within interfaces</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/7gJ_It9oQrE/contract-classes-and-nested-types-within-interfaces.aspx</link><pubDate>Sat, 31 Oct 2009 22:04:54 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1736652</guid><dc:creator>skeet</dc:creator><slash:comments>9</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1736652</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1736652</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/10/31/contract-classes-and-nested-types-within-interfaces.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;ve just been going through some feedback for the draft copy of the second edition of C# in Depth. In the contracts section, I have an example like this:&lt;/p&gt;  &lt;div class="code"&gt;[ContractClass(&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(ICaseConverterContracts))]     &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; ICaseConverter     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; Convert(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; text);     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;[ContractClassFor(&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(ICaseConverter))]     &lt;br /&gt;&lt;span class="Modifier"&gt;internal&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; ICaseConverterContracts : ICaseConverter     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; ICaseConverter.Convert(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; text)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Contract.Requires(text != &lt;span class="Keyword"&gt;null&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Contract.Ensures(Contract.Result&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;() != &lt;span class="Keyword"&gt;null&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;default&lt;/span&gt;(&lt;span class="ReferenceType"&gt;string&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt; ICaseConverterContracts() {}     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; InvariantUpperCaseFormatter : ICaseConverter     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; Convert(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; text)&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; text.ToUpperInvariant();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;The point is to demonstrate how contracts can be specified for interfaces, and then applied automatically to implementations. In this case, &lt;code&gt;ICaseConverter&lt;/code&gt; is the interface, &lt;code&gt;ICaseConverterContracts&lt;/code&gt; is the &lt;i&gt;contract class&lt;/i&gt; which specifies the contract for the interface, and &lt;code&gt;InvariantUpperCaseFormatter&lt;/code&gt; is the real implementation. The binary rewriter effectively copies the contract into each implementation, so you don&amp;#39;t need to duplicate the contract in the source code.&lt;/p&gt;  &lt;p&gt;The reader feedback asked where the contract class code should live - should it go in the same file as the interface itself, or in a separate file as normal? Now normally, I&amp;#39;m firmly of the &amp;quot;one top-level type per file&amp;quot; persuasion, but in this case I think it makes sense to keep the contract class with the interface. It has no meaning without reference to the interface, after all - it&amp;#39;s not a real implementation to be used in the normal way. It&amp;#39;s essentially metadata. This does, however, leave me feeling a little bit dirty. What I&amp;#39;d &lt;em&gt;really&lt;/em&gt; like to be able to do is nest the contract class inside the interface, just like I do with other classes which are tightly coupled to an &amp;quot;owner&amp;quot; type. Then the code would look like this:&lt;/p&gt;  &lt;div class="code"&gt;[ContractClass(&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(ICaseConverterContracts))]     &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; ICaseConverter     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; Convert(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; text);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; [ContractClassFor(&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(ICaseConverter))]     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;internal&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; ICaseConverterContracts : ICaseConverter     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; ICaseConverter.Convert(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; text)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Contract.Requires(text != &lt;span class="Keyword"&gt;null&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Contract.Ensures(Contract.Result&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;() != &lt;span class="Keyword"&gt;null&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;default&lt;/span&gt;(&lt;span class="ReferenceType"&gt;string&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt; ICaseConverterContracts() {}     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; InvariantUpperCaseFormatter : ICaseConverter     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; Convert(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; text)&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; text.ToUpperInvariant();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;That would make me feel happier - all the information to do with the interface would be specified within the interface type&amp;#39;s code. It&amp;#39;s possible that with that as a convention, the Code Contracts tooling could cope without the attributes - if interface &lt;code&gt;IFoo&lt;/code&gt; contains a nested class &lt;code&gt;IFooContracts&lt;/code&gt; which implements &lt;code&gt;IFoo&lt;/code&gt;, assume it&amp;#39;s a contract class and handle it appropriately. That would be sweet.&lt;/p&gt;  &lt;p&gt;You know the really galling thing? I&amp;#39;m pretty sure VB &lt;em&gt;does&lt;/em&gt; allow nested types in interfaces...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1736652" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/7gJ_It9oQrE" height="1" width="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/Books/default.aspx">Books</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/10/31/contract-classes-and-nested-types-within-interfaces.aspx</feedburner:origLink></item><item><title>MVP Again</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/CuSc5VzO9QM/mvp-again.aspx</link><pubDate>Fri, 30 Oct 2009 21:14:09 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1736410</guid><dc:creator>skeet</dc:creator><slash:comments>28</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1736410</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1736410</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/10/30/mvp-again.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;m delighted to be able to announce that I&amp;#39;m now an MVP again.&lt;/p&gt;  &lt;p&gt;Google has reconsidered the situation and worked out a compromise: I now receive no significant gifts from Microsoft, and I&amp;#39;m not under NDA with them. While that precludes me from a lot of MVP activities, it removes any concerns to do with Google&amp;#39;s &lt;a href="http://investor.google.com/conduct.html#III"&gt;Code of Conduct&lt;/a&gt;. Basically my MVP status is truly just a token of Microsoft&amp;#39;s recognition of what I&amp;#39;ve done in the C# community - and that&amp;#39;s fine by me.&lt;/p&gt;  &lt;p&gt;When I &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2009/10/01/mvp-no-more.aspx"&gt;announced&lt;/a&gt; that I&amp;#39;d been advised not to seek renewal, I was amazed at the scale of the reaction in the comments, other blog posts, Twitter and personal email. I was touched by the response of the community. I really love working at Google, and the fact that we could figure out a solution to this situation is definitely one of the things that makes Google such an awesome place to work. Oh, and did I mention that &lt;a href="http://www.google.co.uk/intl/en/jobs/index.html"&gt;we&amp;#39;re hiring&lt;/a&gt;? :)&lt;/p&gt;  &lt;p&gt;Anyway, the basic message of this post is: thanks to the community for caring, thanks to Google for reconsidering, and thanks to Microsoft for renewing my award. And they all lived happily ever after...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1736410" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/CuSc5VzO9QM" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/General/default.aspx">General</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Google/default.aspx">Google</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/10/30/mvp-again.aspx</feedburner:origLink></item><item><title>Migrating from Visual Studio 2010 beta 1 to beta 2 – solution file change required</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/cd1b502oNjc/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>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1735303</wfw:commentRss><wfw:comment>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;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/cd1b502oNjc" height="1" width="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><feedburner:origLink>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</feedburner:origLink></item><item><title>Iterating atomically</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/L8XiRzZI8Sc/iterating-atomically.aspx</link><pubDate>Fri, 23 Oct 2009 21:20:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1734632</guid><dc:creator>skeet</dc:creator><slash:comments>34</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1734632</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1734632</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/10/23/iterating-atomically.aspx#comments</comments><description>&lt;p&gt;The IEnumerable&amp;lt;T&amp;gt; and IEnumerator&amp;lt;T&amp;gt; interfaces in .NET are interesting. They crop up an awful lot, but hardly anyone ever calls them directly - you almost always use a foreach loop to iterate over the collection. That hides all the calls to GetEnumerator(), MoveNext() and Current. Likewise iterator blocks hide the details when you want to implement the interfaces. However, sometimes details matter - such as for &lt;a href="http://stackoverflow.com/questions/1605745"&gt;this recent Stack Overflow question&lt;/a&gt;. The question asks how to create a thread-safe iterator - one that can be called from multiple threads. This is not about iterating over a collection &lt;em&gt;n&lt;/em&gt; times independently on &lt;em&gt;n&lt;/em&gt; different threads - this is about iterating over a collection &lt;em&gt;once&lt;/em&gt; without skipping or duplicating. Imagine it&amp;#39;s some set of jobs that we have to complete. We assume that the iterator itself is thread-safe to the extent that calls from different threads &lt;em&gt;at different times, with intervening locks&lt;/em&gt; will be handled reasonably. This is reasonable - basically, so long as it isn&amp;#39;t going out of its way to be thread-hostile, we should be okay. We also assume that no-one is trying to write to the collection at the same time.&lt;/p&gt;
&lt;p&gt;Sounds easy, right? Well, no... because the IEnumerator&amp;lt;T&amp;gt; interface has two members which we effectively want to call atomically. In particular, we &lt;em&gt;don&amp;#39;t&lt;/em&gt; want the collection { &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot; } to be iterated like this:&lt;/p&gt;
&lt;table width="400" cellpadding="2" cellspacing="0" border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="200" valign="top"&gt;Thread 1&lt;/td&gt;
&lt;td width="200" valign="top"&gt;Thread 2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="200" valign="top"&gt;MoveNext()&lt;/td&gt;
&lt;td width="200" valign="top"&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="200" valign="top"&gt;&amp;nbsp;&lt;/td&gt;
&lt;td width="200" valign="top"&gt;MoveNext()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="200" valign="top"&gt;Current&lt;/td&gt;
&lt;td width="200" valign="top"&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="200" valign="top"&gt;&amp;nbsp;&lt;/td&gt;
&lt;td width="200" valign="top"&gt;Current&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;That way we&amp;#39;ll end up not processing the first item at all, and the second item twice.&lt;/p&gt;
&lt;p&gt;There are two ways of approaching this problem. In both cases I&amp;#39;ve started with IEnumerable&amp;lt;T&amp;gt; for consistency, but in fact it&amp;#39;s IEnumerator&amp;lt;T&amp;gt; which is the interesting bit. In particular, we&amp;#39;re not going to be able to iterate over our result anyway, as each thread needs to have the same IEnumerator&amp;lt;T&amp;gt; - which it won&amp;#39;t do if each of them uses foreach (which calls GetEnumerator() to start with).&lt;/p&gt;
&lt;h3&gt;Fix the interface&lt;/h3&gt;
&lt;p&gt;First we&amp;#39;ll try to fix the interface to look how it should have looked to start with, at least from the point of view of atomicity. Here are the new interfaces:&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; IAtomicEnumerable&amp;lt;T&amp;gt;     &lt;br /&gt;{     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; IAtomicEnumerator&amp;lt;T&amp;gt; GetEnumerator();     &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; IAtomicEnumerator&amp;lt;T&amp;gt;     &lt;br /&gt;{     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ValueType"&gt;bool&lt;/span&gt; TryMoveNext(&lt;span class="MethodParameter"&gt;out&lt;/span&gt; T nextValue);     &lt;br /&gt;} &lt;/div&gt;
&lt;p&gt;One thing you may notice is that we&amp;#39;re not implementing IDisposable. That&amp;#39;s basically because it&amp;#39;s a pain to do so when you think about a multi-threaded environment. Indeed, it&amp;#39;s possibly one of the biggest arguments against something of this nature. At what point do you dispose? Just because &lt;em&gt;one&lt;/em&gt; thread finished doesn&amp;#39;t mean that the rest of them have... don&amp;#39;t forget that &amp;quot;finish&amp;quot; might mean &amp;quot;an exception was thrown while processing the job, I&amp;#39;m bailing out&amp;quot;. You&amp;#39;d need some sort of co-ordinator to make sure that &lt;em&gt;everyone&lt;/em&gt; is finished before you actually do any clean-up. Anyway, the nice thing about this being a blog post is we can ignore that little thorny issue :)&lt;/p&gt;
&lt;p&gt;The important point is that we now have a single method in IAtomicEnumerator&amp;lt;T&amp;gt; - TryMoveNext, which works the way you&amp;#39;d expect it to. It atomically attempts to move to the next item, returns whether or not it succeeded, and sets an out parameter with the next value if it &lt;em&gt;did&lt;/em&gt; succeed. Now there&amp;#39;s no chance of two threads using the method and stomping on each other&amp;#39;s values (unless they&amp;#39;re silly and use the same variable for the out parameter).&lt;/p&gt;
&lt;p&gt;It&amp;#39;s reasonably easy to wrap the standard interfaces in order to implement this interface:&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;/// Wraps a normal IEnumerable[T] up to implement IAtomicEnumerable[T].&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="Modifier"&gt;sealed&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; AtomicEnumerable&amp;lt;T&amp;gt; : IAtomicEnumerable&amp;lt;T&amp;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; IEnumerable&amp;lt;T&amp;gt; original;     &lt;br /&gt;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; AtomicEnumerable(IEnumerable&amp;lt;T&amp;gt; original)     &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="Keyword"&gt;this&lt;/span&gt;.original = original;     &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; IAtomicEnumerator&amp;lt;T&amp;gt; GetEnumerator()     &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; AtomicEnumerator(original.GetEnumerator());     &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;/// Implementation of IAtomicEnumerator[T] to wrap IEnumerator[T].&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;private&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; AtomicEnumerator : IAtomicEnumerator&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="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; IEnumerator&amp;lt;T&amp;gt; original;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;&amp;nbsp;&lt;span class="ReferenceType"&gt;object&lt;/span&gt; padlock = &lt;span class="Keyword"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;object&lt;/span&gt;();     &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;internal&lt;/span&gt; AtomicEnumerator(IEnumerator&amp;lt;T&amp;gt; original)     &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="Keyword"&gt;this&lt;/span&gt;.original = original;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }     &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;&amp;nbsp;&lt;span class="ValueType"&gt;bool&lt;/span&gt; TryMoveNext(&lt;span class="MethodParameter"&gt;out&lt;/span&gt; T 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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;lock&lt;/span&gt; (padlock)     &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="ValueType"&gt;bool&lt;/span&gt; hadNext = original.MoveNext();     &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; value = hadNext ? original.Current : &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;&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; hadNext;     &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; }     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }     &lt;br /&gt;} &lt;/div&gt;
&lt;p&gt;Just ignore the fact that I never dispose of the original IEnumerator&amp;lt;T&amp;gt; :)&lt;/p&gt;
&lt;p&gt;We use a simple lock to make sure that MoveNext() and Current always happen together - that nothing else is going to call MoveNext() between our TryMoveNext() calling it, and it fetching the current value.&lt;/p&gt;
&lt;p&gt;Obviously you&amp;#39;d need to write your own code to actually &lt;em&gt;use&lt;/em&gt; this sort of iterator, but it would be quite simple:&lt;/p&gt;
&lt;div class="code"&gt;T value;    &lt;br /&gt;&lt;span class="Statement"&gt;while&lt;/span&gt; (iterator.TryMoveNext(&lt;span class="MethodParameter"&gt;out&lt;/span&gt; value))     &lt;br /&gt;{     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Use value&lt;/span&gt;     &lt;br /&gt;} &lt;/div&gt;
&lt;p&gt;However, you may already have code which wants to use an IEnumerator&amp;lt;T&amp;gt;. Let&amp;#39;s see what else we can do.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Using thread local variables to fake it&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;.NET 4.0 has a very useful type called ThreadLocal&amp;lt;T&amp;gt;. It does basically what you&amp;#39;d expect it to, with nice features such as being able to supply a delegate to be executed on each thread to provide the initial value. We can use a thread local to make sure that so long as we call both MoveNext() and Current atomically when we&amp;#39;re asked to move to the next element, we can get back the right value for Current later on. It has to be thread local because we&amp;#39;re sharing a single IEnumerator&amp;lt;T&amp;gt; across multiple threads - each needs its own separate storage.&lt;/p&gt;
&lt;p&gt;This is also the approach we&amp;#39;d use if we wanted to wrap an IAtomicEnumerator&amp;lt;T&amp;gt; in an IEnumerator&amp;lt;T&amp;gt;, by the way. Here&amp;#39;s the code to do it:&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; ThreadSafeEnumerable&amp;lt;T&amp;gt; : IEnumerable&amp;lt;T&amp;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; IEnumerable&amp;lt;T&amp;gt; original;     &lt;br /&gt;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; ThreadSafeEnumerable(IEnumerable&amp;lt;T&amp;gt; original)     &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="Keyword"&gt;this&lt;/span&gt;.original = original;     &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; IEnumerator&amp;lt;T&amp;gt; GetEnumerator()     &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; ThreadSafeEnumerator(original.GetEnumerator());     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }     &lt;br /&gt;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; IEnumerator IEnumerable.GetEnumerator()     &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; GetEnumerator();     &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;private&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; ThreadSafeEnumerator : IEnumerator&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="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; IEnumerator&amp;lt;T&amp;gt; original;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;&amp;nbsp;&lt;span class="ReferenceType"&gt;object&lt;/span&gt; padlock = &lt;span class="Keyword"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;object&lt;/span&gt;();     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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; ThreadLocal&amp;lt;T&amp;gt; current = &lt;span class="Keyword"&gt;new&lt;/span&gt; ThreadLocal&amp;lt;T&amp;gt;();     &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;internal&lt;/span&gt; ThreadSafeEnumerator(IEnumerator&amp;lt;T&amp;gt; original)     &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="Keyword"&gt;this&lt;/span&gt;.original = original;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }     &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;&amp;nbsp;&lt;span class="ValueType"&gt;bool&lt;/span&gt; MoveNext()     &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;lock&lt;/span&gt; (padlock)     &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="ValueType"&gt;bool&lt;/span&gt; ret = original.MoveNext();     &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;if&lt;/span&gt; (ret)     &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;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; current.Value = original.Current;&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;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;return&lt;/span&gt; ret;     &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; }     &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; T Current     &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; get { &lt;span class="Statement"&gt;return&lt;/span&gt; current.Value; }     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }     &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;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Dispose()     &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; original.Dispose();     &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; current.Dispose();     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }     &lt;br /&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; IEnumerator.Current     &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; get { &lt;span class="Statement"&gt;return&lt;/span&gt; Current; }     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }     &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;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Reset()     &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; NotSupportedException();     &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;I&amp;#39;m going to say it one last time - we&amp;#39;re broken when it comes to disposal. There&amp;#39;s no way of &lt;em&gt;safely&lt;/em&gt; disposing of the original iterator at &amp;quot;just the right time&amp;quot; when everyone&amp;#39;s finished with it. Oh well.&lt;/p&gt;
&lt;p&gt;Other than that, it&amp;#39;s quite simple. This code has the serendipitous property of actually implementing IEnumerator&amp;lt;T&amp;gt; slightly better than C#-compiler-generated implementations from iterator blocks - if you call the Current property without having called MoveNext(), this will throw an InvalidOperationException, just as the documentation says it should. (It doesn&amp;#39;t do the same at the end, admittedly, but that&amp;#39;s fixable if we really wanted to be pedantic.&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;I found this an intriguing little problem. I think there are better ways of solving the bigger picture - a co-ordinator which takes care of disposing exactly once, and which possibly mediates the original iterator etc is probably the way forward... but I enjoyed thinking about the nitty gritty.&lt;/p&gt;
&lt;p&gt;Generally speaking, I prefer the first of these approaches. Thread local variables always feel like a bit of a grotty hack to me - they can be useful, but it&amp;#39;s better to avoid them if you can. It&amp;#39;s interesting to see how an interface can be inherently thread-friendly or not.&lt;/p&gt;
&lt;p&gt;One last word of warning - this code is &lt;em&gt;completely&lt;/em&gt; untested. It builds, and I can&amp;#39;t immediately see why it wouldn&amp;#39;t work, but I&amp;#39;m making no guarantees...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1734632" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/L8XiRzZI8Sc" height="1" width="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/Parallelisation/default.aspx">Parallelisation</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Stack+Overflow/default.aspx">Stack Overflow</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/10/23/iterating-atomically.aspx</feedburner:origLink></item><item><title>Generic collections - relegate to an appendix?</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/w9RwiLDmrwY/generic-collections-relegate-to-an-appendix.aspx</link><pubDate>Fri, 02 Oct 2009 21:00:59 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1729096</guid><dc:creator>skeet</dc:creator><slash:comments>16</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1729096</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1729096</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/10/02/generic-collections-relegate-to-an-appendix.aspx#comments</comments><description>&lt;p&gt;(I tweeted a brief version of this suggestion and the results have been overwhelmingly positive so far, but I thought it would be worth fleshing out anyway.)&lt;/p&gt;  &lt;p&gt;I&amp;#39;m currently editing chapter 3 of C# in Depth. In the first edition, it&amp;#39;s nearly 48 pages long - the longest in the book, and longer than I want it to be.&lt;/p&gt;  &lt;p&gt;One of the sections in there (only 6 pages, admittedly) is a description of various .NET 2.0 collections. However, it&amp;#39;s mostly comparing them with the nongeneric collections from .NET 1.0, which probably isn&amp;#39;t relevant any more. I suspect my readership has now moved on from &amp;quot;I only know C# 1&amp;quot; to &amp;quot;I&amp;#39;ve used C# 2 and I&amp;#39;m reasonably familiar with the framework, but I want to know the details of the language.&amp;quot;&lt;/p&gt;  &lt;p&gt;I propose moving the collections into an appendix. This will mean:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;I&amp;#39;ll cover all versions of .NET, not just 2.0&lt;/li&gt;    &lt;li&gt;It will all be done in a fairly summary form, like the current appendix. (An appendix doesn&amp;#39;t need as much of a narrative structure as a main chapter, IMO.)&lt;/li&gt;    &lt;li&gt;I&amp;#39;ll cover the interfaces as well as the classes - possibly even with pictures (type hierarchies)!&lt;/li&gt;    &lt;li&gt;Chapter 3 can be a bit slimmer (although I&amp;#39;ve been adding a little bit here and there, so I&amp;#39;m not going to save a massive amount)&lt;/li&gt;    &lt;li&gt;It will be easier to find as a quick reference (and I&amp;#39;ll write it in a way which makes it easy to &lt;em&gt;use&lt;/em&gt; as a reference too, hopefully)&lt;/li&gt;    &lt;li&gt;I don&amp;#39;t have to edit it right now :)&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Does this sound like a plan? I don&amp;#39;t know why I didn&amp;#39;t think of it before, but I &lt;em&gt;think&lt;/em&gt; it&amp;#39;s the right move. In particular, it&amp;#39;s in-keeping with the LINQ operator coverage in the existing appendix.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1729096" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/w9RwiLDmrwY" height="1" width="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><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/10/02/generic-collections-relegate-to-an-appendix.aspx</feedburner:origLink></item><item><title>MVP no more</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/S0unFLX36jk/mvp-no-more.aspx</link><pubDate>Thu, 01 Oct 2009 05:21:27 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1728625</guid><dc:creator>skeet</dc:creator><slash:comments>84</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1728625</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1728625</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/10/01/mvp-no-more.aspx#comments</comments><description>&lt;p&gt;It&amp;#39;s with some sadness that I have to announce that as of the start of October, I&amp;#39;m no longer a Microsoft MVP.&lt;/p&gt;  &lt;p&gt;As renewal time came round again, I asked my employer whether it was okay for me to renew, and was advised not to do so. As a result, while I enjoyed being awarded as an MVP, I&amp;#39;ve asked not to be considered for renewal this year.&lt;/p&gt;  &lt;p&gt;This doesn&amp;#39;t mean I&amp;#39;m turning my back on that side of software development, of course. I&amp;#39;m still going to be an active member of the C# community. I&amp;#39;m still writing the second edition of C# in Depth. I&amp;#39;m still going to post on Stack Overflow. I&amp;#39;m still going to blog here about whatever interesting and wacky topics crop up.&lt;/p&gt;  &lt;p&gt;I just won&amp;#39;t be doing so as an MVP.&lt;/p&gt;  &lt;p&gt;Thanks to all the friends I&amp;#39;ve made in the MVP community and Microsoft over the last 6 years, and I wish you all the best.&lt;/p&gt;  &lt;p&gt;Keep in touch.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1728625" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/S0unFLX36jk" height="1" width="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/General/default.aspx">General</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/10/01/mvp-no-more.aspx</feedburner:origLink></item><item><title>An object lesson in blogging and accuracy; was: Efficient "vote counting" with LINQ to Objects - and the value of nothing</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/cIUpZ1eybMo/efficient-quot-vote-counting-quot-with-linq-to-objects-and-the-value-of-nothing.aspx</link><pubDate>Sun, 20 Sep 2009 20:48:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1725262</guid><dc:creator>skeet</dc:creator><slash:comments>11</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1725262</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1725262</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/09/20/efficient-quot-vote-counting-quot-with-linq-to-objects-and-the-value-of-nothing.aspx#comments</comments><description>&lt;p&gt;Well, this is embarrassing.&lt;/p&gt;  &lt;p&gt;Yesterday evening, I excitedly wrote a blog post about an interesting little idea for making a particular type of LINQ query (basically vote counting) efficient. It was an idea that had occurred to me a few months back, but I hadn&amp;#39;t got round to blogging about it.&lt;/p&gt;  &lt;p&gt;The basic idea was to take a completely empty struct, and use that as the element type in the results of a grouping query - as the struct was empty, it would take no space, therefore &amp;quot;huge&amp;quot; arrays could be created for no cost beyond the fixed array overhead, etc. I carefully checked that the type used for grouping did in fact implement ICollection&amp;lt;T&amp;gt; so that the Count method would be efficient; I wrote sample code which made sure my queries were valid... but I failed to check that the empty struct &lt;em&gt;really&lt;/em&gt; took up no memory.&lt;/p&gt;  &lt;p&gt;Fortunately, I have smart readers, a number of whom pointed out my mistake in very kind terms.&lt;/p&gt;  &lt;p&gt;Ben Voigt gave the reason for the size being 1 in a comment:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;The object identity rules require a unique address for each instance... identity can be shared with super- or sub- class objects (Empty Base Optimization) but the total size of the instance has to be at least 1.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This makes perfect sense - it&amp;#39;s just a shame I didn&amp;#39;t realise it before.&lt;/p&gt;  &lt;p&gt;Live and learn, I guess - but apologies for the poorly researched post. I&amp;#39;ll attempt to be more careful next time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1725262" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/cIUpZ1eybMo" height="1" width="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/LINQ/default.aspx">LINQ</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/09/20/efficient-quot-vote-counting-quot-with-linq-to-objects-and-the-value-of-nothing.aspx</feedburner:origLink></item><item><title>API design: choosing between non-ideal options</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/kYE8qSyGiEg/api-design-choosing-between-non-ideal-options.aspx</link><pubDate>Mon, 14 Sep 2009 16:38:05 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1723277</guid><dc:creator>skeet</dc:creator><slash:comments>10</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1723277</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1723277</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/09/14/api-design-choosing-between-non-ideal-options.aspx#comments</comments><description>&lt;p&gt;So, &lt;a href="http://code.google.com/p/unconstrained-melody"&gt;UnconstrainedMelody&lt;/a&gt; is coming on quite nicely. It now has quite a few useful options for flags enums, &amp;quot;normal enums&amp;quot; and delegates. However, there are two conflicting limitations which leave a couple of options. (Other related answers on Stack Overflow have suggested alternative approaches, basically.)&lt;/p&gt;  &lt;p&gt;Currently, most of the enums code is in two classes: Flags and Enums. Both are non-generic: the methods within them are generic methods, so they have type parameters (and constraints). The main benefit of this is that generic type inference only applies to generic methods, and I &lt;em&gt;definitely&lt;/em&gt; want that for extension methods and anywhere else it makes sense.&lt;/p&gt;  &lt;p&gt;The drawback is that properties can&amp;#39;t be generic. That means my API is entirely expressed in terms of methods, which can be a pain. The option to work around this is to have a generic type which properties in. This adds confusion and guesswork - what call is where?&lt;/p&gt;  &lt;p&gt;To recap, the options are:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Option 1 (current): all methods in a nongeneric class:&lt;/span&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// Some calls which are logically properties end up&lt;/span&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// as methods...&lt;/span&gt;    &lt;br /&gt;IList&amp;lt;Foo&amp;gt; foos = Enums.GetValues&amp;lt;Foo&amp;gt;();    &lt;br /&gt;&lt;span class="InlineComment"&gt;// Type infererence for extenion methods&lt;/span&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// Note that we couldn&amp;#39;t have a Description property&lt;/span&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// as we don&amp;#39;t have extension properties&lt;/span&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; firstDescription = foos[0].GetDescription();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;span class="InlineComment"&gt;// Option 2: Use just a generic type:&lt;/span&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// Now we can use a property...&lt;/span&gt;    &lt;br /&gt;IList&amp;lt;Foo&amp;gt; foos = Enums&amp;lt;Foo&amp;gt;.Values;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// But we can&amp;#39;t use type inference&lt;/span&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; firstDescription = Enums&amp;lt;Foo&amp;gt;.GetDescription(foos[0]);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;span class="InlineComment"&gt;// Option 3: Use a mixture (Enums and Enums&amp;lt;T&amp;gt;):&lt;/span&gt;    &lt;br /&gt;IList&amp;lt;Foo&amp;gt; foos = Enums&amp;lt;Foo&amp;gt;.Values;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// All looks good...&lt;/span&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; firstDescription = foos[0].GetDescription();    &lt;br /&gt;&lt;span class="InlineComment"&gt;// ... but the user has to know when to use which class&lt;/span&gt; &lt;/div&gt;  &lt;p&gt;All of these are somewhat annoying. If we &lt;em&gt;only &lt;/em&gt;put extension methods into the nongeneric class, then I guess users would never need to really think about that - they&amp;#39;d pretty much always be calling the methods via the extension method syntactic sugar anyway. It still feels like a pretty arbitrary split though.&lt;/p&gt;  &lt;p&gt;Any thoughts? Which is more important - conceptual complexity, or the idiomatic client code you end up with once that complexity has been mastered? Is it reasonable to make design decisions like this around what is essentially a single piece of syntactic sugar (extension methods)?&lt;/p&gt;  &lt;p&gt;(By the way, if anyone ever wanted justification for extension properties, I think this is a good example... Description feels like it really &lt;em&gt;should&lt;/em&gt; be a property.)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1723277" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/kYE8qSyGiEg" height="1" width="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/Stack+Overflow/default.aspx">Stack Overflow</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Evil+Code/default.aspx">Evil Code</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/09/14/api-design-choosing-between-non-ideal-options.aspx</feedburner:origLink></item><item><title>Generic constraints for enums and delegates</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/GBOJ7KBN-Gk/generic-constraints-for-enums-and-delegates.aspx</link><pubDate>Thu, 10 Sep 2009 20:09:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1722426</guid><dc:creator>skeet</dc:creator><slash:comments>54</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1722426</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1722426</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/09/10/generic-constraints-for-enums-and-delegates.aspx#comments</comments><description>&lt;p&gt;As most readers probably know, C# prohibits generic type constraints from referring to System.Object, System.Enum, System.Array, System.Delegate and System.ValueType. In other words, this method declaration is illegal:&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; T[]GetValues&amp;lt;T&amp;gt;() &lt;span class="Linq"&gt;where&lt;/span&gt; T : struct, System.Enum&lt;br /&gt;{     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; (T[]) Enum.GetValues(&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(T));     &lt;br /&gt;} &lt;/div&gt;
&lt;p&gt;This is a pity, as such a method could be useful. (In fact there are better things we can do... such as returning a read-only collection. That way we don&amp;#39;t have to create a new array each time the method is called.) As far as I can tell, there is no reason why this should be prohibited. Eric Lippert has stated that he believes &lt;a href="http://stackoverflow.com/questions/1331739/enum-type-constraints-in-c/1331811#1331811"&gt;the CLR doesn&amp;#39;t support this&lt;/a&gt; - but I think he&amp;#39;s wrong. I can&amp;#39;t remember the last time I had cause to believe Eric to be wrong about something, and I&amp;#39;m somewhat nervous of even mentioning it, but section 10.1.7 of the CLI spec (&lt;a href="http://www.ecma-international.org/publications/standards/Ecma-335.htm"&gt;ECMA-335&lt;/a&gt;) partition II (p40) &lt;em&gt;specifically&lt;/em&gt; gives examples of type parameter constraints involving System.Delegate and System.Enum. It introduces the table with &amp;quot;The following table shows the valid combinations of type and special constraints for a representative set of types.&amp;quot; It was only due to reading this table that I realized that the value type constraint on the above is required (or a constructor constraint would do equally well) - otherwise System.Enum itself satisfies the constraint, which would be a Bad Thing.&lt;/p&gt;
&lt;p&gt;It&amp;#39;s possible (but unlikely) that the CLI doesn&amp;#39;t fully implement this part of the CLR spec. I&amp;#39;m hoping that Eric&amp;#39;s just wrong on this occasion, and that actually there&amp;#39;s nothing to stop the C# language from allowing such constraints in the future. (It would be nice to get keyword support, such that a constraint of &amp;quot;T : enum&amp;quot; would be equivalent to the above, but hey...)&lt;/p&gt;
&lt;p&gt;The good news is that ilasm/ildasm have no problem with this. The better news is that if you add a reference to a library which uses those constraints, the C# compiler applies them sensibly, as far as I can tell...&lt;/p&gt;
&lt;h3&gt;Introducing UnconstrainedMelody&lt;/h3&gt;
&lt;p&gt;(Okay, the name will almost surely have to change. But I like the idea of it removing the constraints of C# around which constraints are valid... and yet still being in the key of C#. Better suggestions welcome.)&lt;/p&gt;
&lt;p&gt;I have a plan - I want to write a utility library which does useful things for enums and delegates (and arrays if I can think of anything sensible to do with them). It will be written in C#, with methods 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; T[]GetValues&amp;lt;T&amp;gt;() &lt;span class="Linq"&gt;where&lt;/span&gt; T : struct, IEnumConstraint&lt;br /&gt;{     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; (T[]) Enum.GetValues(&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(T));     &lt;br /&gt;} &lt;/div&gt;
&lt;p&gt;(IEnumConstraint has to be an interface of course, as otherwise the constraint would be invalid.)&lt;/p&gt;
&lt;p&gt;As a post-build step, I will:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Run ildasm on the resulting binary&lt;/li&gt;
&lt;li&gt;Replace every constraint using EnumConstraint with System.Enum&lt;/li&gt;
&lt;li&gt;Run ilasm to build the binary again&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If anyone has a &lt;em&gt;simple&lt;/em&gt; binary rewriter (I&amp;#39;ve looked at &lt;a href="http://postsharp.org"&gt;PostSharp&lt;/a&gt; and &lt;a href="http://cciast.codeplex.com/license"&gt;CCI&lt;/a&gt;; both look &lt;em&gt;way&lt;/em&gt; more complicated than the above) which would do this, that would be great. Otherwise ildasm/ilasm will be fine. It&amp;#39;s not like &lt;em&gt;consumers&lt;/em&gt; will need to perform this step.&lt;/p&gt;
&lt;p&gt;As soon as the name is finalized I&amp;#39;ll add a project on Google Code. Once the infrastructure is in place, adding utility methods should be very straightforward. Suggestions for utility methods would be useful, or just join the project when it&amp;#39;s up and running.&lt;/p&gt;
&lt;p&gt;Am I being silly? Have I overlooked something?&lt;/p&gt;
&lt;h3&gt;A couple of hours later...&lt;/h3&gt;
&lt;p&gt;Okay, I decided not to wait for a better name. The first cut - which does basically nothing but validate the idea, and the fact that I can still unit test it - is in. The &lt;a href="http://code.google.com/p/unconstrained-melody"&gt;UnconstrainedMelody Google Code project&lt;/a&gt; is live!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1722426" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/GBOJ7KBN-Gk" height="1" width="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/Stack+Overflow/default.aspx">Stack Overflow</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/09/10/generic-constraints-for-enums-and-delegates.aspx</feedburner:origLink></item><item><title>Recent activities</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/MAfatkTkv4c/recent-activities.aspx</link><pubDate>Thu, 03 Sep 2009 23:08:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1720570</guid><dc:creator>skeet</dc:creator><slash:comments>15</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1720570</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1720570</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/09/04/recent-activities.aspx#comments</comments><description>&lt;p&gt;It&amp;#39;s been a little while since I&amp;#39;ve blogged, and quite a lot has been going on. In fact, there are a few things I&amp;#39;d have blogged about already if it weren&amp;#39;t for &amp;quot;things&amp;quot; getting in the way.&lt;/p&gt;
&lt;p&gt;Rather than writing a whole series of very short blog posts, I thought I&amp;#39;d wrap them all up here...&lt;/p&gt;
&lt;h3&gt;C# in Depth: next MEAP drop available soon - Code Contracts&lt;/h3&gt;
&lt;p&gt;Thanks to everyone who gave feedback on my &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2009/08/05/tricky-decisions-code-contracts-and-parallel-extensions-in-c-in-depth-2nd-edition.aspx"&gt;writing dilemma&lt;/a&gt;. For the moment, the plan is to have a whole chapter about Code Contracts, but &lt;em&gt;not&lt;/em&gt; include a chapter about Parallel Extensions. My argument for making this decision is that Code Contracts really change the &lt;em&gt;feel&lt;/em&gt; of the code, making it almost like a language feature - and its applicability is almost ubiquitous, unlike PFX.&lt;/p&gt;
&lt;p&gt;I &lt;em&gt;may&lt;/em&gt; write a PFX chapter as a separate download, but I&amp;#39;m sensitive to those who (like me) appreciate slim books. I don&amp;#39;t want to &amp;quot;bulk out&amp;quot; the book with extra topics.&lt;/p&gt;
&lt;p&gt;The Code Contracts chapter is in the final stages before becoming available to MEAP subscribers. (It&amp;#39;s been &amp;quot;nearly ready&amp;quot; for a couple of weeks, but I&amp;#39;ve been on holiday, amongst other things.) After that, I&amp;#39;m going back to the existing chapters and revising them.&lt;/p&gt;
&lt;h3&gt;Talking in Dublin - C# 4 and Parallel Extensions&lt;/h3&gt;
&lt;p&gt;Last week I gave two talks in Dublin at &lt;a href="http://epicenter.ie/"&gt;Epicenter&lt;/a&gt;. One was on C# 4, and the other on Code Contracts and Parallel Extensions. Both are now available in a slightly odd form on the &lt;a href="http://csharpindepth.com/Talks.aspx"&gt;Talks page&lt;/a&gt; of the C# in Depth web site. I no longer write &amp;quot;formal&amp;quot; PowerPoint slides, so the downloads are for simple bullet points of text, along with silly hand-drawn slides. No code yet - I want to tidy it up a bit before including it.&lt;/p&gt;
&lt;h3&gt;Podcasting with The Connected Show&lt;/h3&gt;
&lt;p&gt;I recently recorded a &lt;a href="http://www.lyalin.com/Blog/archive/2009/09/01/connected-show-15-ndash-c-4-it-ainrsquot-that-complex.aspx"&gt;podcast episode&lt;/a&gt; with &lt;a href="http://www.connectedshow.com/"&gt;The Connected Show&lt;/a&gt;. I&amp;#39;m &amp;quot;on&amp;quot; for the second 2/3 of the show - about an hour of me blathering on about the new features of C# 4. If you can understand generic variance just by listening to me talking about it, you&amp;#39;re a smart cookie ;)&lt;/p&gt;
&lt;p&gt;(Oh, and if you like it, please express your amusement on &lt;a href="http://digg.com/microsoft/Connected_Show_15_Jon_Skeet_goes_DEEP_on_C_4_0"&gt;Digg&lt;/a&gt; / &lt;a href="http://www.dzone.com/links/connected_show_15_jon_skeet_goes_deep_on_c_40.html"&gt;DZone&lt;/a&gt; / &lt;a href="http://dotnetshoutout.com/Connected-Show-15-Jon-Skeet-goes-DEEP-on-C-40"&gt;Shout&lt;/a&gt; / &lt;a href="http://www.dotnetkicks.com/csharp/Connected_Show_15_Jon_Skeet_goes_DEEP_on_C_4_0"&gt;Kicks&lt;/a&gt;.)&lt;/p&gt;
&lt;h3&gt;Finishing up with Functional Programming for the Real World&lt;/h3&gt;
&lt;p&gt;Well, this hasn&amp;#39;t been taking much of my time recently (I bowed out of all the indexing etc!) but &lt;a href="http://manning.com/petricek"&gt;Functional Programming for the Real World&lt;/a&gt; is nearly ready to go. Hard copy should be available in the next couple of months... it&amp;#39;ll be really nice to see how it fares. Much kudos to Tomas for all his hard work - I&amp;#39;ve really just been helping out a little.&lt;/p&gt;
&lt;h3&gt;Starting on Groovy in Action, 2nd edition&lt;/h3&gt;
&lt;p&gt;No sooner does one book finish than another one starts. The &lt;a href="http://manning.com/koenig2/"&gt;second edition of Groovy in Action&lt;/a&gt; is in the works, which should prove interesting. To be honest, I haven&amp;#39;t played with Groovy much since the first edition of the book was finished, so it&amp;#39;ll be interesting to see what&amp;#39;s happened to the language in the meantime. I&amp;#39;ll be applying the same sort of spit and polish that I did in the first edition, and asking appropriately ignorant questions of the other authors.&lt;/p&gt;
&lt;h3&gt;Tech Reviewing C# 4.0 in a Nutshell&lt;/h3&gt;
&lt;p&gt;&lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/03/31/book-review-c-3-0-in-a-nutshell.aspx"&gt;I liked C# 3.0 in a Nutshell&lt;/a&gt;, and I feel honoured that Joe asked me to be a tech reviewer for the next edition, which promises to be even better. There&amp;#39;s not a lot more I can say about it at the moment, other than it&amp;#39;ll be out in 2010 - and I still feel that C# in Depth is a good companion book.&lt;/p&gt;
&lt;h3&gt;MoreLINQ now at 1.0 beta&lt;/h3&gt;
&lt;p&gt;A while ago I started the &lt;a href="http://code.google.com/p/morelinq/"&gt;MoreLINQ project&lt;/a&gt;, and it gained some developers with more time than I&amp;#39;ve got available :) Basically the idea is to add some more useful LINQ extension methods to LINQ to Object. Thanks to Atif Aziz, the first beta version has been released. This doesn&amp;#39;t mean we&amp;#39;re &amp;quot;done&amp;quot; though - just that we think we&amp;#39;ve got something useful. Any suggestions for other operators would be welcome.&lt;/p&gt;
&lt;h3&gt;Manning Pop Quiz and discounts&lt;/h3&gt;
&lt;p&gt;While I&amp;#39;m plugging books etc, it&amp;#39;s worth mentioning the &lt;a href="http://www.manning.com/popquiz/"&gt;Manning Pop Quiz&lt;/a&gt; - multiple choice questions on a wide variety of topics. Fabulous prizes available, as well as one-day discounts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Monday, Sept 7th: 50% of all print books (code: pop0907)&lt;/li&gt;
&lt;li&gt;Monday, Sept 14: 50% off all ebooks&amp;nbsp; (code: pop0914)&lt;/li&gt;
&lt;li&gt;Thursday, Sept 17: $25 for C# in Depth, 2nd Edition MEAP print version (code: pop0917) + C# Pop Quiz question&lt;/li&gt;
&lt;li&gt;Monday, Sept 21: 50% off all books&amp;nbsp; (code: pop0921)&lt;/li&gt;
&lt;li&gt;Thursday, Sept 24: $12 for C# in Depth, 2nd Edition MEAP ebook (code: pop0924) + another C# Pop Quiz question&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Future speaking engagements&lt;/h3&gt;
&lt;p&gt;On September 16th I&amp;#39;m going to be speaking to &lt;a href="http://edgeug.net/"&gt;Edge UG&lt;/a&gt; (formerly Vista Squad) in London about Code Contracts and Parallel Extensions. I&amp;#39;m already &lt;em&gt;very&lt;/em&gt; much looking forward to the &lt;a href="http://stackoverflow.carsonified.com/events/london/"&gt;Stack Overflow DevDays London conference&lt;/a&gt; on October 28th, at which I&amp;#39;ll be talking about how humanity has screwed up computing.&lt;/p&gt;
&lt;h3&gt;Future potential blog posts&lt;/h3&gt;
&lt;p&gt;Some day I may get round to writing about:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Revisiting StaticRandom with ThreadLocal&amp;lt;T&amp;gt;&lt;/li&gt;
&lt;li&gt;Volatile doesn&amp;#39;t mean what I thought it did&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There&amp;#39;s a lot more writing than coding in that list... I&amp;#39;d like to spend some more time on &lt;a href="http://code.google.com/p/minibench/"&gt;MiniBench&lt;/a&gt; at some point, but you know what deadlines are like.&lt;/p&gt;
&lt;p&gt;Anyway, that&amp;#39;s what I&amp;#39;ve been up to and what I&amp;#39;ll be doing for a little while...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1720570" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/MAfatkTkv4c" height="1" width="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/General/default.aspx">General</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/C_2300_+4/default.aspx">C# 4</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Parallelisation/default.aspx">Parallelisation</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Speaking+engagements/default.aspx">Speaking engagements</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Stack+Overflow/default.aspx">Stack Overflow</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/09/04/recent-activities.aspx</feedburner:origLink></item><item><title>The "dream book" for C# and .NET</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/OBB7jXxD63w/the-quot-dream-book-quot-for-c-and-net.aspx</link><pubDate>Thu, 20 Aug 2009 16:57:40 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1716905</guid><dc:creator>skeet</dc:creator><slash:comments>33</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1716905</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1716905</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/08/20/the-quot-dream-book-quot-for-c-and-net.aspx#comments</comments><description>&lt;p&gt;This morning I &lt;a href="http://twitter.com/jonskeet/status/3424128643"&gt;showed my hand a little on Twitter&lt;/a&gt;. I&amp;#39;ve had a dream for a long time about the ultimate C# book. It&amp;#39;s a dream based on &lt;a href="http://www.amazon.com/dp/0321356683"&gt;Effective Java&lt;/a&gt;, which is my favourite Java book, along with my experiences of writing C# in Depth.&lt;/p&gt;  &lt;p&gt;Effective Java is written by Josh Bloch, who is an absolute giant in the Java world... and that&amp;#39;s both the problem and the opportunity. There&amp;#39;s no-one of quite the equivalent stature in the .NET world. Instead, there are many very smart people, a lot of whom blog and some of whom have their own books.&lt;/p&gt;  &lt;p&gt;There are &amp;quot;best practices&amp;quot; books, of course: Microsoft&amp;#39;s own Framework Design Guidelines, and Bill Wagner&amp;#39;s &lt;a href="http://www.amazon.com/dp/0321245660"&gt;Effective C#&lt;/a&gt; and &lt;a href="http://www.amazon.com/dp/0321485890"&gt;More Effective C#&lt;/a&gt; being the most obvious examples. I&amp;#39;m in no way trying to knock these books, but I feel we could do even better. The &lt;a href="http://www.amazon.com/dp/0321246756"&gt;Framework Design Guidelines&lt;/a&gt; (also available &lt;a href="http://msdn.microsoft.com/en-us/library/ms229042.aspx"&gt;free to browse on MSDN&lt;/a&gt;) are really about how to create a good API - which is important, but not the be-all-and-end-all for many &lt;em&gt;application&lt;/em&gt; developers who aren&amp;#39;t trying to ship a reusable class library and may well have different concerns. They want to know how to use the &lt;em&gt;language&lt;/em&gt; most effectively, as well as the core types within the framework.&lt;/p&gt;  &lt;p&gt;Bill&amp;#39;s books - and many others which cover the core framework, such as &lt;a href="http://www.amazon.com/dp/0735621632"&gt;CLR via C#&lt;/a&gt;, &lt;a href="http://www.amazon.com/dp/1590598733"&gt;Accelerated C# 2008&lt;/a&gt; and &lt;a href="http://www.amazon.com/dp/0596527578"&gt;C# 3.0 in a Nutshell&lt;/a&gt; - give plenty of advice, but often I&amp;#39;ve felt it&amp;#39;s a little one-sided. Each of these books is the work of a single person (or brothers in the case of Nutshell). Reading them, I&amp;#39;ve often wanted to give present a different point of view - or alternatively, to give a hearty &amp;quot;hear, hear.&amp;quot; I believe that a book giving guidance would benefit greatly from being more of a conversation: where the authors all agree on something, that&amp;#39;s great; where they differ, it would be good to hear about the pros and cons of various approaches. The reader can then weigh up those factors as they apply to each particular real-world scenario.&lt;/p&gt;  &lt;h2&gt;Scope&lt;/h2&gt;  &lt;p&gt;So what would such a book contain? Opinions will vary of course, but &lt;em&gt;I&lt;/em&gt; would like to see:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Effective ways of using language features such as lambda expressions, generic type inference (and indeed generics in general), optional parameters, named arguments and extension methods. Assume that the reader knows &lt;em&gt;roughly&lt;/em&gt; what C# does, but give some extra details around things like iterator blocks and anonymous functions.&lt;/li&gt;    &lt;li&gt;Guidance around class design (in a similar fashion to the FDG, but with more input from others in the community)&lt;/li&gt;    &lt;li&gt;Core framework topics (again, assume the basics are understood):&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Resource management (disposal etc)&lt;/li&gt;      &lt;li&gt;Exceptions&lt;/li&gt;      &lt;li&gt;Collections (including LINQ fundamentals)&lt;/li&gt;      &lt;li&gt;Streams&lt;/li&gt;      &lt;li&gt;Text (including internationalization)&lt;/li&gt;      &lt;li&gt;Numeric types&lt;/li&gt;      &lt;li&gt;Time-related APIs&lt;/li&gt;      &lt;li&gt;Concurrency&lt;/li&gt;      &lt;li&gt;Contracts&lt;/li&gt;      &lt;li&gt;AppDomains&lt;/li&gt;      &lt;li&gt;Security&lt;/li&gt;      &lt;li&gt;Performance&lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;I would prefer to avoid anything around the periphery of .NET (WPF, WinForms, ASP.NET, WCF) - I believe those are better handled in different topics.&lt;/p&gt;  &lt;h2&gt;Obstacles and format&lt;/h2&gt;  &lt;p&gt;There&amp;#39;s one big problem with this idea, but I think it may be a saving grace too. Many of the leading authors work for different publishers. Clearly no single publisher is going to attract &lt;em&gt;all&lt;/em&gt; the best minds in the C# and .NET world. So how could this work in practice? Well...&lt;/p&gt;  &lt;p&gt;Imagine a web site for the book, paid for jointly by all interested publishers. The web site would be the foremost delivery mechanism for the content, both to browse and probably to download in formats appropriate for offline reading (PDF etc). The content would be edited in a collaborative style obviously, but exactly how that would work is a detail to be thrashed out. If you&amp;#39;ve read the annotated C# or CLI specifications, they have about the right feel - opinions can be attributed in places, but not &lt;em&gt;everything&lt;/em&gt; has a label.&lt;/p&gt;  &lt;p&gt;Any contributing publisher could &lt;em&gt;also&lt;/em&gt; take the material and publish it as hard copy if they so wished. Quite how this would work - with potentially multiple hard copy editions of the same content - would be interesting to see. There&amp;#39;s another reason against hard copy ever appearing though, which is that it would be immovable. I&amp;#39;d like to see this work evolve as new features appear and as more best practices are discovered. Publishers could monetize the web site via adverts, possibly according to how much they&amp;#39;re kicking into the site.&lt;/p&gt;  &lt;p&gt;I don&amp;#39;t know how the authors would get paid, admittedly, and that&amp;#39;s another problem. Would this cannibalize the sales of the books listed earlier? It wouldn&amp;#39;t make them redundant - certainly not for the Nutshell type of book, which teaches the basics as well as giving guidance. It would hit Effective C# harder, I suspect - and I apologise to Bill Wagner in advance; if this ever takes off and it hurts his bottom line, I&amp;#39;m very sorry - I think it&amp;#39;s in a good cause though.&lt;/p&gt;  &lt;h2&gt;Dream Team&lt;/h2&gt;  &lt;p&gt;So who would contribute to this? Part of me would like to say &amp;quot;anyone and everyone&amp;quot; in a Wikipedia kind of approach - but I think that practically, it makes sense for industry experts to take their places. (A good feedback/comments mechanism for anyone to use would be crucial, however.) Here&amp;#39;s a list which isn&amp;#39;t meant to be exhaustive, but would make me happy - please don&amp;#39;t take offence if your name isn&amp;#39;t on here but should be, and I wouldn&amp;#39;t expect &lt;em&gt;all&lt;/em&gt; of these people to be interested anyway.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Anders Hejlsberg&lt;/li&gt;    &lt;li&gt;Eric Lippert&lt;/li&gt;    &lt;li&gt;Mads Torgersen&lt;/li&gt;    &lt;li&gt;Don Box&lt;/li&gt;    &lt;li&gt;Brad Abrams&lt;/li&gt;    &lt;li&gt;Krzysztof Cwalina&lt;/li&gt;    &lt;li&gt;Joe Duffy&lt;/li&gt;    &lt;li&gt;Vance Morrison&lt;/li&gt;    &lt;li&gt;Rico Mariani&lt;/li&gt;    &lt;li&gt;Erik Meijer&lt;/li&gt;    &lt;li&gt;Don Symes&lt;/li&gt;    &lt;li&gt;Wes Dyer&lt;/li&gt;    &lt;li&gt;Jeff Richter&lt;/li&gt;    &lt;li&gt;Joe and Ben Albahari&lt;/li&gt;    &lt;li&gt;Andrew Troelsen&lt;/li&gt;    &lt;li&gt;Bill Wagner&lt;/li&gt;    &lt;li&gt;Trey Nash&lt;/li&gt;    &lt;li&gt;Mark Michaelis&lt;/li&gt;    &lt;li&gt;Jon Skeet (yeah, I want to contribute if I can)&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I imagine &amp;quot;principal&amp;quot; authors for specific topics (e.g. Joe Duffy for concurrency) but with all the authors dropping in comments in other places too.&lt;/p&gt;  &lt;h2&gt;Dream or reality?&lt;/h2&gt;  &lt;p&gt;I have no idea whether this will ever happen or not. I&amp;#39;d dearly love it to, and I&amp;#39;ve spoken to a few people before today who&amp;#39;ve been encouraging about the idea. I haven&amp;#39;t been putting any work into getting it off the ground - don&amp;#39;t worry, it&amp;#39;s not been delaying the second edition of C# in Depth. One day though, one day...&lt;/p&gt;  &lt;p&gt;Am I being hopelessly naïve to even consider such a venture? Is the scope too broad? Is the content valuable but not money-making? We&amp;#39;ll see.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1716905" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/OBB7jXxD63w" height="1" width="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/Books/default.aspx">Books</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/08/20/the-quot-dream-book-quot-for-c-and-net.aspx</feedburner:origLink></item><item><title>Tricky decisions... Code Contracts and Parallel Extensions in C# in Depth 2nd edition</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/IoXFdd6x71o/tricky-decisions-code-contracts-and-parallel-extensions-in-c-in-depth-2nd-edition.aspx</link><pubDate>Wed, 05 Aug 2009 16:16:15 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1713522</guid><dc:creator>skeet</dc:creator><slash:comments>83</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1713522</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1713522</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/08/05/tricky-decisions-code-contracts-and-parallel-extensions-in-c-in-depth-2nd-edition.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;d like some feedback from readers, and I suspect my blog is the simplest way to get it.&lt;/p&gt;  &lt;p&gt;I&amp;#39;m currently writing chapter 15 of C# in Depth, tentatively about Code Contracts and Parallel Extensions. The problem is that I&amp;#39;m 15 pages in, and I haven&amp;#39;t finished Code Contracts yet. I suspect that with a typesetter moving the listings around a little it can be shortened a little bit, but I&amp;#39;m still concerned. With the amount I&amp;#39;ve still got to write, Code Contracts is going to end up at 20 pages and I expect Parallel Extensions may be 25. That makes for a pretty monstrous chapter for non-language features.&lt;/p&gt;  &lt;p&gt;I&amp;#39;d like to present a few options:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Keep going as I am, and take the hit of having a big chapter. I&amp;#39;m not going into huge amounts of detail anyway, but the bigger point is to demonstrate how code isn&amp;#39;t what it used to be. We&amp;#39;re no longer writing a simple series of statements to be executed in order. Code Contracts changes this dramatically with the binary rewriter, and Parallel Extensions adjusts the parallelism, and ironically makes it easier to write asynchronous code &lt;em&gt;as if&lt;/em&gt; it were executed sequentially.&lt;/li&gt;    &lt;li&gt;Try to whittle the material down to my original target of around 35 pages. This means it&amp;#39;ll be a really cursory glance at each of the technologies - I&amp;#39;m unsure of how useful it would be at all at that point.&lt;/li&gt;    &lt;li&gt;Don&amp;#39;t even claim to give enough information to really get people going with the new technologies, but possibly introduce extra ones as well, such as &lt;a href="http://www.postsharp.org/"&gt;PostSharp&lt;/a&gt;. Build the theme of &amp;quot;you&amp;#39;re not writing C# 1 any more&amp;quot; in a stronger sense - zoom back to show the bigger picture while ignoring the details.&lt;/li&gt;    &lt;li&gt;Separate them into different chapters. At this point &lt;em&gt;half&lt;/em&gt; the new chapters would be non-language features, which isn&amp;#39;t great for the focus of the book... but at least they&amp;#39;d be a more reasonable size.&lt;/li&gt;    &lt;li&gt;Ditch the chapters from the book completely, possibly writing them as separate chapters to be available as a mini-ebook companion to the book. (We could possibly include them in the ebook version.) This would make the second edition more focused again and possibly give me a bit more space when revising earlier chapters. However, it does mean there&amp;#39;d only be two full-size new chapters for the second edition. (There&amp;#39;ll be a new &amp;quot;wrapping up&amp;quot; chapter as well for a sense of closure, but I&amp;#39;m not generally counting that.)&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Other suggestions are welcome, of course. I&amp;#39;m not going to claim that we&amp;#39;ll end up doing whatever is suggested here, but I&amp;#39;m sure that popular opinion will influence the final decision.&lt;/p&gt;  &lt;p&gt;Thoughts?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1713522" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/IoXFdd6x71o" height="1" width="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/C_2300_+4/default.aspx">C# 4</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/08/05/tricky-decisions-code-contracts-and-parallel-extensions-in-c-in-depth-2nd-edition.aspx</feedburner:origLink></item><item><title>Evil Code of the Day: variance and overloading</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/a66dKV5dTTc/evil-code-of-the-day-variance-and-overloading.aspx</link><pubDate>Mon, 13 Jul 2009 16:10:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1700112</guid><dc:creator>skeet</dc:creator><slash:comments>6</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1700112</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1700112</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/07/13/evil-code-of-the-day-variance-and-overloading.aspx#comments</comments><description>&lt;p&gt;(Note that this kind of breakage was &lt;a href="http://blogs.msdn.com/ericlippert/archive/2007/11/02/covariance-and-contravariance-in-c-part-nine-breaking-changes.aspx"&gt;mentioned a long time ago in Eric Lippert&amp;#39;s blog&lt;/a&gt;, although not in this exact form.)&lt;/p&gt;
&lt;p&gt;Whenever a conversion becomes available where it wasn&amp;#39;t before, overload resolution can change its behaviour. From C# 1 to C# 2 this happened due to delegate variance with method group conversions - now the same thing is true for generic variance for interfaces.&lt;/p&gt;
&lt;p&gt;What does the following code print?&lt;/p&gt;
&lt;div class="code"&gt; &lt;span class="Namespace"&gt;using&lt;/span&gt;&amp;nbsp;System;&lt;br /&gt; &lt;span class="Namespace"&gt;using&lt;/span&gt;&amp;nbsp;System.Collections.Generic;&lt;br /&gt; &lt;br /&gt; &lt;span class="ReferenceType"&gt;class&lt;/span&gt;&amp;nbsp;Base&lt;br /&gt; {&lt;br /&gt; &amp;nbsp;&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;&amp;nbsp;Foo(IEnumerable&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;&amp;nbsp;strings)&lt;br /&gt; &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;Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Strings&amp;quot;&lt;/span&gt;);&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; &lt;span class="ReferenceType"&gt;class&lt;/span&gt;&amp;nbsp;Derived&amp;nbsp;:&amp;nbsp;Base&lt;br /&gt; {&lt;br /&gt; &amp;nbsp;&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;&amp;nbsp;Foo(IEnumerable&amp;lt;&lt;span class="ReferenceType"&gt;object&lt;/span&gt;&amp;gt;&amp;nbsp;objects)&lt;br /&gt; &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;Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Objects&amp;quot;&lt;/span&gt;);&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; &lt;span class="ReferenceType"&gt;class&lt;/span&gt;&amp;nbsp;Test&lt;br /&gt; {&lt;br /&gt; &amp;nbsp;&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;&amp;nbsp;Main()&lt;br /&gt; &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;List&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;&amp;nbsp;strings&amp;nbsp;=&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt;&amp;nbsp;List&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;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt;&amp;nbsp;Derived().Foo(strings);&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; } &lt;/div&gt;
&lt;p&gt;The correct answer is &amp;quot;it depends on which version of C# &lt;em&gt;and&lt;/em&gt; .NET framework you&amp;#39;re using.&amp;quot;&lt;/p&gt;
&lt;p&gt;If you&amp;#39;re using C# 4.0 and .NET 4.0, then IEnumerable&amp;lt;T&amp;gt; is covariant: there&amp;#39;s an implicit conversion from IEnumerable&amp;lt;string&amp;gt; to IEnumerable&amp;lt;object&amp;gt;, so the derived overload is used.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;re using C# 4.0 but .NET 3.5 or earlier then the compiler still knows about variance in general, but the interface in the framework doesn&amp;#39;t have the appropriate metadata to indicate it, so there&amp;#39;s no conversion available, and the base class overload is used.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;re using C# 3.0 or earlier then the compiler doesn&amp;#39;t know about generic variance at all, so again the base class overload is used.&lt;/p&gt;
&lt;p&gt;So, this is a breaking change, and a fairly subtle one at that - and unlike the method group conversion in .NET 2.0, the compiler in .NET 4.0 beta 1 doesn&amp;#39;t issue a warning about it. I&amp;#39;ll edit this post when there&amp;#39;s an appropriate Connect ticket about it...&lt;/p&gt;
&lt;p&gt;In general though, I&amp;#39;d say it&amp;#39;s worth avoiding overloading a method declared in a base class unless you really have to. In particular, overloading it using the same number of parameters but more general ones seems to be a recipe for unreadable code.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1700112" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/a66dKV5dTTc" height="1" width="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/C_2300_+4/default.aspx">C# 4</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Evil+Code/default.aspx">Evil Code</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2009/07/13/evil-code-of-the-day-variance-and-overloading.aspx</feedburner:origLink></item></channel></rss>
