Over the past couple of previews, new C# language features have been trickling in, but Preview 4 marks a point where the majority of C# 7.0 are now available.
Here’s a code sample that uses a good number of these features, and that works in Preview 4 today:
class Program
{
   static void Main(string[] args)
   {
       object[] numbers =
           { 0b1, 0b10, new object[] { 0b100, 0b1000 },  // binary literals
            0b1_0000, 0b10_0000 };                       // digit separators
       var (sum, count) = Tally(numbers);                // deconstruction
       WriteLine($"Sum: {sum}, Count: {count}");
   }
   static (int sum, int count) Tally(object[] values)    // tuple types
   {
       var r = (s: 0, c: 0);                             // tuple literals
       void Add(int s, int c) { r = (r.s + s, r.c + c); } // local functions
       foreach (var v in values)
       {
           switch (v)                                    // switch on any value
           {
               case int i:                               // type patterns
                   Add(i, 1);
                   break;
               case object[] a when a.Length > 0:        // case conditions
                   var t = Tally(a);
                   Add(t.sum, t.count);
                   break;
           }
       }
       return r;
   }
}
For a full write-up about tuples, deconstruction, pattern matching, local functions, and the rest of C# 7.0, head over to the .NET Blog.
Earlier versions of some of these features were shown at Build 2016.
Happy hacking!
![]() |
Mads Torgersen, C# Language Program Manager
@MadsTorgersen
Mads leads the design of the C# language. During his 11 years at Microsoft he also contributed to Typescript, Roslyn, Visual Basic, and LINQ. Before that he was a professor at Aarhus University, doing research on programming languages and type systems. |

0 comments