ReSharper 9: first look at C# 6.0 support

Today we want to share some details on our progress in supporting the new language features coming in C# 6.0. While most of the changes in C# 6.0 are relatively small, they will definitely affect the existing ReSharper actions, quick-fixes, refactorings and code inspections. Let’s take a look at some of the new C# features and how ReSharper will help you work with them.

Conditional access

The conditional access expression, also known as the ‘null-propagating’ operator is a small syntax sugar, designed to help dealing with null values. Despite the popular myth, it can’t really magically save you from NullReferenceException once and for all. However, the conditional access operator can dramatically reduce the amount of code needed in some scenarios to check for null values. Consider this method from the ReSharper source code:

cond1

Code like this is used extremely often in ReSharper development, since IDE tooling deals with incomplete code almost all of the time. We have to write a lot of code just to handle broken syntax and semantic trees. But most of the time you just need to return false/null to exit method early — and here is when the operator ?. is simply invaluable:

cond2

As you could always expect, ReSharper has deep knowledge on how the new language construct behave, how it evaluates, what values it may produce, and whether or not parts of  the expression are reachable during the execution:

cond5

We also notify you if the conditional access is redundant and offer a quick-fix to replace it with normal non-conditional member access:

cond33

But the most interesting part of the ReSharper support for C# 6.0 are transformations. Since tons of C# code had been written before the ? operator was even designed, it is natural for ReSharper to be able to recognize null-propagating code patterns and suggest using the new conditional expression when it matters:

cond6

We reviewed a lot of various code snippets to recognize and formalize the patterns, which were then used to implement context actions and suggestions, as well as reverse transformations:

cond7

We are even considering a small refactoring action to easily upgrade usages of popular extension methods like .IfNotNull(x => …) by implementing the conditional expressions for earlier C# versions:

cond8

The conditional access operator is a great addition to the C# language: it doesn’t affect the language too much, you are not forced to use it, but when you really need the null-propagating code – it provides a nice way of keeping your code clean and concise.

Expression-bodied members

We are super excited by this C# 6.0 language feature. It is designed to reduce the amount of boilerplate code required to declare trivial type members, especially the get-only properties. C# developers might be a bit tired of writing explicit accessors, multiple curly braces and return statements just to yield some values from properties. C# 6.0 solves this problem in a great way with the familiar lambda-expressions syntax:

exprs1

As you can see, ReSharper is here to help you migrate your code to idiomatic C# 6.0, also being able to apply the transformations file-, project- and solution-wide. Expression bodies are also available for method, indexer and user-defined operator declarations.

Improvements on auto-properties

Besides introducing new language constructs, C# 6.0 aims on making some existing features more useful and covering more user scenarios. For example, auto-properties in C# 6.0 can have initializer expression, just like fields and field-like events:

prop1

ReSharper already knows about it and suggests you the transformation even if the backing field is declared readonly and has an initializer expression. Also, you may notice that ReSharper creates auto-property without the set accessor — which is also allowed in C# 6.0 to give you the experience of truly immutable auto-properties, just like readonly fields.

prop2

Primary constructors

This feature has been trending on Roslyn forums for months. Primary constructors are designed to make your initialization code more declarative and trivial, to move away from imperative error-prone initialization code in constructor. This feature also improves usability of field/property initializers – they are not allowed to make use of this reference, but constructor parameters are in the scope now. So a typical data class from the example above transforms into the following form:

ctor1

Unfortunately, C# design team decided not to provide any way of automatic declaration of fields or properties from primary constructor parameter declarations, so you still need to repeat mentioning types and names when declaring property and field members. But there  is still room for future design improvements and we hope that the current design is just an incremental improvement before more significant changes are introduced.

ctor2

Currently, ReSharper recognizes the new primary constructor declaration and the body block syntactically and semantically. The familiar IDE experience, from the extend/shrink selection feature to code completion is available for primary constructors. But of course we want to provide you with the new experience of transforming classes into primary constructor. This is why we’ve built a small analysis engine to inspect the constructor code: ReSharper collects the assignments happening always during the normal constructor execution and tracks type member reads and writes as well as other initialization information. The new ReSharper features like ‘Move assignment to initializer’  already have this new engine under the hood (ReSharper 8 has only the reverse ‘Move initialization to constructor’ action) and we are looking forward to use it in primary constructor transformations:

ctor3

We still have a lot of decisions to make regarding intersections with the existing ReSharper features, and we are still reviewing the feature set that we want to deliver with ReSharper 9. By now, it is unclear how often developers will use this feature, how would they format primary constructor parameters list and so on. Therefore, we would like to hear your opinion on how you expect ReSharper to behave with primary constructor declarations, which transformations and code inspections might be useful to you.

Other changes

We have already supported some other C# 6.0 language features like ‘using static’ (importing of static members of some type in scope) and exception filters (condition clause for catch blocks):

exc1

