<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Jon Skeet: Coding Blog : C# 4, C#</title><link>http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/C_2300_/default.aspx</link><description>Tags: C# 4, C#</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>The irritation of bad names</title><link>http://msmvps.com/blogs/jon_skeet/archive/2010/01/27/the-irritation-of-bad-names.aspx</link><pubDate>Wed, 27 Jan 2010 17:40:54 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1754059</guid><dc:creator>skeet</dc:creator><slash:comments>28</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1754059</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1754059</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2010/01/27/the-irritation-of-bad-names.aspx#comments</comments><description>&lt;p&gt;A couple of days ago I accidentally derailed the comments on &lt;a href="http://blogs.msdn.com/ericlippert/archive/2010/01/25/why-are-unused-using-directives-not-a-warning.aspx"&gt;Eric Lippert&amp;#39;s blog post about unused &amp;quot;using&amp;quot; directives&lt;/a&gt;. The reason that redundant code doesn&amp;#39;t generate a warning in Visual Studio is that it&amp;#39;s what you get to start with in Visual Studio. This led me to rant somewhat about other aspects of Visual Studio&amp;#39;s behaviour which sacrifice long term goodness in favour of short term efficiency. Almost all the subsequent comments (at the time of writing this post) are concerned with my rant rather than Eric&amp;#39;s post. Some agree with me, some don&amp;#39;t - but it&amp;#39;s only now that I&amp;#39;ve spotted the bigger picture behind my annoyances.&lt;/p&gt;  &lt;p&gt;All of them are to do with &lt;em&gt;names&lt;/em&gt; and the defaults provided. I&amp;#39;ve blogged before about how hard it is to find a good name - it&amp;#39;s a problem I run into time and time again, and the ability to rename something is one of the most important refactorings around.&lt;/p&gt;  &lt;h3&gt;If you don&amp;#39;t know, ask&lt;/h3&gt;  &lt;p&gt;Now if it&amp;#39;s hard to find a good name, it stands to reason that anything the IDE can generate automatically is likely to be a poor name... such as &amp;quot;Form1&amp;quot;, &amp;quot;textBox1&amp;quot; or &amp;quot;button1_Click&amp;quot;. And yet, in various situations, Visual Studio will happily generate such names, and it can sometimes be a small but significant pain to correct it.&lt;/p&gt;  &lt;p&gt;The situation which causes me personally a lot of pain is copying a file. For C# in Depth, I have a lot of very small classes, each with a Main method. When I&amp;#39;m evolving an example, I often want to take the existing code and just change it slightly, but in a new file. So I might have a file called OrderByName.cs containing a class called OrderByName. (I agree this would normally be a bad class name, but in the context of a short but complete example it&amp;#39;s okay.) I want to just select the file, hit copy and paste, and be asked for a new filename. The class within the file would then be renamed for me as well. As an aside, this is the behaviour Eclipse has in its Java tooling.&lt;/p&gt;  &lt;p&gt;In reality, I&amp;#39;d end up with a new file called &amp;quot;Copy of OrderByName.cs&amp;quot;, still containing a class called OrderByName. Renaming the file wouldn&amp;#39;t offer to rename the class, as the filename wouldn&amp;#39;t match the class name. Renaming the class by just changing it and then hitting Ctrl-. would &lt;em&gt;also&lt;/em&gt; rename the original class, which is intensely annoying. You&amp;#39;re basically stuck doing it manually with find and replace, as far as I can see. There may be some automated aid available, but at the very least it&amp;#39;s non-obvious.&lt;/p&gt;  &lt;p&gt;Now the question is: why would I ever want a file called &amp;quot;Copy of OrderByName.cs&amp;quot;? That&amp;#39;s always going to be the &lt;em&gt;wrong&lt;/em&gt; name, so why doesn&amp;#39;t Visual Studio ask me for the &lt;em&gt;right&lt;/em&gt; name? It could provide a default so I can skip through if I really want to (and probably an &amp;quot;Apply to all items&amp;quot; if I&amp;#39;m copying multiple files) but at least give me the &lt;em&gt;chance&lt;/em&gt; to specify the right filename at the crucial point. Once it knows the right new filename &lt;em&gt;before&lt;/em&gt; it&amp;#39;s got a broken build, I would hope it would be easy to then apply the new name to the class too.&lt;/p&gt;  &lt;p&gt;The underlying point is that if you absolutely &lt;em&gt;have&lt;/em&gt; to have a name for something, and there&amp;#39;s no way a sensible suggestion can be provided, the user should be consulted. I know there&amp;#39;s a lot of discussion these days about not asking the user pointless questions, but this &lt;em&gt;isn&amp;#39;t&lt;/em&gt; a pointless question... at least when it comes to filenames.&lt;/p&gt;  &lt;h3&gt;If you don&amp;#39;t need a name, don&amp;#39;t use one&lt;/h3&gt;  &lt;p&gt;I&amp;#39;m not a UI person, so some of this section may be either outdated or at least only partially applicable. In particular, I believe WPF does a better job than the Windows Forms designer.&lt;/p&gt;  &lt;p&gt;Names have two main purposes, in my view. They can provide semantic meaning to aid the reader, even if a name isn&amp;#39;t strictly required (think of the &amp;quot;introduce local variable&amp;quot; refactoring) and they can be used for identification.&lt;/p&gt;  &lt;p&gt;Now suppose I&amp;#39;m creating a label on a form. If I&amp;#39;m using the designer, I can probably see the text on the label - its meaning is obvious. I quite possibly don&amp;#39;t have to refer to the label anywhere in code, unless I&amp;#39;m changing the value programmatically... so why does it need a name? If you really think it needs a name, is &amp;quot;label1&amp;quot; ever going to be the &lt;em&gt;right&lt;/em&gt; name - the one you&amp;#39;d have come up with as the most meaningful one you could think of?&lt;/p&gt;  &lt;p&gt;In the comments in Eric&amp;#39;s blog, someone pointed out that being prompted for a name every time you dragged a control onto the designer would interrupt workflow... and I quite agree. Many of those controls won&amp;#39;t need names. However, as soon as they &lt;em&gt;do&lt;/em&gt; need a name, prompting for the name at that point (or just typing it into the property view) isn&amp;#39;t nearly such a distraction... indeed, I&amp;#39;d suggest it&amp;#39;s actually guiding the developer in question to crystallize their thoughts about the purpose of that control.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;Okay, this has mostly been more ranting - but at least it&amp;#39;s now on my blog, and I&amp;#39;ve been able to give a little bit more detail about the general problem I see in Visual Studio - a problem which leads to code containing utterly useless names.&lt;/p&gt;  &lt;p&gt;The fundamental principle is that I want every name in my code to be a meaningful one. The IDE should use two approaches to help me with that goal:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Don&amp;#39;t give a name to anything that doesn&amp;#39;t deserve or need one&lt;/li&gt;    &lt;li&gt;If a name is really necessary, and you can&amp;#39;t guess it from the rest of the context, ask the user&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I don&amp;#39;t expect anything to change, but it&amp;#39;s good to have it off my chest.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1754059" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category></item><item><title>Type initialization changes in .NET 4.0</title><link>http://msmvps.com/blogs/jon_skeet/archive/2010/01/26/type-initialization-changes-in-net-4-0.aspx</link><pubDate>Tue, 26 Jan 2010 17:48:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1753848</guid><dc:creator>skeet</dc:creator><slash:comments>15</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1753848</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1753848</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2010/01/26/type-initialization-changes-in-net-4-0.aspx#comments</comments><description>&lt;p&gt;This morning, while checking out an email I&amp;#39;d received about my &lt;a href="http://www.yoda.arachsys.com/csharp/teasers.html"&gt;brain-teasers page&lt;/a&gt;, I discovered an interesting change to the CLR in .NET 4.0. At least, I think it&amp;#39;s interesting. It&amp;#39;s possible that different builds of the CLR have exhibited different behaviour for a while - I only have 32-bit versions of Windows installed, so that&amp;#39;s what I&amp;#39;m looking at for this whole post. (Oh, and all testing was done under .NET 4.0b2 - it could still change before release.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note: to try any of this code, build in release mode. Running in the debugger or even running a debug build without the debugger may well affect the behaviour.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;Precise initialization: static constructors&lt;/h3&gt;
&lt;p&gt;I&amp;#39;ve written before about static constructors in C# causing types to be initialized &lt;em&gt;immediately&lt;/em&gt; before the type is first used, either by constructing an instance or referring to a static member. In other words, consider the following program:&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; StaticConstructorType    &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;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;int&lt;/span&gt; x = Log();    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Force &amp;quot;precise&amp;quot; initialization&lt;/span&gt;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; StaticConstructorType() {}    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;int&lt;/span&gt; Log()    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Type initialized&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; 0;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; StaticMethod() {}    &lt;br /&gt;}    &lt;br /&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; StaticConstructorTest    &lt;br /&gt;{    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main(&lt;span class="ReferenceType"&gt;string&lt;/span&gt;[] args)    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;if&lt;/span&gt; (args.Length == 0)    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;No args&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;else&lt;/span&gt;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; StaticConstructorType.StaticMethod();    &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;Note how the static variable x is initialized using a method that writes to the console. This program is &lt;em&gt;guaranteed&lt;/em&gt; to write exactly one line to the console: StaticConstructorType will not be initialized unless you give a command line argument to force it into the &amp;quot;else&amp;quot; branch. The way the C# compiler controls this is using the &lt;a href="http://pobox.com/~skeet/csharp/beforefieldinit.html"&gt;beforefieldinit flag&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So far, so boring. We know exactly when the type will be initialized - I&amp;#39;m going to call this &amp;quot;precise&amp;quot; initialization. This behaviour hasn&amp;#39;t changed, and couldn&amp;#39;t change without it being backwardly incompatible. Now let&amp;#39;s consider what happens without the static constructor.&lt;/p&gt;
&lt;h3&gt;Eager initialization: .NET 3.5&lt;/h3&gt;
&lt;p&gt;Let&amp;#39;s take the previous program and &lt;em&gt;just&lt;/em&gt; remove the (code-less) static constructor - and change the name of the type, for clarity:&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; Eager    &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;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;int&lt;/span&gt; x = Log();    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;int&lt;/span&gt; Log()    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Type initialized&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; 0;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; StaticMethod() {}    &lt;br /&gt;}    &lt;br /&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; EagerTest    &lt;br /&gt;{    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main(&lt;span class="ReferenceType"&gt;string&lt;/span&gt;[] args)    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;if&lt;/span&gt; (args.Length == 0)    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;No args&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;else&lt;/span&gt;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Eager.StaticMethod();    &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;Under .NET 3.5, this either writes &lt;em&gt;both&lt;/em&gt; &amp;quot;Type initialized&amp;quot; and &amp;quot;No args&amp;quot; (if you don&amp;#39;t pass any command line arguments) or &lt;em&gt;just&lt;/em&gt; &amp;quot;Type initialized&amp;quot; (if you do). In other words, the type initialization is eager. In my experience, a type is initialized at the start of execution of the first method which refers to that type.&lt;/p&gt;
&lt;p&gt;So what about .NET 4.0? &lt;em&gt;Under .NET 4.0, the above code will never print &amp;quot;Type initialized&amp;quot;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;If you don&amp;#39;t pass in a command line argument, you see &amp;quot;No args&amp;quot; as you might expect... if you do, there&amp;#39;s no output at all. The type is being initialized extremely lazily. Let&amp;#39;s see how far we can push it...&lt;/p&gt;
&lt;h3&gt;Lazy initialization: .NET 4.0&lt;/h3&gt;
&lt;p&gt;The CLR guarantees that the type initializer will be run at some point before the first reference to any static field. If you don&amp;#39;t use a static field, the type doesn&amp;#39;t &lt;em&gt;have&lt;/em&gt; to be initialized... and it looks like .NET 4.0 obeys that in a fairly lazy way. Another test app:&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; Lazy    &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;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;int&lt;/span&gt; x = Log();    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;int&lt;/span&gt; y = 0;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;int&lt;/span&gt; Log()    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Type initialized&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; 0;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; StaticMethod()    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;In static method&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }    &lt;br /&gt;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; StaticMethodUsingField()    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;In static method using field&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;y = {0}&amp;quot;&lt;/span&gt;, y);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; InstanceMethod()    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;In instance method&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }    &lt;br /&gt;}    &lt;br /&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; LazyTest    &lt;br /&gt;{    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main(&lt;span class="ReferenceType"&gt;string&lt;/span&gt;[] args)    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Before static method&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Lazy.StaticMethod();    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Before construction&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Lazy lazy = &lt;span class="Keyword"&gt;new&lt;/span&gt; Lazy();    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Before instance method&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lazy.InstanceMethod();    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Before static method using field&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Lazy.StaticMethodUsingField();    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;End&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }    &lt;br /&gt;} &lt;/div&gt;
&lt;p&gt;This time the output is:&lt;/p&gt;
&lt;div class="code"&gt;Before static method   &lt;br /&gt;In static method    &lt;br /&gt;Before construction    &lt;br /&gt;Before instance method    &lt;br /&gt;In instance method    &lt;br /&gt;Before static method using field    &lt;br /&gt;Type initialized    &lt;br /&gt;In static method using field    &lt;br /&gt;y = 0    &lt;br /&gt;End &lt;/div&gt;
&lt;p&gt;As you can see, the type initialized when StaticMethodUsingField is called. It&amp;#39;s not as lazy as it &lt;em&gt;could&lt;/em&gt; be - the first line of the method could execute before the type is initialized. Still, being able to construct an instance and call a method on it without triggering the type initializer is slightly surprising.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve got one final twist... what would you expect this program to do?&lt;/p&gt;
&lt;div class="code"&gt;using System;   &lt;br /&gt;    &lt;br /&gt;class CachingSideEffect    &lt;br /&gt;{    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private static int x = Log();    &lt;br /&gt;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private static int Log()    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(&amp;quot;Type initialized&amp;quot;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public CachingSideEffect()    &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; Action action = () =&amp;gt; Console.WriteLine(&amp;quot;Action&amp;quot;);    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }    &lt;br /&gt;}    &lt;br /&gt;    &lt;br /&gt;class CachingSideEffectTest    &lt;br /&gt;{    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static void Main(string[] args)    &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; new CachingSideEffect();    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }    &lt;br /&gt;} &lt;/div&gt;
&lt;p&gt;In .NET 4.0, using the Microsoft C# 4 compiler, this &lt;em&gt;does&lt;/em&gt; print &amp;quot;Type initialized&amp;quot;... because the C# compiler has created a static field in which to cache the action. The lambda expression doesn&amp;#39;t capture any variables, so the same delegate instance can be reused every time. That involves caching it in a static field, triggering type initialization. If you change the action to use &lt;code&gt;Console.WriteLine(this)&lt;/code&gt; then it can&amp;#39;t cache the delegate, and the constructor no longer triggers initialization.&lt;/p&gt;
&lt;p&gt;This bit is completely implementation-specific in terms of the C# compiler, but I thought it might tickle your fancy anyway.&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;I&amp;#39;d like to stress that none of this should cause your code any problems. The somewhat eager initialization of types without static constructors was entirely legitimate according to the C# and CLR specs, and so the new lazy behaviour of .NET 4.0. If your code &lt;em&gt;assumed&lt;/em&gt; that just calling a static method, or creating an instance, would trigger initialization, then that&amp;#39;s your own fault to some extent. That doesn&amp;#39;t stop it being an interesting change to spot though :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1753848" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category></item><item><title>Contract classes and nested types within interfaces</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/10/31/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 xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1736652</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">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;</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></item><item><title>Recent activities</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/09/04/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 xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1720570</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">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;</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></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 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 xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1713522</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">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;</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></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 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 xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1700112</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">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;</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></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 22:28:46 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698645</guid><dc:creator>skeet</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1698645</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1698645</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/07/07/faking-com-to-fool-the-c-compiler.aspx#comments</comments><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;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698645" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Wacky+Ideas/default.aspx">Wacky Ideas</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/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/Stack+Overflow/default.aspx">Stack Overflow</category></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 22:28:26 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697785</guid><dc:creator>skeet</dc:creator><slash:comments>35</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1697785</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1697785</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#comments</comments><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;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1697785" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category></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 16:50:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1695865</guid><dc:creator>skeet</dc:creator><slash:comments>19</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1695865</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1695865</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/06/19/os-jam-at-google-london-c-4-and-the-dlr.aspx#comments</comments><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;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1695865" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Google/default.aspx">Google</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Speaking+engagements/default.aspx">Speaking engagements</category></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 20:01:13 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1695727</guid><dc:creator>skeet</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1695727</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1695727</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/06/17/dynamic-type-inference-and-surprising-possibilities.aspx#comments</comments><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;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1695727" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Books/default.aspx">Books</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category></item><item><title>C# 4.0: dynamic&lt;T&gt; ?</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/30/c-4-0-dynamic-lt-t-gt.aspx</link><pubDate>Thu, 30 Oct 2008 22:04:10 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1652571</guid><dc:creator>skeet</dc:creator><slash:comments>29</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1652571</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1652571</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/10/30/c-4-0-dynamic-lt-t-gt.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;ve not played with the VS2010 CTP much yet, and I&amp;#39;ve only looked briefly at the documentation and blogs about the new C# 4.0 &lt;code&gt;dynamic&lt;/code&gt; type, but a thought occurred to me: why not have the option of making it generic as a way of saying &amp;quot;I will dynamically support this set of operations&amp;quot;?&lt;/p&gt; &lt;p&gt;As an example of what I mean, suppose you have an interface &lt;code&gt;IMessageRouter&lt;/code&gt; like this:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessageRouter&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ValueType"&gt;void&lt;/span&gt; Send(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; message, &lt;span class="ReferenceType"&gt;string&lt;/span&gt; destination);&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;(This is an arbitrary example, by the way. The idea isn&amp;#39;t specifically more suitable for message routing than anything else.)&lt;/p&gt; &lt;p&gt;I may have various implementations, written in various languages (or COM) which support the &lt;code&gt;Send&lt;/code&gt; method with those parameters. Some of those implementations actually implement &lt;code&gt;IMessageRouter&lt;/code&gt; but some don&amp;#39;t. I&amp;#39;d like to be able to do the following:&lt;/p&gt; &lt;div class="code"&gt;dynamic&amp;lt;IMessageRouter&amp;gt; router = GetRouter();&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// This is fine (but still invoked dynamically)&lt;/span&gt;&lt;br /&gt;router.Send(&lt;span class="String"&gt;&amp;quot;message&amp;quot;&lt;/span&gt;, &lt;span class="String"&gt;&amp;quot;skeet@pobox.com&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;span class="InlineComment"&gt;// Compilation error: no such overload&lt;/span&gt;&lt;br /&gt;router.Send(&lt;span class="String"&gt;&amp;quot;message&amp;quot;&lt;/span&gt;, &lt;span class="String"&gt;&amp;quot;skeet@pobox.com&amp;quot;&lt;/span&gt;, 20); &lt;/div&gt; &lt;p&gt;Intellisense would work, and we&amp;#39;d still have some of the benefits of static typing but without the implementations having to know about your interface. Of course, it would be quite easy to create an implementation of the interface which did exactly this - but now imagine that instead of &lt;code&gt;IMessageRouter&lt;/code&gt; we had &lt;code&gt;MessageRouter&lt;/code&gt; - a concrete class. In this case the compiler would still restrict the caller to the public API of the class, but it wouldn&amp;#39;t have to be the real class. No checking would be performed by the compiler that your dynamic type &lt;i&gt;actually&lt;/i&gt; supported the operations - given that we&amp;#39;re talking about dynamic invocation, that would be impossible to do. It would instead be an &amp;quot;opt-in&amp;quot; restriction the client places on themselves. It could also potentially help with performance - if the binding involved realised that the &lt;i&gt;actual&lt;/i&gt; type of the dynamic object natively implemented the interface or was/derived from the class, then no real dynamic calls need be made; just route all directly. &lt;/p&gt; &lt;p&gt;This may all sound a bit fuzzy - I&amp;#39;m extremely sleepy, to be honest - but I think it&amp;#39;s a potentially interesting idea. Thoughts? &lt;/p&gt; &lt;h2&gt;Update&lt;/h2&gt;Apparently this post wasn&amp;#39;t as clear as it might be. I&amp;#39;m quite happy to keep the currently proposed dynamic type idea &lt;em&gt;as well&lt;/em&gt; - I&amp;#39;d like this as an &lt;em&gt;additional&lt;/em&gt; way of using dynamic objects.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1652571" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Wacky+Ideas/default.aspx">Wacky Ideas</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category></item><item><title>DotNetRocks interview</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/07/dotnetrocks-interview.aspx</link><pubDate>Tue, 07 Oct 2008 16:33:49 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650011</guid><dc:creator>skeet</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1650011</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1650011</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/10/07/dotnetrocks-interview.aspx#comments</comments><description>&lt;p&gt;Last Monday evening I had a chat with the guys from &lt;a href="http://dotnetrocks.com"&gt;DotNetRocks&lt;/a&gt;, and today &lt;a href="http://www.dotnetrocks.com/default.aspx?showNum=383"&gt;the show has gone live&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;I wouldn&amp;#39;t claim to have said anything &lt;em&gt;particularly&lt;/em&gt; earth-shattering, and regular readers will probably be familiar with many of the themes anyway, but I thoroughly enjoyed it and hope you will too. Amongst other things, we talked about:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Protocol buffers&lt;/li&gt; &lt;li&gt;Implicit typing and anonymous types&lt;/li&gt; &lt;li&gt;Why it doesn&amp;#39;t bother me that Office hasn&amp;#39;t been ported to .NET&lt;/li&gt; &lt;li&gt;C# 4&lt;/li&gt; &lt;li&gt;My wishlist for C#&lt;/li&gt; &lt;li&gt;Threading and Parallel Extensions&lt;/li&gt; &lt;li&gt;Working for Google&lt;/li&gt; &lt;li&gt;How to learn LINQ&lt;/li&gt; &lt;li&gt;C# in Depth&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Feedback welcome. And yes, I know I sound somewhat like a stereotypical upper-class idiot at times. Unfortunately there&amp;#39;s not a lot I can do about that. Only the &amp;quot;idiot&amp;quot; part is accurate :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650011" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/General/default.aspx">General</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/LINQ/default.aspx">LINQ</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/Google/default.aspx">Google</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Protocol+Buffers/default.aspx">Protocol Buffers</category></item><item><title>C# 4: Immutable type initialization</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx</link><pubDate>Sat, 15 Mar 2008 23:02:29 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1544091</guid><dc:creator>skeet</dc:creator><slash:comments>23</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1544091</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1544091</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/03/16/c-4-immutable-type-initialization.aspx#comments</comments><description>&lt;p&gt;(I&amp;#39;m giving up with the numbering now, unless anyone particularly wants me to keep it up. What was originally going to be a limited series appears to be growing without end...)&lt;/p&gt; &lt;p&gt;As Chris Nahr pointed out in my &lt;a href="http://msmvps.com/blogs/jon.skeet/archive/2008/03/11/c-4-part-5-other-bits-and-bobs-which-probably-don-t-merit-inclusion.aspx"&gt;previous post&lt;/a&gt;, my earlier idea about staged initialization was very half-baked. As he&amp;#39;s prompted me to think further about it, I&amp;#39;ve come up with another idea. It&amp;#39;s slightly more baked, although there are lots of different possibilities and open questions.&lt;/p&gt; &lt;p&gt;Let&amp;#39;s take a step back, and look at my motivation: I like immutable types. They&amp;#39;re handy when it comes to thread safety, and they make it a lot easier to reason about the world when you know that nothing can change a certain value after it&amp;#39;s been created. Now, the issues are:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;We really want to be able to fully construct the object in the constructor. That means we can mark all fields as &lt;code&gt;initonly&lt;/code&gt; in the generated IL, potentially giving the CLR more scope for optimisation.  &lt;li&gt;When setting more than two or three values (while allowing some to be optional) constructor overloading ends up being a pain.  &lt;li&gt;Object initializers in C# 3 only apply to properties and fields, not method/constructor arguments - so we can&amp;#39;t get the clarity of naming.  &lt;li&gt;Ideally we want to support validation (or possibly other code) &lt;em&gt;and&lt;/em&gt; automatic properties.  &lt;li&gt;The CLR won&amp;#39;t allow &lt;code&gt;initonly&lt;/code&gt; fields being set anywhere other than in the constructor - so even if we made sure we didn&amp;#39;t call any setters other than in the constructor, we still couldn&amp;#39;t use them to set the fields.  &lt;li&gt;We want to allow simple construction of immutable types from code other than C#. In particular, I care about being able to use projects like Spring.NET and Castle/Windsor (potentially after changes to those projects) to easily create instances of immutable types without resorting to looking up the order of constructor parameters.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The core of the proposal is to be able to mark properties as &lt;code&gt;initonly&lt;/code&gt;, and get the compiler to create an extra type which is thoroughly mutable, and contains those properties - as well as a constructor which accepts an instance of the extra type and uses it to populate the immutable instance of the main type before returning.&lt;/p&gt; &lt;p&gt;Extra syntax could then be used to call this constructor - or indeed, given that the properties are actually readonly, thus avoiding any ambiguity, normal object initializers could be used to create instances.&lt;/p&gt; &lt;p&gt;Just as an example, imagine this code:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Address&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; Line1 { get; initonly set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; Line2 { get; initonly set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; Line3 { get; initonly set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; County { get; initonly set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; State { get; initonly set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; Country { get; initonly set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; ZipCode { get; initonly set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Business methods as normal&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// In another class&lt;/span&gt;&lt;br /&gt;Address addr = &lt;span class="Keyword"&gt;new&lt;/span&gt; Address &lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Line1=&lt;span class="String"&gt;&amp;quot;10 Fairview Avenue&amp;quot;&lt;/span&gt;, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Line3=&lt;span class="String"&gt;&amp;quot;Makebelieve Town&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; County=&lt;span class="String"&gt;&amp;quot;Mono County&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; State=&lt;span class="String"&gt;&amp;quot;California&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Country=&lt;span class="String"&gt;&amp;quot;US&amp;quot;&lt;/span&gt;&lt;br /&gt;}; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;That could be transformed into code a bit like this: &lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Immutable class&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// Let tools (e.g. the compiler!) know how we&lt;/span&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// expect to be initialized. Could be specified&lt;/span&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// manually to avoid using the default class name&lt;/span&gt;&lt;br /&gt;[InitializedWith(&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(Address.Init))]&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Address&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Nested mutable class used for initialization&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [CompilerGenerated]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Init&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; Line1 { get; set; }&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="ReferenceType"&gt;string&lt;/span&gt; Line2 { get; set; }&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="ReferenceType"&gt;string&lt;/span&gt; Line3 { get; set; }&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="ReferenceType"&gt;string&lt;/span&gt; County { get; set; }&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="ReferenceType"&gt;string&lt;/span&gt; State { get; set; }&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="ReferenceType"&gt;string&lt;/span&gt; Country { get; set; }&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="ReferenceType"&gt;string&lt;/span&gt; ZipCode { get; initonly set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Read-only &amp;quot;real&amp;quot; properties, automatically&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// implemented and backed with initonly fields&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; Line1 { get; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; Line2 { get; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; Line3 { get; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; County { get; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; State { get; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; Country { get; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; ZipCode { get; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Automatically generated constructor, using&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// backing fields directly&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Address(Address.Init init)&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; &amp;lt;&amp;gt;_line1 = init.Line1;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;gt;_line2 = init.Line2;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;gt;_line3 = init.Line3;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;gt;_county = init.County;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;gt;_state = init.State;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;gt;_country = init.Country;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;gt;_zipCode = init.ZipCode;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Business methods as normal&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// In another class&lt;/span&gt;&lt;br /&gt;Address addr = &lt;span class="Keyword"&gt;new&lt;/span&gt; Address(&lt;span class="Keyword"&gt;new&lt;/span&gt; Address.Init&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Line1=&lt;span class="String"&gt;&amp;quot;10 Fairview Avenue&amp;quot;&lt;/span&gt;, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Line3=&lt;span class="String"&gt;&amp;quot;Makebelieve Town&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; County=&lt;span class="String"&gt;&amp;quot;Mono County&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; State=&lt;span class="String"&gt;&amp;quot;California&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Country=&lt;span class="String"&gt;&amp;quot;US&amp;quot;&lt;/span&gt;&lt;br /&gt;}); &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;That&amp;#39;s the simple case, of course. Issues: &lt;/p&gt; &lt;ul&gt; &lt;li&gt;Unlike other compiler-generated types (anonymous types, types for iterator blocks, types for anonymous functions) we &lt;em&gt;do&lt;/em&gt; want this to be public, and have a name which can be used elsewhere. We need to find some way of making sure it doesn&amp;#39;t clash with other names. In the example above, I&amp;#39;ve used an attribute to indicate which type is used for initialization - I could imagine some way of doing this in the &amp;quot;pre-transform&amp;quot; code to say what the auto-generated type should be called.  &lt;li&gt;What happens if you put code in the setter, instead of making it automatically implemented? I suspect that code should be moved into the setter of the initialization class - but at that point it won&amp;#39;t have access to the rest of the state of the class (beyond the other properties in the initialization class). It&amp;#39;s somewhat messy.  &lt;li&gt;What if you want to add code to the generated constructor? (Possibly solution: allow constructors to be marked somehow in a way that means &amp;quot;add on the initialization class as a parameter at the end, and copy all the values as a first step.) &lt;li&gt;How can you indicate that some parameters are mandatory, and some are optional? (The mandatory parameters could just be marked as &lt;code&gt;readonly&lt;/code&gt; properties rather than &lt;code&gt;initonly&lt;/code&gt;, and then the initialization class specified as an extra parameter for a constructor which takes all the mandatory ones. Doesn&amp;#39;t feel elegant though, and leaves you with two different types of initialization code being mixed in the client - some named, some positional.) &lt;li&gt;How do you specify default values? (They probably end up being the default values of the automatically generated properties of the initialization class, but there needs to be some syntax to specify them.)&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I suspect there are more issues too - but I think the benefits would be great. I know the C# team has been thinking about immutability, but I&amp;#39;ve no idea what kind of support they&amp;#39;re currently envisioning. Unlike my previous ideas, which were indeed unpalatable for various reasons, I think this one has real potential. Mind you, given that I&amp;#39;ve come up with it after only mulling this over in &amp;quot;spare&amp;quot; time, I highly doubt that it will be a new concept to the team...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1544091" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category></item><item><title>C# 4 idea: Iterator blocks and parameter checking</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx</link><pubDate>Sun, 02 Mar 2008 19:54:44 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1531225</guid><dc:creator>skeet</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1531225</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1531225</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx#comments</comments><description>&lt;p&gt;Iterator blocks have an interesting property: they defer execution. When the method (or property) is called, &lt;a href="http://blogs.msdn.com/ericlippert/archive/2007/09/06/psychic-debugging-part-two.aspx"&gt;none of your code is executed&lt;/a&gt; - it only starts running when &lt;code&gt;MoveNext()&lt;/code&gt; is first called.&lt;/p&gt; &lt;p&gt;Deferred execution is a great thing in many ways, but it&amp;#39;s a pain when it comes to parameter checking. If you check parameters within an iterator block, you&amp;#39;ve effectively left a timebomb - the error will be potentially reported a long way from the original source of the problem. (Side-note: this is why I prefer using a cast to &lt;code&gt;as&lt;/code&gt; when I know what type something really &lt;i&gt;should&lt;/i&gt; be - I&amp;#39;d rather get an &lt;code&gt;InvalidCastException&lt;/code&gt; immediately than a &lt;code&gt;NullReferenceException&lt;/code&gt; later on.)&lt;/p&gt; &lt;p&gt; One solution to this is to check the parameters in the public method and then call a private method which is implemented with an iterator block. That&amp;#39;s a bit ugly though. It would be nice to be able to specify the parameter checking in an initial block which could only occur at the start of the method - something like this simple implementation of &lt;code&gt;Repeat&lt;/code&gt;: &lt;/p&gt; &lt;p&gt;&lt;div class="code"&gt; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;IEnumerable&amp;lt;TResult&amp;gt;&amp;nbsp;Repeat&amp;lt;TResult&amp;gt;(TResult&amp;nbsp;element,&amp;nbsp;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;nbsp;count)&lt;br /&gt; {&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;yield&amp;nbsp;&lt;span class="Statement"&gt;do&lt;/span&gt;&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;&lt;span class="Statement"&gt;if&lt;/span&gt;&amp;nbsp;(count&amp;nbsp;&amp;lt;&amp;nbsp;0)&lt;br /&gt; &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;&lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt;&amp;nbsp;ArgumentException(count);&lt;br /&gt; &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;}&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="Statement"&gt;for&lt;/span&gt;&amp;nbsp;(&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;nbsp;i=0;&amp;nbsp;i&amp;nbsp;&amp;lt;&amp;nbsp;count;&amp;nbsp;i++)&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;yield&amp;nbsp;&lt;span class="Statement"&gt;return&lt;/span&gt;&amp;nbsp;element;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; } &lt;/div&gt;&lt;/p&gt; &lt;p&gt;   I&amp;#39;ve chosen &lt;code&gt;yield do&lt;/code&gt; for two reasons: it fits in with the rest of the &lt;code&gt;yield&lt;/code&gt; pattern,   and by using an existing keyword I suspect it reduces/eliminates the chance of this being a breaking change. It&amp;#39;s   not ideal from the point of self-description, but &lt;code&gt;do&lt;/code&gt; is the closest keyword I could find when I looked   at the spec. In a language with &lt;code&gt;begin&lt;/code&gt; as a keyword, that would probably be a better choice - or &lt;code&gt;initial&lt;/code&gt;.   &lt;code&gt;fixed&lt;/code&gt; appeals in that the code is fixed in the method rather than being shunted off into the compiler-generated   type, but the normal use of &lt;code&gt;fixed&lt;/code&gt; is far too different to make this an appealing choice. Any other suggestions   are very welcome :) &lt;/p&gt; &lt;p&gt;   Is this an important enough change to make it worth including in C# 4? I&amp;#39;m not sure. The negative side is   that I suspect relatively few people use iterator blocks much, so it may not have a very wide benefit. The positive side   is that it only actually makes any difference to those who &lt;i&gt;do&lt;/i&gt; use iterator blocks, and I believe almost   all of them would find it a welcome addition. It&amp;#39;s simple to understand, and it makes iterator blocks much easier to use   in a &amp;quot;correct&amp;quot; manner when any form of initial checking is involved. &lt;/p&gt; &lt;p&gt;Note, mostly to myself and Marc Gravell: I haven&amp;#39;t checked, but I suspect most of Push LINQ is broken in this respect.&lt;/p&gt; &lt;p&gt;Further note to self - I really need to fix my code formatter to treat &lt;code&gt;yield&lt;/code&gt; as a contextual keyword.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1531225" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category></item><item><title>Language design, when is a language "done", and why does it matter?</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/02/11/language-design-when-is-a-language-quot-done-quot-and-why-does-it-matter.aspx</link><pubDate>Mon, 11 Feb 2008 23:45:31 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1508972</guid><dc:creator>skeet</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1508972</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1508972</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/02/11/language-design-when-is-a-language-quot-done-quot-and-why-does-it-matter.aspx#comments</comments><description>&lt;p&gt;As per previous posts, I&amp;#39;ve been thinking a fair amount about how much it&amp;#39;s reasonable to keep progressing a language. Not only have thoughts about C# 4 provoked this, but also a few other sources:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://www.pluralsight.com/blogs/dbox/archive/2008/01/15/49926.aspx"&gt;Don Box on&lt;/a&gt; &lt;a href="http://blogs.tedneward.com/2008/01/15/Java+QuotDonequot+Like+The+Patriots+Or+QuotDonequot+Like+The+Dolphins.aspx"&gt;Ted Neward on Java&lt;/a&gt; (yes, two separate links - but view both)  &lt;li&gt;&lt;a href="http://www.pluralsight.com/blogs/dbox/archive/2008/02/07/50180.aspx"&gt;Don again on the &amp;quot;doneness&amp;quot; of XML&lt;/a&gt;  &lt;li&gt;The &lt;a href="http://channel9.msdn.com/showpost.aspx?postid=380228"&gt;Channel9 video&lt;/a&gt; of Erik Meijer, Gilad Bracha and Mads Torgersen&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The video is very well worth watching in its entirety - even though I wouldn&amp;#39;t pretend to understand everything in it. (It&amp;#39;s worth &lt;em&gt;watching&lt;/em&gt; rather than just listening to, by the way - Gilad&amp;#39;s body language is very telling.) Here are just a few of the things which particularly caught my attention:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Mads, 14:45 on the &amp;quot;Babelification&amp;quot; which could occur if everyone plugs in their own type system into a pluggable language. This is similar to my concern about LISP-like macros in C#, I think.  &lt;li&gt;Gilad, 23:40: &amp;quot;We&amp;#39;re the kind of people who love to learn new things. Most people hate to learn new things.&amp;quot; I don&amp;#39;t agree with that - but I&amp;#39;d say that people hate to feel they&amp;#39;re on a treadmill where they spent all their time learning, with no chance to actually use what they&amp;#39;ve learned.  &lt;li&gt;Gilad, 28:35: &amp;quot;People never know when to stop [...] What happens is they do too much.&amp;quot;  &lt;li&gt;Mads, 50:50: &amp;quot;The perfect language is the one that helps you do your task well, and that varies from task to task.&amp;quot;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;So, what does this have to do with C#? Well, I was wondering how different people would respond when asked if C# was &amp;quot;done&amp;quot;. Eric&amp;#39;s certainly remarked that it&amp;#39;s nowhere near done - whereas prior to C# 3, I think I&amp;#39;d have called C# 2 &amp;quot;done&amp;quot;. C# 3 has opened my eyes a little about what might be possible - how radical changes can be made while still keeping a coherent language.&lt;/p&gt; &lt;p&gt;I&amp;#39;ve been worrying publicly about the burden of learning which is being placed on developers. I&amp;#39;m starting to change my thoughts now (yes, even since yesterday). I&amp;#39;ve started to wonder where the burden is coming from, and why it matters if C# changes even more radically in the future.&lt;/p&gt; &lt;h3&gt;Who would make you learn or use C# 4?&lt;/h3&gt; &lt;p&gt;Suppose the C# team went nuts, and decided that C# 4 would include:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;x86 inline assembly  &lt;li&gt;Optional reverse Polish notation, which could be mixed and matched with the existing syntax  &lt;li&gt;Checked exceptions  &lt;li&gt;Regular expressions as a language feature, but using a new and slightly different regex dialect  &lt;li&gt;User-defined operators (so you could define the &amp;quot;treble clef&amp;quot; operator, should you wish to)  &lt;li&gt;Making semi-colons optional, but whitespace significant. (Heck, we could remove braces at the same time - optionally.)  &lt;li&gt;A scripting mode, where Console.WriteLine(&amp;quot;Hello&amp;quot;) would count as a complete program&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I&amp;#39;m assuming that most readers wouldn&amp;#39;t &lt;em&gt;want&lt;/em&gt; to use or even learn such a language. Would you do it anyway though? Bear it in mind.&lt;/p&gt; &lt;p&gt;Now suppose the C# team worked out ways of including significant pieces of obscure but powerful computer science into C# 4 instead. Lots to learn, but with great rewards. It&amp;#39;s backwardly compatible, but idiomatic C# 4 looks totally different to C# 3.&lt;/p&gt; &lt;p&gt;Here&amp;#39;s the odd thing: I&amp;#39;d be more comfortable with the first scenario than the second. Why? Because the first lets me get on with developing software, guilt-free. There&amp;#39;d be no pressure to learn a lunatic version of C# 4, whereas if it&amp;#39;s reasonably compelling I&amp;#39;ll have to find the time. It&amp;#39;s unlikely (in most companies anyway) that I&amp;#39;ll be given the time by my employers - there might be a training course if I&amp;#39;m lucky, but we all know that&amp;#39;s not really how you learn to use a language productively. You learn it by playing and experimenting in conjunction with the more theoretical training or reading. I like to learn new things, but I&amp;#39;m already several technologies behind.&lt;/p&gt; &lt;h3&gt;What&amp;#39;s in a name?&lt;/h3&gt; &lt;p&gt;Now consider exactly the same scenario, but where instead of &amp;quot;C# 4&amp;quot; the language is named &amp;quot;Gronk#&amp;quot;. In both cases it&amp;#39;s still backwardly compatible with C# 3.&lt;/p&gt; &lt;p&gt;Logically, the name of the language should make no difference whatsoever. &lt;em&gt;But it does. &lt;/em&gt;As a C# developer, I feel an obligation (both personal and from my employer) to keep up with C#. If you&amp;#39;re a C# developer who isn&amp;#39;t at least looking at C# 3 at the moment, you&amp;#39;re likely to find yourself behind the field. Compare that with F#. I&amp;#39;m interested to learn F# properly, and I really will get round to it some time - but I feel no commercial pressure to do so. I&amp;#39;m sure that learning a functional language would benefit many developers - as much (or even more) for the gains in perspective when writing C# 3 as for the likelihood of using the functional language directly in a commercial setting. But hey, it&amp;#39;s not C# so there&amp;#39;s no assumption that it&amp;#39;s on my radar. Indeed, I suspect that if I polled my colleagues, many wouldn&amp;#39;t have even heard of F#. They&amp;#39;re good engineers, but they have a home life which doesn&amp;#39;t involve obsessing over computer languages (yeah, I find it hard to believe too), and at work we&amp;#39;re busy building products.&lt;/p&gt; &lt;p&gt;We could potentially have more &amp;quot;freedom&amp;quot; if every language release came with a completely different name. It would happen to be able to build the old code, but that could seem almost incidental. (It would also potentially give more room for breaking changes, but that&amp;#39;s a very different matter.) There&amp;#39;d be another potential outcome - branching.&lt;/p&gt; &lt;p&gt;Consider the changes I&amp;#39;ve proposed for C# 4. They are mere tweaks. They keep the language headed in the same direction, but with a few minor bumps removed. Let&amp;#39;s call this Jon#.&lt;/p&gt; &lt;p&gt;Now consider a language which (say) Erik Meijer might build as the successor to C# 3. I&amp;#39;m sure there are plenty of features from Haskell which C# doesn&amp;#39;t have yet. Let&amp;#39;s suppose Erik decides to bundle them all into Erik#. (For what it&amp;#39;s worth, I don&amp;#39;t for one moment believe that Erik would actually treat C# insensitively. I have a great respect for him, even if I don&amp;#39;t always understand everything he says.)&lt;/p&gt; &lt;p&gt;Jon# and Erik# can be independent. There&amp;#39;s no need for Erik# to contain the changes of Jon# if they don&amp;#39;t fit in with the bigger picture. Conservative developers can learn Jon# and make their lives a bit easier for little investment. Radical free thinkers can learn Erik# in the hope that it can give them really big rewards in the long run. Everyone&amp;#39;s happy. Innovation and pragmatism both win.&lt;/p&gt; &lt;p&gt;Well, sort of.&lt;/p&gt; &lt;p&gt;We&amp;#39;ve then got two language specs, two compilers, two IDE experiences, etc. That hurts. Branching gives some freedom at the cost of maintenance - as much here as in source control.&lt;/p&gt; &lt;h3&gt;Where do we go from here?&lt;/h3&gt; &lt;p&gt;This has been a meandering post, which is partly due to the circumstances in which I&amp;#39;ve written it, and partly due to the inconclusive nature of my thoughts on the matter. I guess some of the main points are:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Names matter - not just in terms of getting attention, but in the burden of expected learning as well.&lt;/li&gt; &lt;li&gt;Contrary to impressions I may have given before, I really don&amp;#39;t want to be a curmudgeonly stifler of language innovation. I just worry about unintended effects which are more to do with day to day human reality than technical achievement.&lt;/li&gt; &lt;li&gt;There are always options and associated costs - branching being one option which gives freedom at a high price&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I really don&amp;#39;t have a good conclusion here - but I hope plenty of people will spare me their thoughts on this slightly non-technical matter as readily as they have about specific C# 4 features.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1508972" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/General/default.aspx">General</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category></item><item><title>C# 4, part 4: My manifesto and wishlist</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/02/10/c-4-part-4-my-manifesto-and-wishlist.aspx</link><pubDate>Sun, 10 Feb 2008 22:01:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1507108</guid><dc:creator>skeet</dc:creator><slash:comments>46</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1507108</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1507108</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/02/10/c-4-part-4-my-manifesto-and-wishlist.aspx#comments</comments><description>&lt;p&gt;The final part of this little series is the one where I suggest my own ideas for C# 4, beyond those I&amp;#39;ve already indicated my approval for in earlier posts. Before I talk about individual features, however, I&amp;#39;d like to put forward a manifesto which could perhaps help the decision-making process. I hasten to add that I haven&amp;#39;t run all the previous parts through this manifesto to make sure that I&amp;#39;ve been consistent, but all of these thoughts have been running around in my head for a while so I &lt;i&gt;hope&lt;/i&gt; I haven&amp;#39;t been wildly out.&lt;/p&gt; &lt;h2&gt;Manifesto for C# 4&lt;/h2&gt; &lt;p&gt;I would welcome the following goals:&lt;/p&gt; &lt;h3&gt;Remember it&amp;#39;s C#&lt;/h3&gt; &lt;p&gt;Many suggestions have been trying to turn C# into either Ruby, LISP, or other languages. I welcome diversity in languages, and I believe in using the right tool for the job - but that means languages should stick to their core principles, too. Now, I know that sounds like I might be bashing C# 3, given how much that has borrowed from elsewhere for lambda expressions and the like, and I don&amp;#39;t know exactly how I square that circle internally - but I don&amp;#39;t want C# to become a giant toolbox that every useful feature from every language in existence is dumped into.&lt;/p&gt; &lt;p&gt;There are useful &lt;i&gt;ideas&lt;/i&gt; to think about from all kinds of areas - not just existing languages - but I&amp;#39;d be tempted to reject them if they just don&amp;#39;t fit into C# neatly without redefining the whole thing.&lt;/p&gt; &lt;h3&gt;Consider how people will learn it&lt;/h3&gt; &lt;p&gt;I&amp;#39;ve mentioned this before, but I am truly worried about people learning C# 3 from scratch. One of the reasons I didn&amp;#39;t attempt to write about C# from first principles, instead assuming knowledge of C# 1, is that I&amp;#39;m not sure people can sensibly learn it that way. Now, I don&amp;#39;t think I can sensibly get inside the head of someone who doesn&amp;#39;t know anything about C#, but I suspect that I&amp;#39;d want to cover query expressions right at the very end, preferably after quite a while of experience in C# without them.&lt;/p&gt; &lt;p&gt;That might not go for every new feature - it&amp;#39;s probably worth knowing about automatic properties right from the start, for instance, and introducing lambda expressions at the same time as anonymous methods (if C# 3 is the definite goal) but expression trees would be pretty late in my list of topics.&lt;/p&gt; &lt;p&gt;I learned C# 1 from a background of Java, and it didn&amp;#39;t take long to understand the basics of the syntax. Many of the subtleties took a lot longer of course (and it was a very long time before I really understood the differences between events and delegates, I&amp;#39;m sad to say) but it wasn&amp;#39;t a hard move. For a long time C# 2 just meant generics as far as I was concerned - with occasional nullable types, and some of the simpler features such as differing access for getters and setters. Anonymous methods and iterator blocks didn&amp;#39;t really hit me in terms of usefulness until much later - possibly due to using closures in Groovy and more iterators in LINQ. I suspect for many C# 2 developers this is still the case.&lt;/p&gt; &lt;p&gt;My method of learning C# 3 (from the spec, often in rather more detail than normal for the sake of accuracy in writing) is sufficiently far off the beaten track as to make it irrelevant for general purposes, but I wonder how people will learn it in reality. How will it be taught in universities (assuming there are any that teach C#)? How easily will developers moving to C# from Java cope? How about from other languages?&lt;/p&gt; &lt;p&gt;Interestingly, the move from VB 9 to C# 3 is now probably easier than the move from Java 6 to C# 3. Even with the differences in generics between Java and C#, that probably wasn&amp;#39;t true with C# 2.&lt;/p&gt; &lt;p&gt;To get back to C# 4, I&amp;#39;d like the improvements to be somehow blend in so that learning C# 4 from scratch isn&amp;#39;t a significantly different experience to learning C# 3 from scratch. It&amp;#39;s bound to be slightly longer as there will certainly be new features to learn - but if they can be learned alongside existing features, I think that&amp;#39;s a valuable asset. It&amp;#39;s also worth considering how much investment will be required to learn C# 4 from a position of understanding C# 3. Going from C# 2 to C# 3 is a significant task - but it&amp;#39;s one which involves a paradigm shift, and of course the payoffs are massive. I&amp;#39;d be very surprised (and disappointed) to see the same level of change in C# 4, or indeed the same level of payoff. Conservative though this is, I&amp;#39;m after &amp;quot;quick wins&amp;quot; for developers - even if in some cases such as covariance/contravariance the win is &lt;i&gt;far&lt;/i&gt; from quick from the C# design/implementation team&amp;#39;s perspective.&lt;/p&gt; &lt;p&gt;Just to put things into perspective: think about how many new technologies developers have being asked to learn in the last few years - WCF, WPF, Workflow Foundation, Cardspace, Silverlight, AJAX, LINQ, ClickOnce and no doubt other things I&amp;#39;ve forgotten. I feel a bit like Joseph II complaining that Mozart had written &amp;quot;too many notes&amp;quot; - and if you asked me to start exorcising any of these technologies from history I&amp;#39;d be horrified at the prospect. That doesn&amp;#39;t actually make it any easier for us though. I doubt that the pace of change is likely to slow down any time soon in terms of technologies - let&amp;#39;s not make developers feel like complete foreigners in a language they were happy with.&lt;/p&gt; &lt;h3&gt;Keep the language spec understandable&lt;/h3&gt; &lt;p&gt;I know there aren&amp;#39;t many people who look at the language spec, but the C# spec has historically been very, very well written. It&amp;#39;s usually clear (even if it can be hard to find the right section at times) and leaves rigidly defined areas of doubt and uncertainty in appropriate places, while stamping out ambiguity in others. The new unified spec is a great improvement over the &amp;quot;C# 1 ... and then the new bits in C# 2&amp;quot; approach from before. However, it&amp;#39;s growing at a somewhat alarming rate. As it grows, I expect it to become harder to read as a natural consequence - unless particular effort is put into countering that potential problem.&lt;/p&gt; &lt;h3&gt;Stay technology-neutral...&lt;/h3&gt; &lt;p&gt;Okay, this one is aimed fairly squarely at VB9. I know various people love XML literals, but I&amp;#39;m not a fan. It &lt;i&gt;just feels wrong&lt;/i&gt; to have such close ties with a particular technology in the actual language, even one so widely used as XML. (Of course, there&amp;#39;s already a link between XML and C# in terms of documentation comments - but that doens&amp;#39;t feel quite as problematic.)&lt;/p&gt; &lt;p&gt;My first reaction to LINQ (before I understood it) was that C# was being invaded by SQL. Now that I&amp;#39;ve got a much better grasp of query expressions, I have no concern in that area. Perhaps it would be possible to introduce a new hierarchy construct which LINQ to XML understands with ease - or adapt the existing object/collection initializers slightly for this purpose. With some work, it may be possible to do this without restricting it to XML... I&amp;#39;m really just blue-skying though (and this isn&amp;#39;t a feature on my wishlist.)&lt;/p&gt; &lt;h3&gt;... but bear concurrency in mind&lt;/h3&gt; &lt;p&gt;While I don&amp;#39;t like the idea of tying C# to any particular technology, I think the general theme of concurrency is going to be increasingly important. That&amp;#39;s far from insightful - just about every technology commentator in the world is predicting a massively parallel computing landscape in the future. Developers won&amp;#39;t be able to get away with blissful ignorance of concurrency, even if not everyone will need to know the nuts and bolts.&lt;/p&gt; &lt;h3&gt;Make it easier to do the right thing&lt;/h3&gt; &lt;p&gt;This is effectively encouraging developers to &amp;quot;fall into the pit of success&amp;quot;. Often best practices are ignored as being inconvenient or impractical at times, and I&amp;#39;m certainly guilty of that myself. C# has a good history of enabling developers to do the right thing more easily as time progresses: automatic properties, iterator blocks and allowing different getter/setter access spring to mind as examples.&lt;/p&gt; &lt;p&gt;In some ways this is the biggest goal in this manifesto. It&amp;#39;s certainly guided me in terms of encouraging mixin and immutability support, ways of simplifying parameter/invariant checking, and potentially &lt;code&gt;IDisposable&lt;/code&gt; implementation. I like features which don&amp;#39;t require me to learn whole new ways of approaching problems, but let me do what I already knew I &lt;i&gt;should&lt;/i&gt; do, just more easily.&lt;/p&gt; &lt;h2&gt;Wishlist of C# 4 features&lt;/h2&gt; &lt;p&gt;With all that out of the way, what would I like to see in C# 4? Hopefully from the above you won&amp;#39;t be expecting anything earth-shattering - which is a good job, as all of these are reasonably minor modifications. Perhaps we could call it C# 3.5, shipping with .NET 4.0. That would really make life interesting, as people are already referring to C# 3 as C# 3.5 (and C# 2008)...&lt;/p&gt; &lt;h3&gt;Readonly automatic properties&lt;/h3&gt; &lt;p&gt;I&amp;#39;ve mentioned this before, but I&amp;#39;ll give more details here. I&amp;#39;d like to be able to specify &lt;code&gt;readonly&lt;/code&gt; instead of &lt;code&gt;protected&lt;/code&gt;/&lt;code&gt;internal&lt;/code&gt;/&lt;code&gt;private&lt;/code&gt; for the setter access, which would:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Mark the autogenerated backing variable as &lt;code&gt;initonly&lt;/code&gt; in IL  &lt;li&gt;Prevent code outside the constructor from setting the property&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;So, for example:&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; ReadOnlyDemo&lt;br /&gt;{&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; Name { get; &lt;span class="Modifier"&gt;readonly&lt;/span&gt; set; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; ReadOnlyDemo(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; name)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Valid&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Name = name;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; TryToSetName(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; newName)&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="Attention"&gt;// Invalid&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Name = newName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;This would make it easier to write genuinely (and verifiably, as per Joe&amp;#39;s post) immutable classes, or just immutable &lt;i&gt;parts&lt;/i&gt; of classes. As mentioned in previous comments, there could be interesting challenges around serialization and immutability, but frankly they really need to be addressed anyway - immutability &lt;i&gt;is&lt;/i&gt; going to be one part of the toolkit for concurrency, whether it has language support or not. In the generated IL the property would only have a getter - calls to the setter in the constructor would be translated into direct variable sets.&lt;/p&gt; &lt;p&gt;This shouldn&amp;#39;t require a CLR change.&lt;/p&gt; &lt;h3&gt;Property-scoped variables&lt;/h3&gt; &lt;p&gt;I&amp;#39;ve been suggesting this (occasionally) for a long time, but it&amp;#39;s worth reiterating. Every so often, you really want to make sure that no code messes around with a variable other than through a property. This can be solved with discipline, of course - but historically we don&amp;#39;t have a good record on sticking to discipline. Why not get the compiler to enforce the discipline? I would consider code like this:&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Person&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;int&lt;/span&gt; Age&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Variable is not accessible outside the Age property&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ValueType"&gt;int&lt;/span&gt; age;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; age;&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; set&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;if&lt;/span&gt; (value &amp;lt; 0 || value &amp;gt; SomeMaxAgeConstant)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt; ArgumentOutOfRangeException();&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; age = value;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; SetAgeNicely(&lt;span class="ValueType"&gt;int&lt;/span&gt; value)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Perfectly legal&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Age = value;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; SetAgeSneakily(&lt;span class="ValueType"&gt;int&lt;/span&gt; value)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Compilation error!&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; age = value;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Just in case Eric&amp;#39;s reading this: yes, having &lt;code&gt;Age&lt;/code&gt; as a property of a person is a generally bad idea. Specifying a date of birth and calculating the age is a better idea. Really, don&amp;#39;t use this code as a model for a &lt;code&gt;Person&lt;/code&gt; type. However, treat it as a dumb example of a reasonable idea. I need to find myself a better type to use as my first port of call when finding an example...&lt;/p&gt; &lt;p&gt;The variable name would still have to be unique - it would still be the name generated in the IL, for instance. Multiple variables could be declared if required. The generated code could be exactly the same as that of existing code which happened to only use the property to access the variable.&lt;/p&gt; &lt;p&gt;A couple of potential options:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;The variables could be directly accessible during the constructor, potentially. This would help with things like serialization.  &lt;li&gt;Likewise, potentially an attribute could be applied to other members which needed access to the variables. Bear in mind that we&amp;#39;re only trying to save developers from themselves (and their colleagues). We&amp;#39;re not trying to cope with intruders in a security sense. An active &amp;quot;I know I&amp;#39;m violating my own rules&amp;quot; declaration should cause enough discomfort to avoid the accidental issues we&amp;#39;re trying to avoid.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;This shouldn&amp;#39;t require a CLR change.&lt;/p&gt; &lt;h3&gt;Extension properties&lt;/h3&gt; &lt;p&gt;This has been broadly talked about, particularly in view of fluent interfaces. It feels to me that there are two very different reasons for extension properties:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Making fluent interfaces prettier, e.g. &lt;code&gt;19.June(1976) + 8.Hours + 20.Minutes&lt;/code&gt; instead of &lt;code&gt;19.June(1976) + 8.Hours() + 20.Minutes()&lt;/code&gt;  &lt;li&gt;Genuine properties, which of course couldn&amp;#39;t add new state to the extended type, but could access it in a different way.&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Point 1 feels like a bit of a hack, I must admit. It&amp;#39;s using properties not because the result is &amp;quot;property-like&amp;quot; but because we want to miss the brackets off. It&amp;#39;s been pointed out to me that VB already allows this, and that by brackets to be missed out for parameterless methods we could achieve the same effect - but that just feels wrong. Arguably fluent interfaces already mess around with the normal conventions of what methods do and how they&amp;#39;re named, so using properties like this probably isn&amp;#39;t too bad.&lt;/p&gt; &lt;p&gt;Point 2 is a more natural reason for extension properties. As an example, consider a type which exposes a &lt;code&gt;Size&lt;/code&gt; property, but not &lt;code&gt;Width&lt;/code&gt; or &lt;code&gt;Height&lt;/code&gt;. Changing either dimension individually requires setting the Size to a new one with the same value for the other dimension - this is often much harder to read than judicious use of &lt;code&gt;Height&lt;/code&gt;/&lt;code&gt;Width&lt;/code&gt;. I suspect that extension properties would actually be &lt;i&gt;used&lt;/i&gt; for this reason less often than for fluent interfaces, but there may be any number of interesting uses I haven&amp;#39;t thought of.&lt;/p&gt; &lt;p&gt;This shouldn&amp;#39;t require a CLR change, but framework changes may be required.&lt;/p&gt; &lt;h3&gt;Extension method discovery improvements&lt;/h3&gt; &lt;p&gt;I&amp;#39;ve made it clear before now that the way extension methods are discovered (i.e. with &lt;code&gt;using&lt;/code&gt; directives which import all the extension methods of &lt;i&gt;all&lt;/i&gt; the types within the specified namespace) leaves much to be desired. I don&amp;#39;t like trying to reverse bad decisions - it&amp;#39;s pretty hard to do it well - but I really feel strongly about this one. (Interestingly, although I&amp;#39;ve heard many people criticising this choice, I don&amp;#39;t actually remember hearing the C# team defending it. Given that reservations were raised back in 2005, when there was still plenty of time to change stuff, I suspect there are reasons no-one&amp;#39;s thought of. I&amp;#39;d love to hear them some time.)&lt;/p&gt; &lt;p&gt;The goal would be to change from discovering extensions at a namespace to discovering extensions at a type level. (By which I mean at a &amp;quot;type containing extension methods&amp;quot; level - e.g. System.Linq.Enumerable or System.Linq.Queryably. Admittedly discovery on a basis which explicitly specifies the type to extend would also be interesting.) I don&amp;#39;t mind exactly how the syntax works, but the usual ideas are ones such as:&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Namespace"&gt;using&lt;/span&gt; System.Linq.Enumerable;&lt;br /&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; System.Linq.Enumerable;&lt;br /&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; System.Linq.Enumerable; &lt;/div&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;That&amp;#39;s the easy part - the harder part would be working out the best way to phase out the &amp;quot;old&amp;quot; syntax. I would suggest a warning if extension methods are found &lt;i&gt;and used&lt;/i&gt; without being explicitly mentioned by type. In C# 4 this could be a warning which was disabled by default (but could be enabled with pragmas or command line switches), then enabled by default in C# 5 (but with the same options available, this time to disable it). By C# 6 we could perhaps remove the ability to discover extension methods by namespace altogether, so the methods just wouldn&amp;#39;t be found any more.&lt;/p&gt; &lt;p&gt;The C# team could be more aggressive than this, perhaps skipping the first step and making it an enabled warning from C# 4 - but I&amp;#39;m happy to leave that kind of thing to them, without paying it much more attention. I know how seriously they take &lt;a href="http://blogs.msdn.com/ericlippert/archive/tags/Breaking+Changes/default.aspx"&gt;breaking changes&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;No CLR changes required as far as I can see.&lt;/p&gt; &lt;h3&gt;Implicit &amp;quot;select&amp;quot; at end of query expressions&lt;/h3&gt; &lt;p&gt;I can&amp;#39;t say I&amp;#39;ve used VB9&amp;#39;s LINQ support, but I&amp;#39;ve heard about one aspect which has some appeal. In C# 3, every query expression ends with either &amp;quot;select&amp;quot; or &amp;quot;groupby&amp;quot;. The compiler is actually smart enough to ignore a redundant select clause (except for degenerate query expressions ) and indeed the language spec makes this clear. So why require it in the query expression in the first place? As a concrete example of before/after:&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Before&lt;/span&gt;&lt;br /&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; query = &lt;span class="Linq"&gt;from&lt;/span&gt; user &lt;span class="Statement"&gt;in&lt;/span&gt; db.Users&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; user.Age &amp;gt; 18&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;orderby&lt;/span&gt; user.Name&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;select&lt;/span&gt; user;&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// After&lt;/span&gt;&lt;br /&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; query = &lt;span class="Linq"&gt;from&lt;/span&gt; user &lt;span class="Statement"&gt;in&lt;/span&gt; db.Users&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; user.Age &amp;gt; 18&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;orderby&lt;/span&gt; user.Name; &lt;/div&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;This isn&amp;#39;t a massive deal, but it would be quite neat. I worry slightly that there could be significant costs in terms of the specification complexity, however.&lt;/p&gt; &lt;h3&gt;Internal members on internal interfaces&lt;/h3&gt; &lt;p&gt;Interfaces currently only ever have public members, even if the interfaces themselves are internal. This means that implementing an internal interface in an internal class still means making the implementing method public, or using explicit interface implementation (which imposes other restrictions, particularly in terms of overriding). It would be nice to be able to make members internal when the interface itself is internal - either explicitly or implicitly. Implementing such members publicly would still be allowed, but you could choose to keep the implementation internal if desired.&lt;/p&gt; &lt;p&gt;This may require a CLR change - not sure.&lt;/p&gt; &lt;h3&gt;&amp;quot;Namespace+assembly&amp;quot; access restriction&lt;/h3&gt; &lt;p&gt;It&amp;#39;s not an uncommon request on the C# newsgroup for the equivalent of C++&amp;#39;s &amp;quot;friend&amp;quot; feature - where two classes have a special relationship. In many ways InternalsVisibleTo is an assembly-wide version of this feature, but I can certainly see how it would be nice to have a slightly finer grained version. Sometimes two classes are naturally tightly coupled, even though they have distinct responsibilities. Although loose coupling is generally accepted to be a good thing, it&amp;#39;s not always practical. At the same time, giving extra access to &lt;i&gt;all&lt;/i&gt; the types within the same assembly can be a little bit much.&lt;/p&gt; &lt;p&gt;Instead of specifying particular types to share members with, I&amp;#39;d propose a new access level, which would make appropriately decorated members available to other types which are both within the same assembly &lt;i&gt;and&lt;/i&gt; within the same namespace. This would be similar to Java&amp;#39;s &amp;quot;package level&amp;quot; access (the default, for some reason) except without the implicit availability to derived types. (Java&amp;#39;s access levels and defaults are odd to say the least.)&lt;/p&gt; &lt;p&gt;(Of course, this wouldn&amp;#39;t help in assemblies which consisted of types within a single namespace.)&lt;/p&gt; &lt;p&gt;This would almost certainly require a CLR change.&lt;/p&gt; &lt;h3&gt;InternalsVisibleTo simplification for strongly named assemblies&lt;/h3&gt; &lt;p&gt;This one&amp;#39;s just a little niggle. In order to use the InternalsVisibleToAttribute to refer to a strongly named assembly (which you almost always have to do if the declaring assembly is strongly named), you have to specify the public key. Not the public key &lt;i&gt;token&lt;/i&gt; as the documentation claims, but the whole public key. Not only that, but you can&amp;#39;t have any whitespace in it - so you can&amp;#39;t use a verbatim string literal to easily put it in a block. Instead, you either have to have the whole thing on one line, or use compile-time string concatenation to make sure the key is still unbroken.&lt;/p&gt; &lt;p&gt;It&amp;#39;s not often you need to look at the assembly attributes, so it&amp;#39;s far from a major issue - but it&amp;#39;s a mild annoyance which could be fixed with very few downsides.&lt;/p&gt; &lt;p&gt;This may require a CLR change - not sure.&lt;/p&gt; &lt;h2&gt;Is that all?&lt;/h2&gt; &lt;p&gt;I suspect that soon after posting this, I&amp;#39;ll think of other ideas. Some may be daft, some may be more significant than these, but either way I&amp;#39;ll do a new post for new ideas, rather than adding to this one. I&amp;#39;ll update this one for typos, further explanations etc. I suspect if I don&amp;#39;t post this now I&amp;#39;ll keep tweaking it for hours - which is relatively pointless as I&amp;#39;m really trying to provoke discussion rather than presenting polished specification proposals.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1507108" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category></item><item><title>Macros, and languages within languages</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/02/08/macros-and-languages-within-languages.aspx</link><pubDate>Fri, 08 Feb 2008 23:31:32 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1503965</guid><dc:creator>skeet</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1503965</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1503965</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/02/08/macros-and-languages-within-languages.aspx#comments</comments><description>&lt;p&gt;Ian Griffiths mailed me about macros, and explained how LISP macros were very different to C/C++ macros, working at a language level instead of at a text level. I won&amp;#39;t pretend to understand all about what would be possible and what wouldn&amp;#39;t, but Ian gave a good example: query expressions in C# 3. Instead of being part of the language itself, they could apparently have been written as macros, if C# supported them. Then if you wanted to have similar support for different forms of expression, you could just write your own macro library.&lt;/p&gt; &lt;p&gt;Assuming that&amp;#39;s what people are actually requesting, I can certainly see the attraction - but I&amp;#39;d still prefer it if C# didn&amp;#39;t go down that route. I&amp;#39;ll go back to C++ for the guts of the reason why, but it&amp;#39;s not really about macros at this point. It&amp;#39;s about building your own language. Once, someone told me that C++ wasn&amp;#39;t a language - it was a meta-language; no-one used &amp;quot;bare&amp;quot; C++, they worked out their own language made from the building blocks of normal C++, and then used that.&lt;/p&gt; &lt;p&gt;That may or may not be true - or more likely, it&amp;#39;s true in some places but not others - but it scares me as an idea. I&amp;#39;m not going to claim I know every nuance of C#, but it&amp;#39;s pretty rare that you&amp;#39;d throw a line at me without it being reasonably clear what&amp;#39;s going to happen and why, at the language level. Extension methods might mean a bit more information is required as to where a particular method comes from, but it doesn&amp;#39;t take a lot of digging to see what&amp;#39;s going on.&lt;/p&gt; &lt;p&gt;Now imagine that C# 3 didn&amp;#39;t include query expressions, but that someone had come up with them as a macro library. It&amp;#39;s not an insignificant amount of effort to learn what&amp;#39;s going on there, and how it all maps to normal method calls, potentially with expression trees as arguments instead of delegates. Until you understand what&amp;#39;s going on at a reasonably deep level, you can&amp;#39;t really make any firm decisions as to what code including a query expression will do. (Heck, that&amp;#39;s one of the premises of the book: you should really know this stuff, or at least be aware of it.)&lt;/p&gt; &lt;p&gt;That&amp;#39;s fine when there&amp;#39;s a single macro library used globally, but now imagine every company has their own - or worse still, has a bunch of them grabbed from Code Project, possibly including a load of bugs. Most of us aren&amp;#39;t accomplished language designers, and I suspect there&amp;#39;d be an awful lot of macro libraries out there which weren&amp;#39;t quite thought through enough - but were still useful enough to be attractive. They&amp;#39;d become magnets for code warts.&lt;/p&gt; &lt;p&gt;It&amp;#39;s hard enough when you change company to work out what 3rd party libraries are in use, &lt;em&gt;how&lt;/em&gt; they&amp;#39;re being used, what the coding conventions are etc. It would be much worse if I had to learn another flavour of C# itself each time. I&amp;#39;m already worried that developers are picking up C# 3 without having a firm enough grasp of C# 2 - and that&amp;#39;s when there&amp;#39;s just a progression within a single language.&lt;/p&gt; &lt;p&gt;I know this all sounds patronising and/or elitest and/or &amp;quot;nanny state&amp;quot; applied to programming languages - but it&amp;#39;s how I feel nonetheless. I just don&amp;#39;t think we (as a development community) are mature enough to handle that sort of power without it turning into a blood bath. This sort of thing sounds fabulous for hobby and research development, and would probably be great in the hands of the best few companies in the world - but I don&amp;#39;t think it&amp;#39;s a good idea for the mainstream.&lt;/p&gt; &lt;p&gt;Okay - time to hear why I&amp;#39;m wrong :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1503965" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Java/default.aspx">Java</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/General/default.aspx">General</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category></item><item><title>C# 4, part 3: Ideas from Microsoft</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/02/07/c-4-part-3-ideas-from-microsoft.aspx</link><pubDate>Thu, 07 Feb 2008 23:11:37 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1502071</guid><dc:creator>skeet</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1502071</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1502071</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/02/07/c-4-part-3-ideas-from-microsoft.aspx#comments</comments><description>&lt;p&gt;Microsoft haven&amp;#39;t committed to anything in C# 4 yet. However, there have been hints about what they&amp;#39;ve been considering in &lt;a href="http://blogs.msdn.com/ericlippert/"&gt;Eric Lippert&amp;#39;s blog&lt;/a&gt;, and more than hints in &lt;a href="http://blogs.msdn.com/charlie/"&gt;Charlie Calvert&amp;#39;s blog&lt;/a&gt;. There&amp;#39;s not a lot to go on yet, but:&lt;/p&gt; &lt;h3&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/tags/Immutability/default.aspx"&gt;Immutability support&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;Most of Eric&amp;#39;s posts about immutability have so far been about immutable data structures. However, the &lt;a href="http://blogs.msdn.com/ericlippert/archive/2007/11/13/immutability-in-c-part-one-kinds-of-immutability.aspx"&gt;first post in the series&lt;/a&gt; did mention that they&amp;#39;re playing around with immutability from the point of view of potential language support. Joe Duffy also &lt;a href="http://www.bluebytesoftware.com/blog/2007/11/11/ImmutableTypesForC.aspx"&gt;wrote about immutability&lt;/a&gt; at roughly the same time.&lt;/p&gt; &lt;p&gt;What can we expect in terms of support? Possibilities:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Compiler checking via attributes, as per Joe&amp;#39;s posts  &lt;li&gt;Immutable collections to make it easier to sensibly embed collections within immutable types  &lt;li&gt;Readonly automatic properties (&lt;a href="http://msmvps.com/blogs/jon.skeet/archive/2008/02/05/c-4-part-1-looking-back-at-the-past.aspx"&gt;as I mentioned in part 1&lt;/a&gt; - I&amp;#39;ll expand on this in the next post)&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I&amp;#39;m not sure what could be usefully added beyond those. One possibility, however: what about automatic equality and hashcode generation? I touched on this &lt;a href="http://msmvps.com/blogs/jon.skeet/archive/2008/02/07/c-4-part-2-ideas-from-other-community-members.aspx"&gt;last time&lt;/a&gt; when talking about &amp;quot;instant data types&amp;quot; but I don&amp;#39;t see why it shouldn&amp;#39;t be applicable in general. After all:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Immutable types are good candidates for dictionary keys - or to put it the other way round, using mutable types as dictionary keys is a risky idea  &lt;li&gt;If all immutable types automatically override GetHashCode and Equals, and immutable types can only compose other immutable types, everything should just work  &lt;li&gt;The obvious implementation of Equals and a &amp;quot;multiply, add, repeat&amp;quot; implementation of GetHashCode are pretty reasonable for many, many types. You could always manually override the methods if necessary.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;One downside: Equality doesn&amp;#39;t really work well when there&amp;#39;s an inheritance hierarchy involved. Read Josh Bloch&amp;#39;s &amp;quot;&lt;a href="http://www.amazon.com/Effective-Java-Programming-Language-Guide/dp/0201310058"&gt;Effective Java&lt;/a&gt;&amp;quot; for a detailed discussion. (Ooh, new edition coming out soon. Should make for great reading.)&lt;/p&gt; &lt;h3&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx"&gt;Generic variance support&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;Once again, Eric has posted rather a lot on this, concentrating on two aspects: interfaces and delegates. I believe these are already supported in the CLR (I&amp;#39;m sure interfaces are, and I suspect delegates are too).&lt;/p&gt; &lt;p&gt;Just to be clear, I don&amp;#39;t think anyone should expect unsafe covariance/contravariance. The following code still won&amp;#39;t compile (at least unless things go very differently to how I expect):&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Attention"&gt;// Still won&amp;#39;t work!&lt;/span&gt;&lt;br /&gt;IList&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; strings = &lt;span class="Keyword"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;IList&amp;lt;&lt;span class="ReferenceType"&gt;object&lt;/span&gt;&amp;gt; objects = strings;&lt;br /&gt;objects.Add(&lt;span class="Keyword"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;object&lt;/span&gt;()); &lt;span class="InlineComment"&gt;// Oww!&lt;/span&gt; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;This code, however, would be okay, due to the &amp;quot;readonly&amp;quot; nature of iteration:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Should be okay - can&amp;#39;t break type safety&lt;/span&gt;&lt;br /&gt;IEnumerable&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; strings = &lt;span class="Keyword"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;IEnumerable&amp;lt;&lt;span class="ReferenceType"&gt;object&lt;/span&gt;&amp;gt; objects = strings; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;That&amp;#39;s the interface side. For delegates, this would probably be possible:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Contravariance of input parameters to delegates&lt;/span&gt;&lt;br /&gt;Action&amp;lt;&lt;span class="ReferenceType"&gt;object&lt;/span&gt;&amp;gt; objectAction = (x =&amp;gt; Console.WriteLine(x));&lt;br /&gt;Action&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; stringAction = objectAction;&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// Covariance of returns (and output parameters?) from delegates&lt;/span&gt;&lt;br /&gt;Func&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; stringFunc = () =&amp;gt; &lt;span class="String"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;; &lt;br /&gt;Func&amp;lt;&lt;span class="ReferenceType"&gt;object&lt;/span&gt;&amp;gt; objectFunc = stringFunc; &lt;/div&gt; &lt;p&gt;All of this is conceptually good. It&amp;#39;s more for people to understand, of course, but it&amp;#39;s still a useful thing to have available.&lt;/p&gt; &lt;h3&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2008/01/25/future-focus.aspx"&gt;Dynamic calling support&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;This one really surprised me - to the extent that I&amp;#39;ll now need to edit the last chapter in the book so as not to look stupid when it&amp;#39;s published. (It&amp;#39;s fine to be wrong when predicting the future, but making a prediction which has already been proven false before publication is embarrassing.) I expected C# to stay fully static forever. However, it looks like the C# team is at least strongly considering making dynamic &lt;em&gt;calling &lt;/em&gt;support available. Read the blog for more details, but note what &lt;em&gt;isn&amp;#39;t&lt;/em&gt; included: C# &lt;em&gt;reacting&lt;/em&gt; dynamically. In other words, Ayende&amp;#39;s beloved &lt;code&gt;IDynamicObject&lt;/code&gt; support isn&amp;#39;t being proposed (at the moment) - although I guess there&amp;#39;s always a possibility that the DLR support will somehow be available through &amp;quot;normal&amp;quot; C# without extra compiler support.&lt;/p&gt; &lt;p&gt;This will disappoint some people of course, but I&amp;#39;m happy enough. I can&amp;#39;t see myself using the new support particularly often, and I&amp;#39;m slightly worried at the extra complexity required in the language spec to explain what it will actually do, but I&amp;#39;m likely to treat it in roughly the same way as unsafe blocks - something to ignore most of the time.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h2&gt;Conclusion&lt;/h2&gt; &lt;p&gt;As you can see, there&amp;#39;s not a lot really on the table yet. That in itself is quite interesting, however - as well as the nature of the changes. I believe that the changes from C# 3 to 4 will be much more like those from 1 to 2 than 2 to 3. Think about when VS 2005 was released - C# 3 extensions were already available in CTP form for the VS 2005 beta. Here we are with VS 2008 having RTM&amp;#39;d a while ago, and we only have a few ideas to mull over. Now, I&amp;#39;m sure that there are implementations of all of the above features and almost certainly more - but they&amp;#39;re not putting them out just yet. Hopefully we&amp;#39;ll learn more over the next few months.&lt;/p&gt; &lt;p&gt;The big difference with C# 3 was that there was a grand plan: LINQ. Almost every feature in C# 3 (basically not automatic properties or partial methods) supports LINQ in some way or other. I don&amp;#39;t see a big plan for C# 4. That&amp;#39;s a very good thing, in my view. We need time - quite a lot of time, I suspect - to digest LINQ. Awful as the phrase is, LINQ has the potential to be a paradigm shift in development. Those shouldn&amp;#39;t come along too often. Disparate changes can still be incredibly useful of course, so let&amp;#39;s not lack ambition for C# 4 - but I&amp;#39;m expecting idiomatic C# 4 to still be &lt;em&gt;roughly&lt;/em&gt; similar to idiomatic C# 3, which certainly couldn&amp;#39;t be said of C# 3 to C# 2.&lt;/p&gt; &lt;p&gt;Only one more post in the immediate future: my own ideas for C# 4 (or at least those which I&amp;#39;ve come up with independently, even if others have mentioned them too).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1502071" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category></item><item><title>C# 4, part 2: Ideas from other community members</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/02/07/c-4-part-2-ideas-from-other-community-members.aspx</link><pubDate>Thu, 07 Feb 2008 21:01:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1501927</guid><dc:creator>skeet</dc:creator><slash:comments>23</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1501927</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1501927</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/02/07/c-4-part-2-ideas-from-other-community-members.aspx#comments</comments><description>&lt;p&gt;There has been a fair amount of speculation online about what should be in C# 4. I&amp;#39;ve taken the list below from a few posts, primarily those by &lt;a href="http://www.ayende.com/Blog/archive/2007/11/26/C-vNext.aspx"&gt;Ayende&lt;/a&gt; and &lt;a href="http://codebetter.com/blogs/jeremy.miller/archive/2007/11/25/what-do-you-want-in-c-vnext.aspx"&gt;Jeremy Miller&lt;/a&gt;. I&amp;#39;ve deliberately left out the ideas that Microsoft have mentioned that they&amp;#39;re at least considering - they&amp;#39;ll come in the next post.&lt;/p&gt; &lt;h3&gt;Mixins&lt;/h3&gt; &lt;p&gt;I suspect everyone has a different idea of what these mean, but I&amp;#39;ll say what &lt;i&gt;I&amp;#39;d&lt;/i&gt; like. I want to be able to implement an interface by proxying all calls (other than those I&amp;#39;ve actually implemented) to a particular member variable, as an aid to favouring composition over inheritance. As an example, here&amp;#39;s a class which implements &lt;code&gt;IList&amp;lt;string&amp;gt;&lt;/code&gt; but makes sure that only strings with length 5 or more can be added:&lt;/p&gt; &lt;p&gt;&amp;nbsp;&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; LongStringList : IList&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [MixinImplementation(&lt;span class="Keyword"&gt;typeof&lt;/span&gt;(IList&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;))]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;readonly&lt;/span&gt; IList&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; m_list = &lt;span class="Keyword"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ValueType"&gt;void&lt;/span&gt; Add(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; item)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;if&lt;/span&gt; (item.Length &amp;lt; 5)&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; ArgumentException(&lt;span class="String"&gt;&amp;quot;Only strings of length &amp;gt;= 5 allowed&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_list.Add(item);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;(Yes, you&amp;#39;d need to implement the setter as well to prevent other values being replaced. This is just sample code to give the rough flavour - and the syntax is pretty much made up as an example too. I&amp;#39;m not hung up on the syntax, I just want the functionality.&lt;/p&gt; &lt;p&gt;You could of course derive from &lt;code&gt;System.ObjectModel.Collection&amp;lt;string&amp;gt;&lt;/code&gt; - but this prevents you from deriving from any other class, and fixes the inheritance forever. If you only really want to provide your clients with an &lt;code&gt;IList&amp;lt;string&amp;gt;&lt;/code&gt; implementation, it&amp;#39;s nicer not to pin anything down. At a later date you could manually implement more of the interface members instead of proxying them, without changing any of the calling code. &lt;/p&gt; &lt;h3&gt;Symbols&lt;/h3&gt; &lt;p&gt;I don&amp;#39;t see the benefit over normal string interning here. That could just be because of a poor description of symbols in Ruby, admittedly... but I suspect any other benefit wouldn&amp;#39;t meet the &amp;quot;it&amp;#39;s got to be really useful in many situations&amp;quot; bar.&lt;/p&gt; &lt;h3&gt;Hashes&lt;/h3&gt; &lt;p&gt;I&amp;#39;ve only extensively used one language with hashes built in: &lt;a href="http://groovy.codehaus.org"&gt;Groovy&lt;/a&gt;. While I agree it&amp;#39;s nice occasionally, I don&amp;#39;t think it&amp;#39;s worth bending the language out of shape for as we&amp;#39;ve now got collection initializers anyway:&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class="code"&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; hash = &lt;span class="Keyword"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;,&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;span class="String"&gt;&amp;quot;First&amp;quot;&lt;/span&gt;, 1 },&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;span class="String"&gt;&amp;quot;Second&amp;quot;&lt;/span&gt;, 2}&lt;br /&gt;}; &lt;/div&gt; &lt;h3&gt;Automatic delegation&lt;/h3&gt; &lt;p&gt;To be honest I don&amp;#39;t really know what Jeremy means here - although it&amp;#39;s possible that he means what I understand as mixins. Ah the joys of loose terminology.&lt;/p&gt; &lt;h3&gt;Metaprogramming&lt;/h3&gt; &lt;p&gt;I only have vague ideas of what metaprogramming is all about, and those are mostly through Ayende&amp;#39;s blog. I can see that it&amp;#39;s almost certainly very powerful, but I&amp;#39;m not sure I want it in C#. I don&amp;#39;t want C# to turn into a massive box with every nifty feature ever considered. It&amp;#39;s possible I could be turned on this one, if someone showed me it working really nicely.&lt;/p&gt; &lt;h3&gt;Macros&lt;/h3&gt; &lt;p&gt;Ick, no. I&amp;#39;ve seen what macros tend to be used for. I&amp;#39;m sure there are nice shiny reasons for them, but certainly in the C/C++ form I&amp;#39;d be heavily against them.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; &lt;a href="http://www.interact-sw.co.uk/iangblog/"&gt;Ian Griffiths&lt;/a&gt; mailed me drawing my attention to LISP macros and how different they are to C/C++ macros. The way Ian described it sounds similar to what I understand of the metaprogramming that Ayende wants to do. I can see why it&amp;#39;s a powerful tool... but personally I think I&amp;#39;d rather keep it away from a mainstream language like C#. I&amp;#39;ll be writing another blog post to explain my view on this, because it&amp;#39;s worthy of a much fuller discussion. &lt;/p&gt; &lt;h3&gt;Everything virtual by default&lt;/h3&gt; &lt;p&gt;Absolutely not! Yes, it would make mocking easier - but then making everything public by default would probably make things easier too. Inheritance is hard to control properly, and should only be done with very careful design. As I wrote in the previous post, I&amp;#39;d prefer classes to be sealed by default, i.e. a step in the opposite direction. Oh, there&amp;#39;s the performance implication too, which is one of the reason&amp;#39;s Java needs a much more complicated multi-pass JIT - to allow even virtual methods to be inlined until they&amp;#39;re actually overridden. The performance part is much, much less important than the &amp;quot;inheritance is powerful but easy to misuse&amp;quot; argument.&lt;/p&gt; &lt;p&gt;Not only should the default not change at this point, but it was the right default to start with.&lt;/p&gt; &lt;h3&gt;Instant Data Type&lt;/h3&gt; &lt;p&gt;This would basically be a way of using anonymous types at a higher level - returning them with a return type of &lt;code&gt;var&lt;/code&gt;, for instance. I don&amp;#39;t support that proposal per se, but I &lt;i&gt;can&lt;/i&gt; see a benefit in having &amp;quot;named anonymous types&amp;quot; - classes which have the same behaviour as anonymous types (in terms of immutability, equality, hash codes etc) but in a named manner. Something like this:&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Person = &lt;span class="Keyword"&gt;new&lt;/span&gt; {&lt;span class="ReferenceType"&gt;string&lt;/span&gt; Name, DateTime DateOfBirth}&lt;br /&gt;...&lt;br /&gt;Person p = &lt;span class="Keyword"&gt;new&lt;/span&gt; Person { Name = &lt;span class="String"&gt;&amp;quot;Jon&amp;quot;&lt;/span&gt;, DateOfBirth = 19.June(1976) };&lt;br /&gt;Person p2 = &lt;span class="Keyword"&gt;new&lt;/span&gt; Person { Name = &lt;span class="String"&gt;&amp;quot;Jon&amp;quot;&lt;/span&gt;, DateOfBirth = 19.June(1976) };&lt;br /&gt;&lt;br /&gt;Assert.IsTrue(p.Equals(p2));&lt;br /&gt;Assert.AreEqual(p.GetHashCode(), p2.GetHashCode());&lt;br /&gt;&lt;span class="InlineComment"&gt;// etc&lt;/span&gt; &lt;/div&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Again, the syntax isn&amp;#39;t terribly important to me - but the ability to define very simple immutable data objects is nice. It could also improve the readability of some LINQ code as you could make the meaning of the (currently anonymous) tuple clear in the name.&lt;/p&gt; &lt;p&gt;A few anticipated comebacks:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Clash with object initializers: yes, it looks like it&amp;#39;s setting properties rather than passing them in as constructor arguments. That&amp;#39;s unfortunate, and maybe parentheses would be better than braces here. That would require named parameters though. (I&amp;#39;ll come onto those in another post!)  &lt;/li&gt;&lt;li&gt;Why not just refactor the anonymous type to a named type? ReSharper lets you do this! Indeed it does - but then you&amp;#39;ve got a complete class to maintain. Given a single line of code, I know the features of the &lt;code&gt;Person&lt;/code&gt; class. I can add a new property (breaking existing uses, of course) without having to make sure I get the equality and hash code implementations right manually, etc. I prefer simplicity of language expression over just saving typing by using snippets etc - that&amp;#39;s why I like automatic properties.  &lt;/li&gt;&lt;li&gt;It can&amp;#39;t use quite the same implementation as anonymous types. Indeed, anonymous types are quite interesting in terms of the number of types actually generated in the IL, due to sharing of generics. I don&amp;#39;t think it would be a great loss in this case though.  &lt;/li&gt;&lt;li&gt;The use still isn&amp;#39;t as brief as with anonymous types, due to needing to specify the name. True, but unavoidable, I think.&lt;/li&gt;&lt;/ol&gt; &lt;h3&gt;MemberInfo (infoof)&lt;/h3&gt; &lt;p&gt;I don&amp;#39;t think the C# team have actually stated that this is even potentially on the table, but one of the lovely things about having Eric Lippert as a tech reviewer for the book is I get to hear all kinds of anecdotes about what&amp;#39;s been considered before. Some of them will be on the book&amp;#39;s website in the notes section. In this case, I don&amp;#39;t think it&amp;#39;s a problem to reveal that the C# team have considered this before as an &lt;code&gt;infoof&lt;/code&gt; operator (commonly pronounced &amp;quot;in-foof&amp;quot; of course).&lt;/p&gt; &lt;p&gt;I could go for this idea - it would certainly make reflection simpler in a number of cases.&lt;/p&gt; &lt;h3&gt;Method Interception and IDynamicObject&lt;/h3&gt; &lt;p&gt;I&amp;#39;ve lumped these two together as they&amp;#39;re similiar (in my view) - they&amp;#39;re leading down the road to a dynamic language. I can appreciate the benefits of dynamic languages, but that doesn&amp;#39;t mean I think every language ought to be dynamic. I&amp;#39;d pass on these two.&lt;/p&gt; &lt;h3&gt;Static interfaces&lt;/h3&gt; &lt;p&gt;I&amp;#39;m not entirely sure what Ayende means on this front, but I know I&amp;#39;ve seen a number of requests for the ability to declare that a type definitely has a given static method. Indeed, I&amp;#39;ve wanted it myself a few times. However, I&amp;#39;m not sure how I&amp;#39;d go about &lt;i&gt;using&lt;/i&gt; it. Interfaces by their current nature are used when we&amp;#39;ve got an instance. We already know how to pass references etc around - but not types, other than as either type parameters or &lt;code&gt;Type&lt;/code&gt; objects.&lt;/p&gt; &lt;p&gt;Now, having just written it I wonder whether that&amp;#39;s what Ayende means - if a type parameter is constrained to implement a particular interface, any static methods within that interface could be called using the type parameter. I can see the use in a few situations, but I&amp;#39;d need to be convinced that it was common enough to warrant a language change. The bar wouldn&amp;#39;t be too high for me on this one though, as I think we could use very natural syntax without having to make up anything significantly new.&lt;/p&gt; &lt;h3&gt;Aspect-Oriented Programming&lt;/h3&gt; &lt;p&gt;Ooh, tricky one. I&amp;#39;m definitely undecided on this. I can see benefits, but also drawbacks in terms of how obvious the flow of the code is, etc - all the normal objections.&lt;/p&gt; &lt;p&gt;I think I&amp;#39;d welcome additions to the framework and/or runtime to make AOP support simpler, but then leave it to IoC containers etc to actually implement, rather than embedding AOP directly in the language.&lt;/p&gt; &lt;h3&gt;Design by Contract&lt;/h3&gt; &lt;p&gt;There are parts of DbC that I&amp;#39;d really like to see in the language, or possibly as a language/framework mixture where the framework describes certain common attributes (invariants, non-null arguments etc) and then each compiler takes note of the same attributes. I would really, really like to get rid of having manually-written trivial argument checking in my code. I don&amp;#39;t think I&amp;#39;d immediately want to go as far as Spec# though, in terms of trying to deduce correctness. I wouldn&amp;#39;t like to say why, beyond unfamiliarity (which I know isn&amp;#39;t a good reason). Again, I could possibly be persuaded.&lt;/p&gt; &lt;h3&gt;IDisposable implementation support&lt;/h3&gt; &lt;p&gt;Good idea. It&amp;#39;s a pain to implement &lt;code&gt;IDisposable&lt;/code&gt; properly - some help would be welcome. It would probably need to be flexible enough to allow the developer to say whether a finalizer was required or not, and possibly some other things - but in principle, I&amp;#39;m in favour.&lt;/p&gt; &lt;h3&gt;Constructor inheritance&lt;/h3&gt; &lt;p&gt;Aargh, no. Constructors effectively belong to the type rather than instances of the type, so they&amp;#39;re not inherited in the same way. They&amp;#39;re a bit like static members - and I know we can call static members as if they were inherited as normal (e.g. &lt;code&gt;UnicodeEncoding.ASCII&lt;/code&gt;), but it&amp;#39;s generally a bad idea to do so in my view.&lt;/p&gt; &lt;p&gt;Also consider the lack of control. &lt;code&gt;System.Object&lt;/code&gt; has a parameterless constructor - so should all types do so as well, given that they all inherit (directly or indirectly) from &lt;code&gt;System.Object&lt;/code&gt;? What would &lt;code&gt;new FileStream()&lt;/code&gt; really mean? I suppose one possibility would be to mark your type as &lt;i&gt;intentionally&lt;/i&gt; inheriting constructors - which is all very well until the base class adds a new constructor you don&amp;#39;t want, and you don&amp;#39;t realise it until it&amp;#39;s too late. On this one the complexities and disadvantages outweigh the advantages for me.&lt;/p&gt; &lt;h3&gt;&amp;quot;Const correctness&amp;quot;&lt;/h3&gt; &lt;p&gt;I haven&amp;#39;t actually seen anyone asking for this specifically for C# 4, but it&amp;#39;s been a general feature request pretty much forever. Again, I can see the benefits but:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;I suspect it&amp;#39;s the kind of thing you really need to get right in V1.0 for it to be genuinely useful.&lt;/li&gt; &lt;li&gt;I still haven&amp;#39;t seen an easy way to express &amp;quot;this is an immutable reference to a mutable list of immutable objects of a particular type&amp;quot;. Basically you need to express &amp;quot;constness&amp;quot; for every level down the composition hierarchy, which isn&amp;#39;t simple.&lt;/li&gt;&lt;/ol&gt; &lt;h2&gt;&amp;nbsp;&lt;/h2&gt; &lt;h2&gt;Conclusion&lt;/h2&gt; &lt;p&gt;Just to wrap the above up, here are the above features in &amp;quot;yes, maybe, no&amp;quot; categorization (just for my own view, of course):&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Yes: Mixins, instant data types, IDisposable implementation, design by contract (partial), infoof&lt;/li&gt; &lt;li&gt;Maybe: Automatic delegation, metaprogramming, static interfaces&lt;/li&gt; &lt;li&gt;No: Symbols, hashes, everything virtual by default, macros, constructor inheritance, AOP, method interception and IDynamicObject&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Next time (which may be tonight if I&amp;#39;m feeling energetic) I&amp;#39;ll look at what Microsoft has hinted at.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1501927" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category></item><item><title>C# 4, part 1: Looking back at the past</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/02/05/c-4-part-1-looking-back-at-the-past.aspx</link><pubDate>Tue, 05 Feb 2008 19:45:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1497875</guid><dc:creator>skeet</dc:creator><slash:comments>21</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1497875</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1497875</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/02/05/c-4-part-1-looking-back-at-the-past.aspx#comments</comments><description>&lt;p&gt;Everyone else is speculating about what&amp;#39;s going to be in C# 4 (and various possibilities are coming out of MS), so I thought it would be wise to start my own series of wishlist posts before I miss the boat completely.&lt;/p&gt; &lt;p&gt;In this first post, I&amp;#39;m not going to look at the future at all - I&amp;#39;m going to look at mistakes of the past. When I say &amp;quot;mistake&amp;quot; I of course mean &amp;quot;things I would have done differently had I been a language designer with 20/20 hindsight&amp;quot;. Of course, there&amp;#39;s a lot of room for argument :)&lt;/p&gt; &lt;p&gt;&lt;b&gt;Mistakes in C# 1 &lt;/b&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Lack of separate getter/setter access for properties. This came in C# 2, but it should have been obvious that it was highly desirable long before C# 1 came out.&lt;/li&gt; &lt;li&gt;Lack of generics - ish. Don&amp;#39;t worry, I&amp;#39;m not going to claim that all the features of C# 2 and 3 should have been in C# 1, but if generics had been in at the start we could have avoided having the non-generic collections (and interfaces) completely. Mind you, I&amp;#39;m glad that the .NET team took their time instead of including the bodged (IMO) generics of Java 5.&lt;/li&gt; &lt;li&gt;Classes not being sealed by default. I&amp;#39;ve believed for a long time that &lt;a href="http://msmvps.com/blogs/jon.skeet/archive/2006/03/04/inheritancetax.aspx"&gt;allowing inheritance incurs a design cost&lt;/a&gt; (and it&amp;#39;s not like I&amp;#39;m unique in that respect). C# fixed a mistake of Java by making methods non-virtual by default; the same should be true for classes in my view.&lt;/li&gt; &lt;li&gt;Enums just being named numbers. Again, &lt;a href="http://msmvps.com/blogs/jon.skeet/archive/2006/01/05/classenum.aspx"&gt;I&amp;#39;ve blogged about this before&lt;/a&gt;, but it&amp;#39;s worth mentioning again. It&amp;#39;s possible to work around the lack of this feature (as the blog post readers pointed out!) but framework and language support would have been very welcome.&lt;/li&gt; &lt;li&gt;The &amp;quot;\x&amp;quot; character escape sequence. Fortunately it&amp;#39;s rarely used, but it&amp;#39;s &lt;i&gt;so&lt;/i&gt; error-prone. Quick, how different are &amp;quot;\x8Good compiler&amp;quot;, &amp;quot;\x8Bad compiler&amp;quot;? What&amp;#39;s the first character in each string? (This will appear soon on my &lt;a href="http://pobox.com/%7Eskeet/csharp/teasers.html"&gt;brainteasers&lt;/a&gt; page).&lt;/li&gt; &lt;li&gt;The switch statement. There are lots of ways in which this could have been better designed. VB addresses some of them (such as making it easier to express multiple matching values) but there are other ways in which this construct needed overhauling. Fallthrough is (rightly) prohibited, so why not just force braces round the code in the case block instead of requiring a break statement? Aside from anything else, that would fix the somewhat bizarre scoping rules.&lt;/li&gt; &lt;li&gt;Wacky overload resolution. I entirely understand the point that introducing new methods in a base type shouldn&amp;#39;t change the behaviour in derived types - but if you&amp;#39;ve explicitly chosen to override that method, that should be more easily callable than it is. (See the first example of the &lt;a href="http://pobox.com/%7Eskeet/csharp/teasers.html"&gt;brainteasers&lt;/a&gt; page to see what I&amp;#39;m talking about.)&lt;/li&gt; &lt;li&gt;The &amp;quot;lock&amp;quot; keyword, and associated issues. Basically, the IDisposable pattern should have been used for locking, and not every object should have a monitor associated with it. Developers should keep a close eye on what&amp;#39;s being locked, and being able to lock on everything takes away from this. Likewise &amp;quot;lock&amp;quot; creates a keyword for little purpose (and one which would otherwise be useful as a variable name etc).&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;b&gt;Mistakes in C# 2&lt;/b&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Lack of partial methods. I&amp;#39;m really only saying this because it broke the format of C# in Depth slightly. I&amp;#39;ve introduced partial methods along with partial types because they logically fit in with them, and they don&amp;#39;t fit in with any of the other features of C# 3 particularly. This is just a matter of not working out all of how partial types would be used - or at least not doing so early enough. (For all I know partial methods were on the table before C# 2 shipped - I wouldn&amp;#39;t be surprised.)&lt;/li&gt; &lt;li&gt;Possibly the lack of generic variance. This is certainly a big issue of understanding which is often raised as a question. On the other hand, I suspect that if/when it becomes available, it will raise just as many questions in terms of the detail anyway...&lt;/li&gt; &lt;li&gt;The System.Nullable class. It&amp;#39;s really only there as an historical accident, and I know it&amp;#39;s not a C# issue as such - but even so... (Note for extra clarity - I&amp;#39;m fine with nullable types and the System.Nullable&amp;lt;T&amp;gt; struct. It&amp;#39;s just the supporting class that I don&amp;#39;t like.)&lt;br /&gt;&lt;/li&gt; &lt;li&gt;InternalsVisibleTo requiring the whole public key for strongly signed assemblies instead of the public key token (contrary to the documentation). Ick.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;b&gt;Mistakes in C# 3&lt;/b&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;It&amp;#39;s a real shame that readonly automatic properties aren&amp;#39;t in C# 3. I suspect they&amp;#39;ll come in C# 4 (and they&amp;#39;ll be on my wishlist in future posts) but I think it&amp;#39;s reasonable to wonder why they weren&amp;#39;t included in C# 3. Immutability is a known pattern of goodness, and although C# 4 may well contain any number of more significant improvements towards making it easier, readonly automatic properties would have been a good start.&lt;/li&gt; &lt;li&gt;The way that extension methods are found. This issue was raised time and time again before release, and I&amp;#39;ve never heard a good defence of finding them by whole namespace, instead of allowing developers to say &amp;quot;use the extension methods found in &lt;i&gt;this&lt;/i&gt; class, please&amp;quot;. As it is, anyone writing their own extension methods is likely to end up with whole namespaces devoted to a single type. It&amp;#39;s very odd.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Of course that&amp;#39;s not to say there aren&amp;#39;t other things I&amp;#39;d like to see - but these are more &amp;quot;features which were slightly misdesigned&amp;quot; rather than &amp;quot;features which I really want&amp;quot;.&lt;/p&gt; &lt;p&gt;I&amp;#39;m not trying to take anything away from the language designers - C# is still easily my favourite language in terms of its design, particularly in C# 3, but nobody&amp;#39;s perfect :)&lt;/p&gt; &lt;p&gt;Next time I&amp;#39;ll start giving my opinions of features that other people are calling for.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1497875" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category></item></channel></rss>