|C# 7 Features|
|Written by Mike James|
|Monday, 27 April 2015|
While you are still trying to get your head around the new features in C# 6 the team are starting to think about what should be in C# 7.
Ah yes, I remember C# when it was a small compact language. Since then it has done what most languages do - put on features. Now it's a multiparadigm programming language, and it's still under development!
Mads Torgersen, Program Manager for the C# Language, has just posted a list of possible ideas for inclusion in the next version. None of the proposals are firm and he is careful not to get your hopes up that any particular one is going to make it into the cut, but they are categorized into Strong interest; Some interest; Small but useful; Interesting but require CLR support; Probably not this time; Probably never and "Unbucketed".
You can see the full list at his GitHub post but it is worth just getting the flavor of the Strong interest category (the explanations are mine and not Torgersen's).
Languages like Python support tuples and the biggest problem is usually to explain to beginners why it has tuples at all. However C# doesn't have an untyped list and so it is easier to make a case for the tuple. Essentially a tuple is a group of typed values that are gathered together for some temporary purpose. For example, think of a set of method parameters as a input tuple. The idea is that we can extend the syntax so that tuples can be used for method outputs.
You can see that the tuple can be regarded as an anonymous struct which is how it would be implemented.
Note: C# already has a set of generic tuple classes but these are limited by comparison.
Pattern Matching and algebraic types
This is another functional idea that is suggested for C# is pattern matching. This allows simpler and more powerful conditionals based on the type of an expression. For example, instead of having to write:
This seems like a small change but the example int is just the start of the sort of complex patterns you can specify. Patterns are usually used in a switch:
However this ability to write conditionals that involve type is really only useful if you have a more advanced form of type - an algebraic or parametric type. So along with pattern matching we have to introduce to C# an algebraic type which, initially at least is called a record type. A record type can be though of as a class definition that includes parameters. For example:
defines a type with two double properties. You can see how this could be compiled into a standard class. However you also get a new type Point (double, double) which can be used in pattern matching.
To give you some idea of how powerful this all is just take a look at the example given in the proposal. First we define some types suitable for representing a symbolic polynomial expression in X:
Using pattern matching we can now write a recursive function that finds the derivative of the expression:
This is a much smaller proposal than patterns and records and it only involves the way the compiler checks the code and not the generated IL.
If you use a type like Cat then nothing changes but if you put a ! after it Cat! then it cannot be null it is a mandatory reference. This means that the compiler will flag any attempt to set a reference to null:
throws a compile time error. The compiler also lets you use a mandatory references without checking for null.
A nullable reference, indicated by a trailing ?, however always has to be checked before use. For example:
is fine but if you now write
you will see a compile time error. The only way you can use a nullable reference is to check it before use:
You can check for null in all sort of ways and part of the challenge is to make sure that the compiler checks for enough subtle ways of checking or not-checking to be useful.
Of course if you don't use ! or ? you can do what ever you want with a reference.
Using statements and async/await
The using statement is a way of making sure objects are disposed of as soon as it goes out of scope. The new proposal is to simply extend this to async methods.
So at the moment you can write things like:
and you can be sure that myClass is disposed of at the end of the block. The proposal is to allow using inside of an async method to dispose of resources automatically and asynchronously.
There are some interesting ideas in the "Some interest" category, but there are 18 different proposals each of which would take a lot of words to explain. Two I'd favour are lists and dictionaries within the core language.
On the other hand some of the "Small but useful" might make it on the grounds they are relatively easy:
Overall my main feeling is that it might be better to leave C# alone and only add features that are so obviously needed that they fit into the "Must have" category. Allowing C# to drift ever further towards functional programming might seem attractive, but why then do we have F#?
Visual Studio 2015 CTP 5 Released
Microsoft Mass Migration To GitHub
.NET Core The Details - Is It Enough?
C# Gets A New Operator - Safe Navigation
RyuJIT - The Next Gen JIT .NET Compiler
Microsoft Team Explains Language Stagnation
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
or email your comment to: firstname.lastname@example.org
|Last Updated ( Tuesday, 28 April 2015 )|