Your browser may have trouble rendering this page. See supported browsers for more information.

This page shows the source for this entry, with WebCore formatting language tags and attributes highlighted.

Title

C# 6 Features and C# 7 Design Notes

Description

Microsoft has recently made a lot of their .NET code open-source. Not only is the code for many of the base libraries open-source but also the code for the runtime itself. On top of that, basic .NET development is now much more open to community involvement. In that spirit, even endeavors like designing the features to be included in the next version of C# are online and open to all: <a href="https://github.com/dotnet/roslyn/issues/98" source="GitHub" author="Mads Torgerson">C# Design Meeting Notes for Jan 21, 2015</a>. <h>C# 6 Recap</h> You may be surprised at the version number "7"---aren't we still waiting for C# 6 to be officially released? Yes, we are. If you'll recall, the primary feature added to C# 5 was support for asynchronous operations through the <c>async</c>/<c>await</c> keywords. Most .NET programmers are only getting around to using this rather far- and deep-reaching feature, to say nothing of the new C# 6 features that are <i>almost</i> officially available. C# 6 brings the following features with it and can be used in the CTP versions of <a href="https://www.visualstudio.com/en-us/downloads/visual-studio-2015-downloads-vs.aspx">Visual Studio 2015</a> or downloaded from <a href="https://github.com/dotnet/roslyn" source="GitHub">the Roslyn project</a>. Some of the more <a href="http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx">interesting features of C# 6</a> are: <ul> <b>Auto-Property Initializers</b>: initialize a property in the declaration rather than in the constructor or on an otherwise unnecessary local variable. <b>Out Parameter Declaration</b>: An <c>out</c> parameter can now be declared inline with <c>var</c> or a specific type. This avoids the ugly variable declaration outside of a call to a <c>Try*</c> method. <b>Using Static Class</b>: <c>using</c> can now be used with with a static class as well as a namespace. Direct access to methods and properties of a static class should clean up some code considerably. <b>String Interpolation</b>: Instead of using <c>string.Format()</c> and numbered parameters for formatting, C# 6 allows expressions to be embedded directly in a string (รก la PHP): e.g. <c>"{Name} logged in at {Time}"</c> <b>nameof()</b>: This language feature gets the name of the element passed to it; useful for data-binding, logging or anything that refers to variables or properties. <b>Null-conditional operator</b>: This feature reduces conditional, null-checking cruft by returning <c>null</c> when the target of a call is <c>null</c>. E.g. <c>company.People?[0]?.ContactInfo?.BusinessAddress.Street</c> includes three null-checks </ul> <h>Looking ahead to C# 7</h> If the idea of using <c>await</c> correctly or wrapping your head around the C# 6 features outlined above doesn't already make your poor head spin, then let's move on to language features that aren't even <i>close</i> to being implemented yet. That said, the <a href="https://github.com/dotnet/roslyn/issues/98" source="GitHub" author="Mads Torgerson">first set of design notes for C# 7</a> include several interesting ideas as well. <ul> <b>Pattern-matching</b>: C# has been ogling its similarly named colleague F# for a while. One of the major ideas on the table for C# is improving the ability to represent as well as match against various types of pure data, with an emphasis on immutable data. <div><b>Metaprogramming</b>: Another focus for C# is reducing boilerplate and capturing common code-generation patterns. They're thinking of delegation of interfaces through composition. Also welcome would be an improvement in the expressiveness of generic constraints. Related User Voice issues: <ul> <a href="http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2122427-expand-generic-constraints-for-constructors">Expand Generic Constraints for constructors</a> <a href="http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2315417-proper-generic-type-alising">[p]roper (generic) type ali[a]sing</a> </ul> </div> <b>Controlling Nullability</b>: Another idea is to be able to declare reference types that can never be <c>null</c> at compile-time (where reasonable---they do acknowledge that they may end up with a <iq>less ambitious approach</iq>). <b>Readonly parameters and locals</b>: Being able to express when change is allowed is a powerful form of expressiveness. C# 7 may include the ability to make local variables and parameters readonly. This will help avoid accidental side-effects. <div><b>Lambda capture lists</b>: One of the issues with closures is that they currently just <i>close</i> over any referenced variables. The compiler just makes this happen and for the most part works as expected. When it doesn't work as expected, it creates subtle bugs that lead to leaks, race conditions and all sorts of hairy situations that are difficult to debug. If you throw in the increased use of and nesting of lambda calls, you end up with subtle bugs buried in frameworks and libraries that are nearly impossible to tease out. The idea of this feature is to allow a lambda to <i>explicitly</i> capture variables and perhaps even indicate whether the capture is read-only. Any additional capture would be flagged by the compiler or tools as an error.</div> <div><b>Contracts(!)</b>: And, finally, this is the feature I'm most excited about because I've been waiting for integrated language support for Design by Contract for literally <i>decades</i><fn>, ever since I read the <a href="http://www.amazon.com/Object-Oriented-Software-Construction-Book-CD-ROM/dp/0136291554/" source="Amazon">Object-Oriented Software Construction 2</a> (OOSC2) for the first time. The design document doesn't say much about it, but mentions that <iq>.NET already has a contract system</iq>, the weaknesses of which I've <a href="{app}view_article.php?id=2183">written about before</a>. Torgersen writes: <bq>When you think about how much code is currently occupied with arguments and result checking, this certainly seems like an attractive way to reduce code bloat and improve readability.</bq> ...and <i>expressiveness</i> and <i>provability</i>! There are a bunch of User Voice issues that I can't encourage you enough to vote for so we can finally get this feature: <ul> <a href="http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2304022-integrate-code-contract-keywords-into-the-main-ne">Integrate Code Contracts more deeply in the .NET Framework</a> <a href="http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2304022-integrate-code-contract-keywords-into-the-main-ne">Integrate Code Contract Keywords into the main .Net Languages</a> </ul> </div> </ul> With some or all of these improvements, C# 7 would move much closer to a provable language at compile-time, an improvement over being a safe language at run-time. We can already indicate that instance data or properties are readonly. We can already mark methods as static to prevent the use of <c>this</c>. We can use ReSharper <c>[NotNull]</c> attributes to (kinda) enforce non-null references without using structs and incurring the debt of value-passing and -copying semantics. I'm already quite happy with C# 5, but if you throw in some or all of the stuff outlined above, I'll be even happier. I'll still have stuff I can think of to increase expressiveness---covariant return types for polymorphic methods or anchored types or relaxed contravariant type-conformance---but this next set of features being discussed sounds really, really good. <hr> <ft>I love the features of the language <a href="https://www.eiffel.com">Eiffel</a>, but haven't ever been able to use it for work. The tools and IDE are a bit stuck in the past (very dated on Windows; X11 required on OS X). The language is super-strong, with native support for contracts, anchored types, null-safe programming, contravariant type-conformance, covariant return types and probably much more that C# is slowly but surely including with each version. Unfair? I've been writing about this progress for years (from newest to oldest): <ul> <a href="{app}view_article.php?id=2857" date="June 2013">.NET 4.5.1 and Visual Studio 2013 previews are available</a> <a href="{app}view_article.php?id=2784" date="February 2013">A provably safe parallel language extension for C#</a> <a href="{app}view_article.php?id=2216" date="October 2009">Waiting for C# 4.0: A casting problem in C# 3.5</a> <a href="{app}view_article.php?id=2183" date="June 2009">Microsoft Code Contracts: Not with a Ten-foot Pole</a> <a href="{app}view_article.php?id=1586" date="May 2007">Generics and Delegates in C#</a> <a href="{app}view_article.php?id=1436" date="November 2006">Wildcard Generics</a> (this one was actually about Java) <a href="{app}view_article.php?id=892" date="April 2004">An analysis of C# language design</a> <a href="{app}view_article.php?id=820" date="June 2003">Static-typing for languages with covariant parameters</a> <a href="{app}view_article.php?id=367" date="February 2002">What is .NET?</a> </ul></ft>