What’s New for C# and VB in Visual Studio
This week at Build 2016, we released Visual Studio 2015 Update 2 and Visual Studio “15” Preview. Both releases include many new language features that you can try today. It’s safe to install both versions of Visual Studio on the same machine so that you can check out all of the new features for yourself.
New C# and VB features in Visual Studio 2015 Update 2
In Visual Studio 2015 Update 2, you’ll notice that we’ve added some enhancements to previous features as well as added some new refactorings. The team focused on improving developer productivity by cutting down time, mouse-clicks, and keystrokes to make the actions you perform every day more efficient.
Interactive Improvements (C# only in Update 2, VB planned for future)
The C# Interactive window and command-line REPL, csi, were introduced in Visual Studio Update 1. In Update 2, we’ve paired the interactive experience with the editor by allowing developers to send code snippets from the editor to be executed in the Interactive window. We’ve also enabled developers to initialize the Interactive window with a project’s context.
To play with these features:
- Highlight a code snippet in the editor, right-click, and press Execute in Interactive (or Ctrl+E, Ctrl+E), as shown in the image below.
- Right-click on a project in the Solution Explorer and press Initialize Interactive with project.
Add Imports/Using Improvements (VB and C# in Update 2)
We’ve improved the Adding Imports/Using command to support “fuzzy” matching on misspelled types and to search your entire solution and metadata for the correct type—adding both a using/imports and any project/metadata references, if necessary.
You can see an example of this feature with a misspelled “WebCleint” type. The type name needs to be fixed (two letters are transposed) and the System.Net using needs to be added.
A couple refactorings we sprinkled in were:
- Make method synchronous (VB and C# in Update 2)
- Use null-conditional for delegate invocation (C# only in Update 2, Maybe for VB? – Read More below)
So, the killer scenario for this feature is raising events in a thread-safe way. Prior to C# 6 the proper way to do this was to copy the backing field of the event to a local variable, check the variable for null-ness and invoke the delegate inside the if. Otherwise Thread B could set the delegate to null by removing the last handler after Thread A has checked it for null resulting in Thread A unintentionally throwing a NullReferenceException. Using the null-conditional in C# is a much shorter form of this pattern. But in VB the RaiseEvent statement already raised the event in a null safe way, using the same code-gen. So the killer scenario for this refactoring really didn’t exist and worse, if we add the refactoring people might mistakenly change their code to be less idiomatic with no benefit. From time to time we review samples that don’t understand this and perform the null check explicitly anyway so this seems likely to reinforce that redundant behavior. Let us know in the comments if you think the refactoring still has tons of value for you outside of raising events and we’ll reconsider! -ADG
Roslyn Features (VB and C# in Update 2)
We’ve added two new compiler flags to the Roslyn compiler:
- deterministic: This switch will ensure builds with the same inputs produce the same the outputs, byte for byte. Previously, PE entries–like MVID, PDB ID and Timestamp–would change on every build but now can be calculated deterministically based on the inputs.
- publicSign: Supports a new method of signing that is similar to delay signing except it doesn’t need to add skip verification entries to your machine. Binaries can be public signed with only the public key and load into contexts necessary for development and testing. This is also known as OSS signing.
Sneak Peek: What’s in Visual Studio “15” Preview
We released a first look of Visual Studio “15” this week at Build. It is point in time view of what we’ve been working on. Some features will still change and others are still coming. It’s a good opportunity to provide feedback on the next big release of Visual Studio.
Play with C# 7 Prototypes (VB 15 Prototypes planned)
The guiding theme for C# 7 language design is “working with data”. While the final feature set for C# 7 is still being determined by the Language Design Committee, you can play with some of our language feature prototypes today in Visual Studio “15” Preview.
To access the language prototypes, right-click on your project in Solution Explorer > Properties > Build and type “__DEMO__” in the “Conditional compilation symbols” text box. This will enable you to play with a preview of local functions, digit separators, binary literals, ref returns, and pattern matching.
There is a known bug related to ref return IntelliSense, which can be worked around by:
- right-click on your project in Solution Explorer > Unload Project
- right-click on your project after it’s been unloaded > Edit csproj
- in the first Property Group under <AssemblyName> add: <Features>refLocalsAndReturns</Features>
- Ignore any XML schema warnings you may see
Custom Code Style Enforcement (VB and C# in Visual Studio “15” Preview)
The feature you all have been asking for is almost here! In Visual Studio “15” Preview, you can play around and give us feedback on our initial prototype for customizable code style enforcement. To see the style options we support today, go to Tools > Options > C#/VB > Code Style. Under the General options, you can tweak “this.”/”Me.”, predefined type, and “var”/type inference preferences. Today, with “var’ preferences, you can control the severity of enforcement—e.g., I can prefer “var” over explicit types for built-in types and make any violation of this squiggle as an error in the editor.
You can also add Naming rules, for instance, to require methods to be PascalCase.
Please Keep up the Feedback
Thanks for all the feedback we’ve received over the last year. It’s had a big impact on the features that I’ve described here and on others we’ve been working on. Please keep it coming. The language feedback on the open source Roslyn project has been extensive. It’s great to see a broader language community developing around the Roslyn open source project on Github.
To give feedback, try one of the following places:
- The Roslyn project, for language feedback
- Send Feedback Option Visual Studio, for Visual Studio feedback
- UserVoice , for Visual Studio suggestions
Thanks for using the product. I hope you enjoy using it to build your next app.
Over ‘n’ out Kasey Uhlenhuth, Program Manager, Managed Languages Team