<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results for 'app:weblogs' matching tag 'C# 4'</title><link>http://msmvps.com/search/SearchResults.aspx?q=app:weblogs&amp;tag=C%23+4&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tag 'C# 4'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Recent activities</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/09/04/recent-activities.aspx</link><pubDate>Fri, 04 Sep 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1720570</guid><dc:creator>skeet</dc:creator><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;</description></item><item><title>Tricky decisions... Code Contracts and Parallel Extensions in C# in Depth 2nd edition</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/08/05/tricky-decisions-code-contracts-and-parallel-extensions-in-c-in-depth-2nd-edition.aspx</link><pubDate>Wed, 05 Aug 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1713522</guid><dc:creator>skeet</dc:creator><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;</description></item><item><title>Evil Code of the Day: variance and overloading</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/13/evil-code-of-the-day-variance-and-overloading.aspx</link><pubDate>Mon, 13 Jul 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1700112</guid><dc:creator>skeet</dc:creator><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;</description></item><item><title>Faking COM to fool the C# compiler</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/07/faking-com-to-fool-the-c-compiler.aspx</link><pubDate>Tue, 07 Jul 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698645</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;C# 4 has some great features to make programming against COM components &lt;strike&gt;bearable&lt;/strike&gt; fun and exciting. In particular:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;PIA linking allows you to embed just the relevant bits of the Primary Interop Assembly into your own assembly, so the PIA isn&amp;#39;t actually required at execution time &lt;/li&gt;    &lt;li&gt;Named arguments and optional parameters make life &lt;em&gt;much&lt;/em&gt; simpler for APIs like Office which are full of methods with gazillions of parameters &lt;/li&gt;    &lt;li&gt;&amp;quot;ref&amp;quot; removal allows you to pass an argument by value even though the parameter is a by-reference parameter (COM only, folks - don&amp;#39;t worry!) &lt;/li&gt;    &lt;li&gt;Dynamic typing allows you to remove a load of casts by converting every parameter and return type of &amp;quot;object&amp;quot; into &amp;quot;dynamic&amp;quot; (if you&amp;#39;re using PIA linking) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I&amp;#39;m currently writing about these features for &lt;a href="http://manning.com/skeet2"&gt;the book&lt;/a&gt; (don&amp;#39;t forget to &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2009/07/06/books-going-cheap.aspx"&gt;buy it cheap on Friday&lt;/a&gt;) but I&amp;#39;m not really a COM person. I want to be able to see these compiler features at work against a really simple type. Unfortunately, these really are COM-specific features... so we&amp;#39;re going to have to persuade COM that the type really is a COM type.&lt;/p&gt;  &lt;p&gt;I got slightly stuck on this first, but thanks to &lt;a href="http://stackoverflow.com/questions/1093536"&gt;the power of Stack Overflow&lt;/a&gt;, I now have a reasonably complete demo &amp;quot;fake&amp;quot; COM type. It doesn&amp;#39;t do a lot, and in particular it doesn&amp;#39;t have any events, but it&amp;#39;s enough to show the compiler features:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System;     &lt;br /&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System.Runtime.InteropServices;     &lt;br /&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// Required for linking into another assembly (C# 4)&lt;/span&gt;     &lt;br /&gt;[assembly:Guid(&lt;span class="String"&gt;&amp;quot;86ca55e4-9d4b-462b-8ec8-b62e993aeb64&amp;quot;&lt;/span&gt;)]     &lt;br /&gt;[assembly:ImportedFromTypeLib(&lt;span class="String"&gt;&amp;quot;fake.tlb&amp;quot;&lt;/span&gt;)]     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Namespace"&gt;namespace&lt;/span&gt; FakeCom     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; [Guid(&lt;span class="String"&gt;&amp;quot;c3cb8098-0b8f-4a9a-9772-788d340d6ae0&amp;quot;&lt;/span&gt;)]     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; [ComImport, CoClass(&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(FakeImpl))]     &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;interface&lt;/span&gt; FakeComponent     &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;object&lt;/span&gt; MakeMeDynamic(&lt;span class="ReferenceType"&gt;object&lt;/span&gt; arg);     &lt;br /&gt;&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;span class="ValueType"&gt;void&lt;/span&gt; Foo([Optional] &lt;span class="MethodParameter"&gt;ref&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;int&lt;/span&gt; x,     &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;&amp;#160; [Optional] &lt;span class="MethodParameter"&gt;ref&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; y);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; [Guid(&lt;span class="String"&gt;&amp;quot;734e6105-a20f-4748-a7de-2c83d7e91b04&amp;quot;&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="ReferenceType"&gt;class&lt;/span&gt; FakeImpl {}     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;We have an interface representing our COM type, and a class which the interface claims will implement it. Fortunately the compiler doesn&amp;#39;t actually check that, so we can get away with leaving it entirely unimplemented. It&amp;#39;s also worth noting that our optional parameters can be by-reference parameters (which you can&amp;#39;t normally do in C# 4) and we haven&amp;#39;t given them any default values (as those are ignored for COM anyway).&lt;/p&gt;  &lt;p&gt;This is compiled just like any other assembly: &lt;/p&gt;  &lt;div class="code"&gt;csc /target:library FakeCom.cs &lt;/div&gt;  &lt;p&gt;Then we get to use it with a test program: &lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; FakeCom;    &lt;br /&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Test    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main()    &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="InlineComment"&gt;// Yes, that is calling a &amp;quot;constructor&amp;quot; on an interface&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FakeComponent com = &lt;span class="Keyword"&gt;new&lt;/span&gt; FakeComponent();    &lt;br /&gt;&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;span class="InlineComment"&gt;// The boring old fashioned way of calling a method&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; i = 0;    &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; j = &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; com.Foo(&lt;span class="MethodParameter"&gt;ref&lt;/span&gt; i, &lt;span class="MethodParameter"&gt;ref&lt;/span&gt; j);    &lt;br /&gt;&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;span class="InlineComment"&gt;// Look ma, no ref!&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; com.Foo(10, &lt;span class="String"&gt;&amp;quot;Wow!&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&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;span class="InlineComment"&gt;// Who cares about parameter ordering?&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; com.Foo(y: &lt;span class="String"&gt;&amp;quot;Not me&amp;quot;&lt;/span&gt;, x: 0);    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// And the parameters are optional too&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; com.Foo();    &lt;br /&gt;&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;span class="InlineComment"&gt;// The line below only works when linked rather than&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// referenced, as otherwise you need a cast.&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// The compiler treats it as if it both takes and&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// returns a dynamic value.&lt;/span&gt;    &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; value = com.MakeMeDynamic(10);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;This is compiled either in the old &amp;quot;deploy the PIA as well&amp;quot; way (after adding a cast in the last line): &lt;/p&gt;  &lt;div class="code"&gt;csc /r:FakeCom.dll Test.cs &lt;/div&gt;  &lt;p&gt;... or by linking the PIA instead:&lt;/p&gt;  &lt;div class="code"&gt;csc /l:FakeCom.dll Test.cs &lt;/div&gt;  &lt;p&gt;(The difference is just using &lt;code&gt;/l&lt;/code&gt; instead of &lt;code&gt;/r&lt;/code&gt;.)&lt;/p&gt;  &lt;p&gt;When the test code is compiled as a reference, it decompiles in Reflector to this (I&amp;#39;ve added whitespace for clarity):&lt;/p&gt;  &lt;div class="code"&gt;&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="ValueType"&gt;void&lt;/span&gt; Main()    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; FakeComponent component = (FakeComponent) &lt;span class="Keyword"&gt;new&lt;/span&gt; FakeImpl();    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; x = 0;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; y = &lt;span class="Keyword"&gt;null&lt;/span&gt;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; component.Foo(&lt;span class="MethodParameter"&gt;ref&lt;/span&gt; x, &lt;span class="MethodParameter"&gt;ref&lt;/span&gt; y);    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; num2 = 10;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; str3 = &lt;span class="String"&gt;&amp;quot;Wow!&amp;quot;&lt;/span&gt;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; component.Foo(&lt;span class="MethodParameter"&gt;ref&lt;/span&gt; num2, &lt;span class="MethodParameter"&gt;ref&lt;/span&gt; str3);    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; str4 = &lt;span class="String"&gt;&amp;quot;Not me&amp;quot;&lt;/span&gt;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; num3 = 0;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; component.Foo(&lt;span class="MethodParameter"&gt;ref&lt;/span&gt; num3, &lt;span class="MethodParameter"&gt;ref&lt;/span&gt; str4);    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; num4 = 0;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; str5 = &lt;span class="Keyword"&gt;null&lt;/span&gt;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; component.Foo(&lt;span class="MethodParameter"&gt;ref&lt;/span&gt; num4, &lt;span class="MethodParameter"&gt;ref&lt;/span&gt; str5);    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; str2 = (&lt;span class="ReferenceType"&gt;string&lt;/span&gt;) component.MakeMeDynamic(10);    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Note how the compiler has created local variables to pass by reference; any changes to the parameter are ignored when the method returns. (If you actually pass a variable by reference, the compiler won&amp;#39;t take that away, however.) &lt;/p&gt;  &lt;p&gt;When the code is linked instead, the middle section is the same, but the construction and the line calling &lt;code&gt;MakeMeDynamic&lt;/code&gt; are very different:&lt;/p&gt;  &lt;div class="code"&gt;&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="ValueType"&gt;void&lt;/span&gt; Main()    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; FakeComponent component = (FakeComponent) Activator.CreateInstance(Type.GetTypeFromCLSID    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (&lt;span class="Keyword"&gt;new&lt;/span&gt; Guid(&lt;span class="String"&gt;&amp;quot;734E6105-A20F-4748-A7DE-2C83D7E91B04&amp;quot;&lt;/span&gt;)));    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Middle bit as before&lt;/span&gt;    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (&amp;lt;Main&amp;gt;o__SiteContainer6.&amp;lt;&amp;gt;p__Site7 == &lt;span class="Keyword"&gt;null&lt;/span&gt;)    &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; &amp;lt;Main&amp;gt;o__SiteContainer6.&amp;lt;&amp;gt;p__Site7 = CallSite&amp;lt;Func&amp;lt;CallSite, &lt;span class="ReferenceType"&gt;object&lt;/span&gt;, &lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;&amp;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; .Create(&lt;span class="Keyword"&gt;new&lt;/span&gt; CSharpConvertBinder    &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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(&lt;span class="ReferenceType"&gt;string&lt;/span&gt;),&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CSharpConversionKind.ImplicitConversion, &lt;span class="Keyword"&gt;false&lt;/span&gt;));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; str2 = &amp;lt;Main&amp;gt;o__SiteContainer6.&amp;lt;&amp;gt;p__Site7.Target.Invoke    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (&amp;lt;Main&amp;gt;o__SiteContainer6.&amp;lt;&amp;gt;p__Site7, component.MakeMeDynamic(10));    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;The interface is embedded in the generated assembly, but with a slightly different set of attributes:&lt;/p&gt;  &lt;div class="code"&gt;[ComImport, CompilerGenerated]   &lt;br /&gt;[Guid(&lt;span class="String"&gt;&amp;quot;C3CB8098-0B8F-4A9A-9772-788D340D6AE0&amp;quot;&lt;/span&gt;), TypeIdentifier]    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; FakeComponent    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;object&lt;/span&gt; MakeMeDynamic(&lt;span class="ReferenceType"&gt;object&lt;/span&gt; arg);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;void&lt;/span&gt; Foo([Optional] &lt;span class="MethodParameter"&gt;ref&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;int&lt;/span&gt; x, [Optional] &lt;span class="MethodParameter"&gt;ref&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; y);    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;The class isn&amp;#39;t present at all.&lt;/p&gt;  &lt;p&gt;I should point out that doing this has no practical benefit in real code - but the ability to mess around with a pseudo-COM type rather than having to find a &lt;em&gt;real&lt;/em&gt; one with the exact members I want will make it a lot easier to try a few corner cases for the book.&lt;/p&gt;  &lt;p&gt;So, not a terribly productive evening in terms of getting actual writing done, but interesting nonetheless...&lt;/p&gt;</description></item><item><title>Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx</link><pubDate>Fri, 03 Jul 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697785</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;At a glance, this code doesn&amp;#39;t look particularly evil. What does it do though? Compile it with the C# 4.0b1 compiler and run it...&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System;     &lt;br /&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Base     &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="Modifier"&gt;virtual&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Foo(&lt;span class="ValueType"&gt;int&lt;/span&gt; x, &lt;span class="ValueType"&gt;int&lt;/span&gt; y)     &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; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Base: x={0}, y={1}&amp;quot;&lt;/span&gt;, x, y);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Derived : Base     &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="Modifier"&gt;override&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Foo(&lt;span class="ValueType"&gt;int&lt;/span&gt; y, &lt;span class="ValueType"&gt;int&lt;/span&gt; x)     &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; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Derived: x={0}, y={1}&amp;quot;&lt;/span&gt;, x, y);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; PureEvil     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main()     &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; Derived d = &lt;span class="Keyword"&gt;new&lt;/span&gt; Derived();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Base b = d;     &lt;br /&gt;&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; b.Foo(x: 10, y: 20);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; d.Foo(x: 10, y: 20);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;The results are:&lt;/p&gt;  &lt;div class="output"&gt;Derived: x=20, y=10    &lt;br /&gt;Derived: x=10, y=20&lt;/div&gt;  &lt;p&gt;I&amp;#39;m very nearly tempted to leave it there and just see what the reactions are like, but I&amp;#39;ll at least give you a hint as to where to look - section 21.3 of the &lt;a href="http://download.microsoft.com/download/7/E/6/7E6A548C-9C20-4C80-B3B8-860FAF20887A/CSharp%204.0%20Specification.doc"&gt;C# 4 spec&lt;/a&gt; explains why this gives odd results. It does make perfect sense, but it&amp;#39;s hideously evil.&lt;/p&gt;  &lt;p&gt;I feel dirty.&lt;/p&gt;  &lt;h3&gt;Bonus questions&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;What happens if you rename the parameters in &lt;code&gt;Derived.Foo&lt;/code&gt; to &lt;code&gt;yy&lt;/code&gt; and &lt;code&gt;xx&lt;/code&gt;? &lt;/li&gt;    &lt;li&gt;(As suggested by Mehrdad) What happens if you call it with a dynamic value?&lt;/li&gt; &lt;/ul&gt;</description></item><item><title>OS Jam at Google London: C# 4 and the DLR</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/06/19/os-jam-at-google-london-c-4-and-the-dlr.aspx</link><pubDate>Fri, 19 Jun 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1695865</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;Last night I presented for the first time at the &lt;a href="http://osjam.appspot.com/"&gt;Google Open Source Jam&lt;/a&gt; at our offices in London. The room was packed, but only a very few attendees were C# developers. I know that C# isn&amp;#39;t the most popular language on the Open Source scene, but I was still surprised there weren&amp;#39;t more people using C# for their jobs and hacking on Ruby/Python/etc at night.&lt;/p&gt;
&lt;p&gt;All the talks at OSJam are just 5 minutes long, with 2 minutes for questions. I&amp;#39;m really not used to this format, and felt extremely rushed... however, it was still a lot of fun. I used a somewhat different approach to my slides than the normal &amp;quot;bullet points in PowerPoint&amp;quot; - and as it was only short, I thought I might as well effectively repeat the presentation here in digital form. (Apologies if the images are an inconvenient size for you. I tried a few different ones, and this seemed about right. Comments welcome, as I may do a similar thing in the future.)&lt;/p&gt;
&lt;table border="0" cellspacing="0" cellpadding="2"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image001" border="0" alt="First slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image001.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;Introductory slide. Colleagues forced me to include the &lt;a href="http://askjonskeet.com"&gt;askjonskeet.com&lt;/a&gt; link. &lt;/p&gt;
&lt;br /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image002" border="0" alt="Second slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image002.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;.NET isn&amp;#39;t Open Source. You can &lt;a href="http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx"&gt;debug through a lot of the source code&lt;/a&gt; for the framework if you agree to a &amp;quot;reference licence&amp;quot;, but it&amp;#39;s not quite the same thing.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image003" border="0" alt="Third slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image003.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;.NET isn&amp;#39;t Open Source, but the DLR is. And IronRuby. And IronPython. Yay!&lt;/p&gt;
&lt;p&gt;And of course &lt;a href="http://mono-project.com"&gt;Mono&lt;/a&gt;&amp;nbsp;is Open Source: the DLR and Mono&amp;nbsp;play nicely together, and the Mono team is hoping to implement the new C# 4.0 features for the 2.8 release in roughly the same timeframe as Microsoft.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image004" border="0" alt="Fourth slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image004.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;This is what .NET 4.0 will look like. The DLR will be included in it, despite being open source. IronRuby and IronPython aren&amp;#39;t included, but depend heavily on the DLR. (Currently available versions allow you to use a &amp;quot;standalone&amp;quot; DLR or the one in .NET 4.0b1.) &lt;/p&gt;
&lt;p&gt;C# doesn&amp;#39;t really depend on the DLR except for its handling of &lt;code&gt;dynamic&lt;/code&gt;. C# is a statically typed language, but C# 4.0 has a new static type called &lt;code&gt;dynamic&lt;/code&gt; which you can do just about anything with. (This got a laugh, despite being a simple and mostly accurate summary of the dynamic typing support in C# 4.0.)&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image005" border="0" alt="Fifth slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image005.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;The fundamental point of the DLR is to handle &lt;em&gt;call sites&lt;/em&gt; - decide what to do dynamically with little bits of code. Oh, and do it quickly. That&amp;#39;s what the caches are for. They&amp;#39;re really clever - particularly the L0 cache which compiles rules (about the context in which a particular decision is valid) into IL via dynamic methods. Awesome stuff.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m sure the DLR does many other snazzy things, but this feels like it&amp;#39;s the core part of it. &lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image006" border="0" alt="Sixth slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image006.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;At execution time, the relevant &lt;em&gt;binder&lt;/em&gt; is used to work out what a call site should actually do. Unless, that is, the call has a target which implements the shadowy &lt;code&gt;IDynamicMetaObjectProvider&lt;/code&gt; interface (winner of &amp;quot;biggest mouthful of a type name&amp;quot; prize, 2009) - in which case, the object is asked to handle the call. Who knows what it will do?&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image007" border="0" alt="Seventh slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image007.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;Beautifully syntax-highlighted C# 4.0 source code showing the &lt;code&gt;dynamic&lt;/code&gt; type in action. The method calls on lines 2 and 3 are both dynamic, even though in the latter case it&amp;#39;s just using a static method. Which overload will it pick? It all depends on the type of the actual value at execution time.&lt;/p&gt;
&lt;p&gt;If I&amp;#39;d had more time, I&amp;#39;d have demonstrated how the C# compiler preserves the static type information it knows at compile time for the execution time binder to use. This is very cool, but would take far too long to demonstrate in this talk - especially to a bunch of non-C# developers.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image008" border="0" alt="Eighth slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image008.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;There were a couple of questions, but I can&amp;#39;t remember them offhand. Someone asked me afterwards about how all this worked on non-.NET implementations (i.e. Mono, basically). I gather the DLR itself works, but I don&amp;#39;t know whether C# code compiled in the MS compiler will work at the moment - it embeds references to binder types in Microsoft.CSharp.dll, and I don&amp;#39;t know what the story is about that being supported on Mono.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;This is definitely the format I want to use for future presentations. It&amp;#39;s fun to write, fun to present, and I&amp;#39;m sure the &amp;quot;non-professionalism&amp;quot; of it makes it a lot more interesting to watch. Although it&amp;#39;s slower to create text-like slides (such as the first and the last one) this way, the fact that I don&amp;#39;t need to find clip-art or draw boxes with painful user interfaces is a definite win - especially as I&amp;#39;m going to try to be much more image-biased from now on. (I don&amp;#39;t want people reading slides while I&amp;#39;m talking - they should be listening, otherwise it&amp;#39;s just pointless.)&lt;/p&gt;</description></item><item><title>OS Jam at Google London: C# 4 and the DLR</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/06/19/os-jam-at-google-london-c-4-and-the-dlr.aspx</link><pubDate>Fri, 19 Jun 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1695865</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;Last night I presented for the first time at the &lt;a href="http://osjam.appspot.com/"&gt;Google Open Source Jam&lt;/a&gt; at our offices in London. The room was packed, but only a very few attendees were C# developers. I know that C# isn&amp;#39;t the most popular language on the Open Source scene, but I was still surprised there weren&amp;#39;t more people using C# for their jobs and hacking on Ruby/Python/etc at night.&lt;/p&gt;
&lt;p&gt;All the talks at OSJam are just 5 minutes long, with 2 minutes for questions. I&amp;#39;m really not used to this format, and felt extremely rushed... however, it was still a lot of fun. I used a somewhat different approach to my slides than the normal &amp;quot;bullet points in PowerPoint&amp;quot; - and as it was only short, I thought I might as well effectively repeat the presentation here in digital form. (Apologies if the images are an inconvenient size for you. I tried a few different ones, and this seemed about right. Comments welcome, as I may do a similar thing in the future.)&lt;/p&gt;
&lt;table border="0" cellspacing="0" cellpadding="2"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image001" border="0" alt="First slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image001.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;Introductory slide. Colleagues forced me to include the &lt;a href="http://askjonskeet.com"&gt;askjonskeet.com&lt;/a&gt; link. &lt;/p&gt;
&lt;br /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image002" border="0" alt="Second slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image002.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;.NET isn&amp;#39;t Open Source. You can &lt;a href="http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx"&gt;debug through a lot of the source code&lt;/a&gt; for the framework if you agree to a &amp;quot;reference licence&amp;quot;, but it&amp;#39;s not quite the same thing.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image003" border="0" alt="Third slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image003.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;.NET isn&amp;#39;t Open Source, but the DLR is. And IronRuby. And IronPython. Yay!&lt;/p&gt;
&lt;p&gt;And of course &lt;a href="http://mono-project.com"&gt;Mono&lt;/a&gt;&amp;nbsp;is Open Source: the DLR and Mono&amp;nbsp;play nicely together, and the Mono team is hoping to implement the new C# 4.0 features for the 2.8 release in roughly the same timeframe as Microsoft.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image004" border="0" alt="Fourth slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image004.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;This is what .NET 4.0 will look like. The DLR will be included in it, despite being open source. IronRuby and IronPython aren&amp;#39;t included, but depend heavily on the DLR. (Currently available versions allow you to use a &amp;quot;standalone&amp;quot; DLR or the one in .NET 4.0b1.) &lt;/p&gt;
&lt;p&gt;C# doesn&amp;#39;t really depend on the DLR except for its handling of &lt;code&gt;dynamic&lt;/code&gt;. C# is a statically typed language, but C# 4.0 has a new static type called &lt;code&gt;dynamic&lt;/code&gt; which you can do just about anything with. (This got a laugh, despite being a simple and mostly accurate summary of the dynamic typing support in C# 4.0.)&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image005" border="0" alt="Fifth slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image005.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;The fundamental point of the DLR is to handle &lt;em&gt;call sites&lt;/em&gt; - decide what to do dynamically with little bits of code. Oh, and do it quickly. That&amp;#39;s what the caches are for. They&amp;#39;re really clever - particularly the L0 cache which compiles rules (about the context in which a particular decision is valid) into IL via dynamic methods. Awesome stuff.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m sure the DLR does many other snazzy things, but this feels like it&amp;#39;s the core part of it. &lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image006" border="0" alt="Sixth slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image006.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;At execution time, the relevant &lt;em&gt;binder&lt;/em&gt; is used to work out what a call site should actually do. Unless, that is, the call has a target which implements the shadowy &lt;code&gt;IDynamicMetaObjectProvider&lt;/code&gt; interface (winner of &amp;quot;biggest mouthful of a type name&amp;quot; prize, 2009) - in which case, the object is asked to handle the call. Who knows what it will do?&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image007" border="0" alt="Seventh slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image007.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;Beautifully syntax-highlighted C# 4.0 source code showing the &lt;code&gt;dynamic&lt;/code&gt; type in action. The method calls on lines 2 and 3 are both dynamic, even though in the latter case it&amp;#39;s just using a static method. Which overload will it pick? It all depends on the type of the actual value at execution time.&lt;/p&gt;
&lt;p&gt;If I&amp;#39;d had more time, I&amp;#39;d have demonstrated how the C# compiler preserves the static type information it knows at compile time for the execution time binder to use. This is very cool, but would take far too long to demonstrate in this talk - especially to a bunch of non-C# developers.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image008" border="0" alt="Eighth slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image008.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;There were a couple of questions, but I can&amp;#39;t remember them offhand. Someone asked me afterwards about how all this worked on non-.NET implementations (i.e. Mono, basically). I gather the DLR itself works, but I don&amp;#39;t know whether C# code compiled in the MS compiler will work at the moment - it embeds references to binder types in Microsoft.CSharp.dll, and I don&amp;#39;t know what the story is about that being supported on Mono.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;This is definitely the format I want to use for future presentations. It&amp;#39;s fun to write, fun to present, and I&amp;#39;m sure the &amp;quot;non-professionalism&amp;quot; of it makes it a lot more interesting to watch. Although it&amp;#39;s slower to create text-like slides (such as the first and the last one) this way, the fact that I don&amp;#39;t need to find clip-art or draw boxes with painful user interfaces is a definite win - especially as I&amp;#39;m going to try to be much more image-biased from now on. (I don&amp;#39;t want people reading slides while I&amp;#39;m talking - they should be listening, otherwise it&amp;#39;s just pointless.)&lt;/p&gt;</description></item><item><title>Dynamic type inference and surprising possibilities</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/06/17/dynamic-type-inference-and-surprising-possibilities.aspx</link><pubDate>Wed, 17 Jun 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1695727</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;There have been mutterings about the fact that I haven&amp;#39;t been blogging much recently. I&amp;#39;ve been getting down to serious work on the second edition of C# in Depth, and it&amp;#39;s taking a lot of my time. However, I thought I&amp;#39;d share a ghastly little example I&amp;#39;ve just come up with.&lt;/p&gt;  &lt;p&gt;I&amp;#39;ve been having an email discussion with &lt;a href="http://blogs.msdn.com/samng/"&gt;Sam Ng&lt;/a&gt;, &lt;a href="http://blogs.msdn.com/cburrows/"&gt;Chris Burrows&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/ericlippert/"&gt;Eric Lippert&lt;/a&gt; about how dynamic typing works. Sam mentioned that even for dynamically bound calls, type inference can fail at compile time. This can only happen for type parameters where none of the dynamic values contribute to the type inference. For example, this fails to compile:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Execute&amp;lt;T&amp;gt;(T item, &lt;span class="ValueType"&gt;int&lt;/span&gt; value) &lt;span class="Linq"&gt;where&lt;/span&gt; T : &lt;span class="ValueType"&gt;struct&lt;/span&gt; {}     &lt;br /&gt;...     &lt;br /&gt;&lt;span class="Keyword"&gt;dynamic&lt;/span&gt; guid = Guid.NewGuid();     &lt;br /&gt;Execute(&lt;span class="String"&gt;&amp;quot;test&amp;quot;&lt;/span&gt;, guid); &lt;/div&gt;  &lt;p&gt;Whatever the value of &lt;code&gt;guid&lt;/code&gt; is at execution time, this can&amp;#39;t &lt;i&gt;possibly&lt;/i&gt; manage to infer a valid type argument for &lt;code&gt;T&lt;/code&gt;. The only type which can be inferred is &lt;code&gt;string&lt;/code&gt;, and that&amp;#39;s not a value type. Fair enough... but what about this one?&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Execute&amp;lt;T&amp;gt;(T first, T second, T third) &lt;span class="Linq"&gt;where&lt;/span&gt; T : &lt;span class="ValueType"&gt;struct&lt;/span&gt; {}    &lt;br /&gt;...    &lt;br /&gt;&lt;span class="Keyword"&gt;dynamic&lt;/span&gt; guid = Guid.NewGuid();    &lt;br /&gt;Execute(10, 0, guid);    &lt;br /&gt;Execute(10, &lt;span class="Keyword"&gt;false&lt;/span&gt;, guid);    &lt;br /&gt;Execute(&lt;span class="String"&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;, &lt;span class="String"&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;, guid); &lt;/div&gt;  &lt;p&gt;I expected the first call to compile (but fail at execution time) and the second and third calls to fail at compile time. After all, &lt;code&gt;T&lt;/code&gt; couldn&amp;#39;t be both an &lt;code&gt;int&lt;/code&gt; &lt;i&gt;and&lt;/i&gt; a &lt;code&gt;bool&lt;/code&gt; could it? And then I remembered implicit typing... what if the vaue of &lt;code&gt;guid&lt;/code&gt; isn&amp;#39;t actually a &lt;code&gt;Guid&lt;/code&gt;, but some struct which has an implicit conversion from &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;bool&lt;/code&gt; and &lt;code&gt;string&lt;/code&gt;? In other words, what if the full code actually looked like this: &lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System;    &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;struct&lt;/span&gt; Foo    &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="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;implicit&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;operator&lt;/span&gt; Foo(&lt;span class="ValueType"&gt;int&lt;/span&gt; x)    &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;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; Foo();    &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;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;implicit&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;operator&lt;/span&gt; Foo(&lt;span class="ValueType"&gt;bool&lt;/span&gt; x)    &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;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; Foo();    &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;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;implicit&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;operator&lt;/span&gt; Foo(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; x)    &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;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; Foo();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;}    &lt;br /&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Test    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Execute&amp;lt;T&amp;gt;(T first, T second, T third) &lt;span class="Linq"&gt;where&lt;/span&gt; T : &lt;span class="ValueType"&gt;struct&lt;/span&gt; {}    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main()    &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="Keyword"&gt;dynamic&lt;/span&gt; foo = &lt;span class="Keyword"&gt;new&lt;/span&gt; Foo();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Execute(10, 0, foo);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Execute(10, &lt;span class="Keyword"&gt;false&lt;/span&gt;, foo);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Execute(&lt;span class="String"&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;, &lt;span class="String"&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;, foo);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Then &lt;code&gt;T=Foo&lt;/code&gt; is a perfectly valid inference. So yes, it all compiles - and the C# binders even get it all right at execution time. So much for any intuition I might have about dynamic typing and inference... &lt;/p&gt;  &lt;p&gt;No doubt I&amp;#39;ll have similar posts about new C# 4 features occasionally... but they&amp;#39;re more likely to be explanations of misunderstandings than deep insights into a &lt;i&gt;correct&lt;/i&gt; view of the language. Those end up in the book instead :) &lt;/p&gt;</description></item><item><title>Dynamic type inference and surprising possibilities</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/06/17/dynamic-type-inference-and-surprising-possibilities.aspx</link><pubDate>Wed, 17 Jun 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1695727</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;There have been mutterings about the fact that I haven&amp;#39;t been blogging much recently. I&amp;#39;ve been getting down to serious work on the second edition of C# in Depth, and it&amp;#39;s taking a lot of my time. However, I thought I&amp;#39;d share a ghastly little example I&amp;#39;ve just come up with.&lt;/p&gt;  &lt;p&gt;I&amp;#39;ve been having an email discussion with &lt;a href="http://blogs.msdn.com/samng/"&gt;Sam Ng&lt;/a&gt;, &lt;a href="http://blogs.msdn.com/cburrows/"&gt;Chris Burrows&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/ericlippert/"&gt;Eric Lippert&lt;/a&gt; about how dynamic typing works. Sam mentioned that even for dynamically bound calls, type inference can fail at compile time. This can only happen for type parameters where none of the dynamic values contribute to the type inference. For example, this fails to compile:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Execute&amp;lt;T&amp;gt;(T item, &lt;span class="ValueType"&gt;int&lt;/span&gt; value) &lt;span class="Linq"&gt;where&lt;/span&gt; T : &lt;span class="ValueType"&gt;struct&lt;/span&gt; {}     &lt;br /&gt;...     &lt;br /&gt;&lt;span class="Keyword"&gt;dynamic&lt;/span&gt; guid = Guid.NewGuid();     &lt;br /&gt;Execute(&lt;span class="String"&gt;&amp;quot;test&amp;quot;&lt;/span&gt;, guid); &lt;/div&gt;  &lt;p&gt;Whatever the value of &lt;code&gt;guid&lt;/code&gt; is at execution time, this can&amp;#39;t &lt;i&gt;possibly&lt;/i&gt; manage to infer a valid type argument for &lt;code&gt;T&lt;/code&gt;. The only type which can be inferred is &lt;code&gt;string&lt;/code&gt;, and that&amp;#39;s not a value type. Fair enough... but what about this one?&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Execute&amp;lt;T&amp;gt;(T first, T second, T third) &lt;span class="Linq"&gt;where&lt;/span&gt; T : &lt;span class="ValueType"&gt;struct&lt;/span&gt; {}    &lt;br /&gt;...    &lt;br /&gt;&lt;span class="Keyword"&gt;dynamic&lt;/span&gt; guid = Guid.NewGuid();    &lt;br /&gt;Execute(10, 0, guid);    &lt;br /&gt;Execute(10, &lt;span class="Keyword"&gt;false&lt;/span&gt;, guid);    &lt;br /&gt;Execute(&lt;span class="String"&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;, &lt;span class="String"&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;, guid); &lt;/div&gt;  &lt;p&gt;I expected the first call to compile (but fail at execution time) and the second and third calls to fail at compile time. After all, &lt;code&gt;T&lt;/code&gt; couldn&amp;#39;t be both an &lt;code&gt;int&lt;/code&gt; &lt;i&gt;and&lt;/i&gt; a &lt;code&gt;bool&lt;/code&gt; could it? And then I remembered implicit typing... what if the vaue of &lt;code&gt;guid&lt;/code&gt; isn&amp;#39;t actually a &lt;code&gt;Guid&lt;/code&gt;, but some struct which has an implicit conversion from &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;bool&lt;/code&gt; and &lt;code&gt;string&lt;/code&gt;? In other words, what if the full code actually looked like this: &lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System;    &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;struct&lt;/span&gt; Foo    &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="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;implicit&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;operator&lt;/span&gt; Foo(&lt;span class="ValueType"&gt;int&lt;/span&gt; x)    &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;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; Foo();    &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;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;implicit&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;operator&lt;/span&gt; Foo(&lt;span class="ValueType"&gt;bool&lt;/span&gt; x)    &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;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; Foo();    &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;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;implicit&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;operator&lt;/span&gt; Foo(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; x)    &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;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; Foo();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;}    &lt;br /&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Test    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Execute&amp;lt;T&amp;gt;(T first, T second, T third) &lt;span class="Linq"&gt;where&lt;/span&gt; T : &lt;span class="ValueType"&gt;struct&lt;/span&gt; {}    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main()    &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="Keyword"&gt;dynamic&lt;/span&gt; foo = &lt;span class="Keyword"&gt;new&lt;/span&gt; Foo();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Execute(10, 0, foo);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Execute(10, &lt;span class="Keyword"&gt;false&lt;/span&gt;, foo);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Execute(&lt;span class="String"&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;, &lt;span class="String"&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;, foo);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Then &lt;code&gt;T=Foo&lt;/code&gt; is a perfectly valid inference. So yes, it all compiles - and the C# binders even get it all right at execution time. So much for any intuition I might have about dynamic typing and inference... &lt;/p&gt;  &lt;p&gt;No doubt I&amp;#39;ll have similar posts about new C# 4 features occasionally... but they&amp;#39;re more likely to be explanations of misunderstandings than deep insights into a &lt;i&gt;correct&lt;/i&gt; view of the language. Those end up in the book instead :) &lt;/p&gt;</description></item><item><title>C# 4.0: dynamic&amp;lt;T&amp;gt; ?</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/30/c-4-0-dynamic-lt-t-gt.aspx</link><pubDate>Thu, 30 Oct 2008 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652571</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;I&amp;#39;ve not played with the VS2010 CTP much yet, and I&amp;#39;ve only looked briefly at the documentation and blogs about the new C# 4.0 &lt;code&gt;dynamic&lt;/code&gt; type, but a thought occurred to me: why not have the option of making it generic as a way of saying &amp;quot;I will dynamically support this set of operations&amp;quot;?&lt;/p&gt; &lt;p&gt;As an example of what I mean, suppose you have an interface &lt;code&gt;IMessageRouter&lt;/code&gt; like this:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessageRouter&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ValueType"&gt;void&lt;/span&gt; Send(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; message, &lt;span class="ReferenceType"&gt;string&lt;/span&gt; destination);&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;(This is an arbitrary example, by the way. The idea isn&amp;#39;t specifically more suitable for message routing than anything else.)&lt;/p&gt; &lt;p&gt;I may have various implementations, written in various languages (or COM) which support the &lt;code&gt;Send&lt;/code&gt; method with those parameters. Some of those implementations actually implement &lt;code&gt;IMessageRouter&lt;/code&gt; but some don&amp;#39;t. I&amp;#39;d like to be able to do the following:&lt;/p&gt; &lt;div class="code"&gt;dynamic&amp;lt;IMessageRouter&amp;gt; router = GetRouter();&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// This is fine (but still invoked dynamically)&lt;/span&gt;&lt;br /&gt;router.Send(&lt;span class="String"&gt;&amp;quot;message&amp;quot;&lt;/span&gt;, &lt;span class="String"&gt;&amp;quot;skeet@pobox.com&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;span class="InlineComment"&gt;// Compilation error: no such overload&lt;/span&gt;&lt;br /&gt;router.Send(&lt;span class="String"&gt;&amp;quot;message&amp;quot;&lt;/span&gt;, &lt;span class="String"&gt;&amp;quot;skeet@pobox.com&amp;quot;&lt;/span&gt;, 20); &lt;/div&gt; &lt;p&gt;Intellisense would work, and we&amp;#39;d still have some of the benefits of static typing but without the implementations having to know about your interface. Of course, it would be quite easy to create an implementation of the interface which did exactly this - but now imagine that instead of &lt;code&gt;IMessageRouter&lt;/code&gt; we had &lt;code&gt;MessageRouter&lt;/code&gt; - a concrete class. In this case the compiler would still restrict the caller to the public API of the class, but it wouldn&amp;#39;t have to be the real class. No checking would be performed by the compiler that your dynamic type &lt;i&gt;actually&lt;/i&gt; supported the operations - given that we&amp;#39;re talking about dynamic invocation, that would be impossible to do. It would instead be an &amp;quot;opt-in&amp;quot; restriction the client places on themselves. It could also potentially help with performance - if the binding involved realised that the &lt;i&gt;actual&lt;/i&gt; type of the dynamic object natively implemented the interface or was/derived from the class, then no real dynamic calls need be made; just route all directly. &lt;/p&gt; &lt;p&gt;This may all sound a bit fuzzy - I&amp;#39;m extremely sleepy, to be honest - but I think it&amp;#39;s a potentially interesting idea. Thoughts? &lt;/p&gt; &lt;h2&gt;Update&lt;/h2&gt;Apparently this post wasn&amp;#39;t as clear as it might be. I&amp;#39;m quite happy to keep the currently proposed dynamic type idea &lt;em&gt;as well&lt;/em&gt; - I&amp;#39;d like this as an &lt;em&gt;additional&lt;/em&gt; way of using dynamic objects.</description></item></channel></rss>