Tag Archives: .NET 2015

C# 6 Preview: String Interpolation

Update 31st January 2015: The syntax for string interpolation has changed as from VS2015 CTP5, as per the example at the end of this article. Please see C# 6 Preview: Changes in VS2015 CTP 5 for the latest syntax and examples. This article remains available due to historical significance.

Visual Studio 2015 Preview was released last week, and it supercedes the CTPs of what was previously known as “Visual Studio 14”. This VS2015 pre-release comes with a new C# 6.0 feature that wasn’t in the CTPs: string interpolation. Let’s learn about this feature by looking at an example.

I’ve got this Customer class with properties for an Id (integer), FirstName, LastName, and DateOfBirth, and I’ve declared an instance as follows:

            var dateOfBirth = new DateTime(2014, 10, 12);
            var customer = new Customer(1354, "Tony", "Smith", dateOfBirth);

Now, if I want to combine the first and last names into a full name, I can use the typical string.Format():

            var fullName = string.Format("{0} {1}", customer.FirstName, customer.LastName);

But in C# 6.0, I can use string interpolation to take out the placeholders and incorporate the string formatting arguments (in this case the two properties) directly in the string:

            var fullName = "\{customer.FirstName} \{customer.LastName}";

This eliminates the need to have placeholders that match the arguments, which can be a nightmare to maintain when you have a lot of them.

Just like string.Format(), string interpolation allows you to include formatting arguments:

            var dob2 = "Customer \{customer.IdNo} was born: \{customer.DateOfBirth:"yyyy-MM-dd"}";

In the case of the date, I had to put the format string in quotes to prevent the dashes from being interpreted as minus signs.

VS2015 highlights interpolated strings such that the referenced variables are shown as such, and not as part of the string:

vs2015-string-interpolation-highlighting

The actual syntax of string interpolation is going to change. String interpolation has gone through numerous discussions (such as this, this and this), and the development team has decided to change it for the benefit of the language. Quoting the latest C# feature descriptions (PDF):

“Note: This describes the syntax that works in the Preview. However, we’ve decided to change the syntax, to even better match that of format strings. In a later release you’ll see interpolated strings written like this:”

var s = $"{p.Name,20} is {p.Age:D3} year{{s}} old";

Visual Studio 2015 and .NET 2015 Announcements

.NET goes Open Source

During the Connect(); event on 12-13 November, a few pretty exciting announcements were made. One of the most notable of these announcements came from Scott Guthrie’s keynote speech and his followup blog post: Microsoft are open sourcing the .NET Core Runtime:

“Today I’m excited to announce that we are going even further, and will be open sourcing the .NET Core Runtime.  This will include everything needed to execute .NET code – including the CLR, Just-In-Time Compiler (JIT), Garbage Collector (GC), and core .NET base class libraries.”

Microsoft have already been working hard to open source the .NET server stack, and on top of that, they will be releasing official distributions of .NET Core for Linux and Mac.

From the Microsoft news article:

“Delivering on its promise to support cross-platform development, Microsoft is providing the full .NET server stack in open source, including ASP.NET, the .NET compiler, the .NET Core Runtime, Framework and Libraries, enabling developers to build with .NET across Windows, Mac or Linux. Through this implementation, Microsoft will work closely with the open source community, taking contributions for future improvements to .NET and will work through the .NET Foundation.”

There is no mention of client technologies such as WPF moving over to Linux or Mac, but that’s understandable – moving the .NET Core and server stack there is already a remarkable achievement, and whether all the other .NET technologies will follow suit in the future can only be subject to speculation at this stage.

Releases

A post on the Visual Studio blog announces new Visual Studio-related releases.

The first of these is Visual Studio 15 Preview, which supercedes the previous Visual Studio 14 CTPs:

Download Visual Studio 2015 Preview. This is the first full preview of what we used to call Visual Studio “14.” Even if you’ve been following the earlier CTPs, you’ll find some new things in here, including a new Visual Studio Emulator for Android and support for building Android applications using C++ based off of Clang and LLVM. There’s an Azure VM image available in the Gallery as well. You can get the entire list of feature and enhancement from Visual Studio 2015 Preview release notes. [UPDATE: The language packs for Visual Studio 2015 Preview are now available for download.]”

Before you rush out and try the new preview, though, check out this little warning:

“Since the majority of initial comments tend to be questions about supported configurations, I’ll put this up front: before you try to upgrade from Visual Studio “14” CTPs to Visual Studio 2015 Ultimate Preview, first uninstall Visual Studio “14” CTP – if you don’t, your system can wind up in an unstable state.”

As Somasegar explained in his Connect(); speech, the new .NET version will be known as .NET 2015, which intentionally breaks away from the previous versioning system.

Another release is Visual Studio Community 2013, which is free for commercial/non-commercial use for teams up to 5 people, “includes all the great functionality of Visual Studio Professional 2013”, and brings together the old Express editions which previously were in separate units for web development, application development, etc.

Finally, Microsoft has announced the availability of Visual Studio 2013 Update 4.

Other Enhancements

There are a myriad of enhancements throughout Visual Studio, and they are certainly not limited to the server stack or .NET becoming open source. Visual Studio is getting an emulator for Android (as already quoted above), there will be enhancements to WPF,  and lots more. Somasegar’s overview will give you an idea of what’s new, and the Visual Studio blog’s post has all the details.

Further Reading

For more information about the upcoming features in Visual Studio 2015 and .NET 2015, check out: