The

Future
of C#

Ross Murray

  • BS in CS from Southeastern
  • Started using C# in 2011
  • Programming is pretty neat

This is about C# 7

  • New language features
  • Language design process
  • Direction of the language

Why?

  • New features often come from F# and other languages
  • They can introduce new patterns and ways of programming
  • Approaching problems in different ways leads to valuable insights and increased productivity
  • Use the right tool for the job.

Background Info

  • C# 6 just came out with VS 2015
  • A lot of .NET stuff is on github now
    • You can contribute to C# if you want
  • 7 is a long way away
  • Roslyn repo has C# language design meeting notes
  • Notes say not to infer future of C#
    • Let's do it anyway

How do they choose what to add?

  • There's a design team, they make the decisions
  • Ultimately the chief architect has the final say
  • The team picks vague themes to focus on for a new version
  • Anyone can propose new language features
    • make an issue on github with a proposal
    • these should be thorough and detailed

How do they choose what to add?

  • The team decides which proposals to explore
  • Chosen features are assigned to somebody to maintain a living proposal document
  • Proposals update over time with changes and comments
  • Practically, features are chosen by impact and cost

Some themes for C# 7

  • Working with data
  • Performance (interop)
  • Null
  • More syntax niceties

A focus on data

  • A common scenario today is sending around data between services, over the internet
  • Codebases with plain data models, and services with no state
  • OOP techniques aren't super effective in this scenario
    • coupling data and state, via encapsulation
    • a focus on object behavior rather than data
  • Functional programming is more effective in a data-focused scenario
    • composable functions that perform simple manipulations of immutable data

Let’s continue being inspired by functional languages, and in particular other languages – F#, Scala, Swift – that aim to mix functional and object-oriented concepts as smoothly as possible.

- MadsTorgersen (C# language PM)

What's the progress?

  • First big announcement in Jan 2015
  • Design review in April 2015
  • Several minor meetings between and since
  • Many features have detailed, accessible proposals
  • example (tuples)

Features

let me show you them

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

  • "is" operator now overridable.
public class Fahrenheit(double Temperature)
{
    public bool operator is(Celsius c) { ... }
}

var someTemp = new Fahrenheit(212.0);
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;
  }
}

Pattern matching

  • check it out
public record class Cartesian(double X, double Y);

public static class Polar
{
    public static bool operator is(Cartesian c, out double R,
        out double Theta)
    {
        R = Math.Sqrt(c.X*c.X + c.Y*c.Y);
        Theta = Math.Atan2(c.Y, c.X);
        return c.X != 0 || c.Y != 0;
    }
}

var c = Cartesian(3, 4);
if (c is Polar(var R, *)) Console.WriteLine(R);

Neat

  • Google to find all this info: C# 7 features
  • Much of this will change, but it gives an idea of the direction of the language.
  • Many C# features have similar origins in functional programming languages, such as generics, lambdas, LINQ, etc.
  • Tuples seem like a sure thing for C# 7
    • Probably Records and Pattern Matching, too.
    • The rest: /shrug

Neat

  • I recommend at least skimming the pattern matching proposal
    • It has a ton of little edge cases and uses
  • I love C#, and I think it keeps getting better.
  • Not afraid to push the boundaries 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)

Copy of The Future of C#

By Ross Murray

Copy of The Future of C#

  • 1,524