C# 7.0: Devs Get Excited About Tuples and Pattern Matching
Many key features of the upcoming C# 7.0 release are available in the latest Visual Studio "15" preview, and developers are already getting excited about tuples and pattern matching.
"C# 7.0 adds a number of new features and brings a focus on data consumption, code simplification and performance," said Mads Torgersen, C# program manager, in a blog post Wednesday. "Perhaps the biggest features are tuples, which make it easy to have multiple results, and pattern matching, which simplifies code that is conditional on the shape of data."
Although a fully finished C# 7.0 isn't out yet, Torgersen noted that most of its new features "are coming alive" in the recent release of Visual Studio "15" Preview 4.
Regarding the new tuples functionality, Torgersen noted that the existing options for returning more than one value from a method aren't optimal, so C# 7.0 has introduced tuple types and tuple literals.
However, their functionality goes beyond that.
"Tuples are value types, and their elements are simply public, mutable fields," Torgersen said. "They have value equality, meaning that two tuples are equal (and have the same hash code) if all their elements are pairwise equal (and have the same hash code). This makes tuples useful for many other situations beyond multiple return values. For instance, if you need a dictionary with multiple keys, use a tuple as your key and everything works out right. If you need a list with multiple values at each position, use a tuple, and searching the list etc. will work correctly."
Regarding pattern matching, Torgersen said, "C# 7.0 introduces the notion of patterns, which, abstractly speaking, are syntactic elements that can test that a value has a certain 'shape', and extract information from the value when it does." New patterns include constant patterns, type patterns and var patterns, with more planned for the new language element. For now, the
case language constructs are enhanced with patterns.
Tuples and pattern matching were met with enthusiasm by several programmers on the Web.
"Pattern matching! Tuples! Awesome, C# 7.0 is scratching two itches I've felt every time I've worked with it," said a commentator on Hacker News.
"Wow! These are some really impressive features," read another HN post. "I started learning F# a few months back and I can see some interesting parallels. Very excited to see Tuples and Pattern Matching."
A Reddit reader said: "Ah, tuples. You are soon to join the list of non-hated types. Indeed, I see myself using you often!"
The new features were also lauded among the hundreds (208 and counting) of comments attached to Torgersen's .NET Blog post.
"Some great new features -- I'm going to love the ability to return multiple values from a method."
- "Looking forward to using local functions, returning tuple, deconstructing a tuple directly and expression bodied members."
- "That's funny -- just yesterday I stumbled over the out parameter and async method problem and ended up using a tuple. Can't wait to get working with the new tuples."
- "I can't wait to start using these new features, especially the Tuples, which alleviate the need for the bazillion container classes we have lying around."
Besides enhanced tuple functionality (a Tuple class does already exist) and pattern matching, other features that are planned for C# 7.0 (several didn't make it into the latest Visual Studio preview) include: out variables; local functions; literal improvements; ref returns and locals; generalized async return types; more expression bodied members; and more.
With developers being developers, though, most wanted more. Just some of the many features requested by commentators across the aforementioned sites include:
- Design by contract
- Postfix conditionals
- Wildcards (Torgersen said: "It is still uncertain whether wildcards make it into C# 7.0")
- Supersede/original/replace and source generators
- Extension properties
- A suitable cross-platform UI toolkit
- Easy syntax for immutable records
- Better immutability support
- Ability to declare something at non-nullable
- Anonymous classes with actual class bodies that can implement interfaces
- Guaranteed not-nullable types
- Immutable classes
- DateTime expressions
More requests for C# features can be found on the Microsoft UserVoice site for the language, where "non-nullable reference types" leads the list with more than 3,300 votes.
While many approved of the new functionality, some complained about too many new features being added to the language, citing language bloat and the dangers of becoming overcomplicated or trying to do too many things.
"It looks like that C# is becoming polluted with all kinds of tricks," said a comment on the blog post.
On Reddit, one reader said: "Wow. The language is really starting to look kitchen sink. It's not a good idea to just add any cool feature you can find in other languages."
"C# is so full of 'features' that code refactoring tools like ReSharper, CodeRush and JustCode became simply indispensable," said a comment on Hacker News. "It is quite hard to be sure, every time, what is the most elegant/compact way of doing things."
Nevertheless, the language is marching on, Torgersen reminded everyone after describing the new features.
"We hope that they all combine to make your code more efficient and clear, and you more happy and productive," Torgersen said. "Please use the 'send feedback' button at the top of the Visual Studio window to tell us if something is not working as you expect, or if you have thoughts on improvement of the features."
David Ramel is an editor and writer for 1105 Media.