As usual, ReSharper maintains the ‘language level’ concept, enabling different sets of features and inspections for projects targeting any C# compiler version starting from 2.0. We handle everything from the changes in overload resolution between C# versions to small language improvements like await expressions availability in catch/finally clauses of try block in C# 6.0:

exc2

New features like ‘declaration expressions’ (ability to declare variables anywhere inside expressions), dictionary initializers and nameof() operator are on their way.

Looking forward

We should mention that the openness of Roslyn development process helps us a lot. Now we are able to play with the new language and compiler features as early as they are implemented, debug compilers to understand their behavior and even participate in language design discussions. By the way, ReSharper finally successfully compiles with the Roslyn compiler (even while some VB bugs critical for us are still not fixed) and we are looking forward to Visual Studion 14 and Roslyn releases. We expect fast adoption of C# 6.0, since the new language features do not depend on the BCL support and can be used even with .NET Framework 2.0 projects.

In this post we have highlighted only the C# 6.0 bits of upcoming ReSharper 9, just to give you an overview of what we are working on. As usual, we are going to open the EAP as soon as our builds become stable enough. Stay tuned and tell us what you think!

This entry was posted in ReSharper Tips&Tricks and tagged , , . Bookmark the permalink.

23 Responses to ReSharper 9: first look at C# 6.0 support

  1. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1663

  2. Marcin says:

    I hope you’ll be ready when VS 2014 RC is released.

    Also, is there any chance you’ll add duplicate detection, like in Idea, or R# competitors?

    • Alexander Shvedov says:

      Actually, ReSharper has duplicate code detection in it’s command line version (running in TeamCity, for example), but currently it’s not that suitable to be used inside an 32-bit IDE process with limited resources, in incremental way.

      Unfortunately, this is out of ReSharper 9 scope by now.

      • Vlad Ivanov says:

        Was a little surprised to read this being given as a technical reason that prevents duplicate detection implemented in Resharper.

        Why not marshal to a 64bit process that is not constrained by memory limitations of the host? Marshaling can be done extremely efficiently and has being utilized in such performance demanding applications as real-time music (VST plugins: http://jstuff.wordpress.com/jbridge/)

        • Alexander Shvedov says:

          The problem is not the restricted memory environment, the problem is memory/cpu consumption by feature itself. We believe it can be implemented in a nicer and more performant way.

          Talking about external process: we have external process for solution-wide inspections for a long time. In ReSharper 9 we improved SWA in a such way, that we don’t need it anymore, cold data was offloaded to disk, hot data was dramatically reduced in size such as it perfectly fits VS process now. Having external process and data marshalling always introducing extra level of complexity and possible issues. We want to keep things as simple as possible.

          • Vlad Ivanov says:

            Thank you very much for reply and consideration. If it helps to sway your decision in any way – i’d like to cast my vote in favor of implementing duplicate code detection sooner rather than later. This problem is a acute pain point in our environment and having it integrated with Resharper would make a great difference for us (aware of alternatives, not 100% satisfied, plus have internal constraints implementing). I was really hoping it would make it into the product soon and was a bit disappointed when i read it here that it’s not, which prompted my post.

  3. Pingback: Dew Drop – July 31, 2014 (#1826) | Morning Dew

  4. casvan marcel says:

    Still waiting for an Intellij Idea C# plugin. There is no decent IDE on mac for Unity3d….

  5. Kris McGinnes says:

    This all looks amazing. I imagine this level of support and completeness could not have been possible if you were also rewriting your semantic analyzers to use Roslyn at the core. Making the decision to stick with what you know has gotten you above and beyond what I expected this early on. Kudos!

  6. Pingback: Les liens de la semaine – Édition #91 | French Coding

  7. Den says:

    Could you please make sure the ReSharper SDK samples will be updated for the new version? Every time I had been downloading them (8.0, 8.1, 8.2) they basically don’t even compile (had to check some third-party examples to see something working). Unless of course your vision is to replace ReSharper extensions with Roslyn extensions (which would be a good option as well).

  8. mr.yang says:

    i didn’t find it forum,can we post this place?
    ——————————————————
    reshaper version:8.2.1
    descritpion:,i define pageparserfitlertype in web.config,ex:

    edit webform aspx page,resharper notice:
    resharper does not support pageparserfiltertype specified in web.config

    can you fix it?thanks.
    i’m so sorry english poor.

    • Sergey Kuks says:

      mr.yang, could you please contact me directly to provide more details.
      I believe pageparserfiltertype has been supported by ReSharper for several releases already.
      You may reach me in Skype at sergey.coox or email qx@jetbrains.com.
      Thanks in advance.

  9. James says:

    Any idea when Resharper 9 might be released?

    • Shawn says:

      I’m also curious about this. I’m planning to buy a copy of ReSharper for personal use soon, but don’t want to buy 8 right before 9 comes out and then have to pay again to upgrade.

  10. Kevin says:

    That’s a good reference like what’s new in C# 6.0. Thanks.

  11. Pingback: [#VS2015] Un poco de #Roslyn, #ReSharper 9.0 RTM disponible y ahora es parte de una suite ;) | El Bruno

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">