The
Future
of C#
A brief history of
What's my version again?
( it's 5.0 )
(the one with async/await)
C# 6
is out this year
- out with Visual Studio 2015
- new Roslyn compiler
- await in try/finally
- lots of property syntax sugar
- expression bodies
- null conditional operator ?.
- lots more
What about 7.0?
¯\_(ツ)_/¯
C# is developed in the open
- C# 7 design meeting notes are on Github.
- They're organized, thorough, and accessible.
- This talk comes entirely from their notes.
Not now
- C# 7 is a work in progress. A lot will change.
- Let's look anyway.
- github.com/dotnet/roslyn/issues/2136
-
To find this later, google "C# 7 features."
Tuples
Real language support, not just System.Tuple
public (double meanAge, int oldest) GetAgeStats(IEnumerable<Person> people)
{
var mean = people.Average(x => x.Age);
var max = people.Max(x => x.Age);
var result = (meanAge: mean, oldest: max);
return result;
}
(var mean, var oldest) = GetAgeStats(people);
Non-nullable references
Dog - regular reference we have today
Dog! - a reference which cannot be null
Dog? - a reference which might be null
- Compiler won't let you dereference anything from a Dog? unless you do a null check first. It's opting into safety.
- Use Dog! everywhere you can, to preserve nullability information.
Deterministic disposal
- Adds the notion of a destructible type.
- Alternative to IDisposable or a finalizer.
- Ensures an object's destructor is called exactly when the variable goes out of scope. No using() or waiting for GC.
- Compiler enforces you can't make copies of the variable or stash them away.
public destructible struct ImageResource
{
...
~Cleanup() { ... }
}
Record types
- Special class made with shorthand notation
- Compiler generates a full class with private readonly backing fields and public properties that only have getters.
- You can add more to your class declaration to override defaults, like adding setters.
public class Celsius(double Temperature);
public class Fahrenheit(double Temperature);
public class Cartesian(double X, double Y);
public class Polar(double r: Radius, double phi: Angle);
Record types
- Can omit "new" when instantiating.
- "is" operator now overridable.
public class Fahrenheit(double Temperature)
{
public bool operator is(Celsius c) { ... }
}
var someTemp = Fahrenheit(212.0); //don't need new
if(someTemp is Celsius(100.0))
{
//someTemp is boiling
}
Pattern matching
- New ways to use "switch" and "is" keywords
var v = foo as Video;
if (v != null) {
//code using v
}
becomes
if (foo is Video v) {
//code using v
}
//v doesn't exist
Pattern matching
- New ways to use "switch" and "is" keywords
abstract class Shape;
class Square(double Width, double Height) : Shape;
class Circle(double Radius) : Shape;
public double GetArea(Shape shape) {
switch(shape) {
case Square(1.0, 1.0): return 1.0;
case Square s: return s.Width * s.Height;
case Circle c: return Math.PI * c.Radius * c.Radius;
}
}
All the small things
- Most of this will change, but it gives an idea of the direction of the language.
- Pattern matching, Records, Tuples, and much more are features taken directly from F# and other functional programming languages.
- Many C# features have similar origins, such as generics, lambdas, LINQ, etc.
- Deterministic destructors is based on "linear types" which is new ground for PLs. Only Rust has this in practice, and 1.0 just came out.
Feeling this
- I love C#, and I think it keeps getting better.
- Open development
- Open source
- Multi-platform
- Not afraid to push the boundries a bit
- If you don't feel the same, that's cool too.
- Life is short, be kind and enjoy colors.
(sorry if you're colorblind)
The Future of C#
By Ross Murray
The Future of C#
- 1,637