What is new in C# 7?

Here is an overview of new features in C#:
  • Out variables
  • Pattern matching
  • Tuples
  • Deconstruction
  • Discards
  • Local Functions
  • Binary Literals
  • Digit Separators
  • Ref returns and locals
  • Generalized async return types
  • More expression-bodied members
  • Throw expressions
  • // Out variables
    // Old way
    int parsedOld;
    string parseMeOld = 222224;
    int.TryParse(parseMeOld, out parsedOld);
    Console.WriteLine(parsedOld);
    // New way
    string parseMe = 3115;
    int.TryParse(parseMe, out int parsed);
    Console.WriteLine(parsed);

Tuples

List<int> numbers = new List<int> { 3, 5, 11, 4, 7 };
var result = GetLowestHighest(numbers);
Console.WriteLine(result.Item1); // ugly?
Console.WriteLine(result.Item2); // yeah, ugly..
// Notice the return type of the method. It’s tuple
private static(int, int) GetLowestHighest(IReadOnlyCollection < int > numbers) {
int lowest = numbers.Min(n => n);
int highest = numbers.Max(n => n);
return (lowest, highest);
}

Published by arjunpremier

Software Engineer, Blogger,

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started