|<<>>|139 of 274 Show listMobile Mode

C# 6 Features and C# 7 Design Notes

Published by marco on

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: C# Design Meeting Notes for Jan 21, 2015 by Mads Torgerson (GitHub).

C# 6 Recap

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 async/await 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 almost officially available.

C# 6 brings the following features with it and can be used in the CTP versions of Visual Studio 2015 or downloaded from the Roslyn project (GitHub).

Some of the more interesting features of C# 6 are:

  • Auto-Property Initializers: initialize a property in the declaration rather than in the constructor or on an otherwise unnecessary local variable.
  • Out Parameter Declaration: An out parameter can now be declared inline with var or a specific type. This avoids the ugly variable declaration outside of a call to a Try* method.
  • Using Static Class: using 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.
  • String Interpolation: Instead of using string.Format() and numbered parameters for formatting, C# 6 allows expressions to be embedded directly in a string (á la PHP): e.g. “{Name} logged in at {Time}”
  • nameof(): 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.
  • Null-conditional operator: This feature reduces conditional, null-checking cruft by returning null when the target of a call is null. E.g. company.People?[0]?.ContactInfo?.BusinessAddress.Street includes three null-checks

Looking ahead to C# 7

If the idea of using await 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 close to being implemented yet.

That said, the first set of design notes for C# 7 by Mads Torgerson (GitHub) include several interesting ideas as well.

  • Pattern-matching: 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.
  • Metaprogramming: 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:

  • Controlling Nullability: Another idea is to be able to declare reference types that can never be null at compile-time (where reasonable—they do acknowledge that they may end up with a “less ambitious approach”).
  • Readonly parameters and locals: 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.
  • Lambda capture lists: One of the issues with closures is that they currently just close 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 explicitly 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.

  • Contracts(!): 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 decades[1], ever since I read the Object-Oriented Software Construction 2 (Amazon) (OOSC2) for the first time. The design document doesn’t say much about it, but mentions that “.NET already has a contract system”, the weaknesses of which I’ve written about before. Torgersen writes:

    “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.”

    …and expressiveness and provability!

    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:

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 this. We can use ReSharper [NotNull] 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.


[1]

I love the features of the language Eiffel, 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):