Mads Torgersen, program manager of C#, presented the upcoming C# 7 at QCon New York 2016. He also explained briefly the evolution of C# and introduced some features being developed for future versions.
The emergence of the cloud and distributed systems over the last years creates new challenges for developers. As developer needs evolve, languages have to follow. Evolving a multi-purpose language like C# is a matter of balancing different concerns:
- Improving vs staying simple
- Improve existing development vs attract new users
- Embrace new paradigms vs stay true to the OOP spirit of C#
.Net as a whole also experienced significant changes over the past few years:
- Support for Windows, Mac OS and Linux (.Net Core)
- Deploy system
- Compile to native code (.Net Native)
- Open source compilers and public Roslyn APIs
- Multiple editor choices (enabled by OmniSharp and Roslyn)
- Open source framework, editors and tools
C# 7
C# 7 will be shipped in Visual Studio 15, which is currently available as a preview. In order to make new language features available faster, point releases are being considered. This would lead to language features being included in minor releases. The goal is to give early adopters the option of activating individual features as soon as they are completed.
Tuples syntax will be included in C# 7. Returning several values from a method becomes more straightforward:
static (int sum, int count) Method()
{
return (0, 0)
}
// Calling the method and using the result
var result = Method();
Console.WriteLine($"Sum:{result.sum}.Count: {result.count}.");
Tuples can also be used inside generic types. Tuples are value types, meaning they are allocated on the stack instead of the heap. This can lead to potential performance gains, such as reducing garbage collection overhead in critical code paths.
// async method returning a tuple
static async Task<(int sum, int count)> Method()
// Dictionary using a tuple as a key
var dict = new Dictionary<(string first, string last), person>();
The cut of some pattern matching features has led to rumors that pattern matching was being cancelled altogether. Pattern matching is still in C# 7 and will be enhanced in future versions.
Beyond C# 7
Several features are currently in development for next versions of C#. Additional semantics are being explored for pattern matching to provide a terser way to handle some scenarios:
// Using C# 7 pattern matching
if (O is Point p && p.X == 5) { WriteLine($"Y: {p.Y}")}
// Same scenario with alternative syntax for future versions
if (o is Point X {var x, Y: var y} && x == 5) { WriteLine($"Y: {y}")}
if (o is Point { X : 5, Y: var y}) { WriteLine($"Y: {y}")}
if (o is Point(5, var y)) { WriteLine($"Y: {y}")}
Nullable reference types implementation is still ongoing, the reason being to raise warnings when the compiler detects incoherent use of null values:
string? n;
string s;
n = null; // OK, nullable
s = null; // warning, shouldn't be null
s = n; // warning
WriteLine(s.Length); // Sure; it's not null
WriteLine(n.Length) // Warning! could be null
Record type, an immutable value type, is another feature being developed. Immutability makes sharing data in a concurrent environment safer, as well as makes programs easier to reason about. A record type provides a terser way to use immutability in C#. A record has default value semantic, meaning out of the box implementation for getters, GetHashCode and equality members.