C# 7.0 adds a number of new features and focuses on data consumption, code simplification and performance. To play demo, you need to use visual studio 2017 to create a new console project, then modify your code as below
Out variables
In C# version < 7.0, you need to declare x variable outside the TryParse method
int x;
if (int.TryParse(Console.ReadLine(), out int x))
{
Console.WriteLine($"Your number: {x}");
}
In C# 7.0 you can modify code as below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Please enter a number: ");
if (int.TryParse(Console.ReadLine(), out int x))
{
Console.WriteLine($"Your number: {x}");
}
Console.ReadLine();
}
}
}
Local functions
Local functions are private methods of nested type in another member. They can only be called from their containing member.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
int Add(int x, int y)
{
return x + y;
}
Console.WriteLine($"x + y = {Add(2, 3)}");
Console.ReadLine();
}
}
}
Discards
C# 7.0 allows you to use discard and the returned value which is not required. Using _ character for discarding the parameter. The discard parameter is also referred as write-only and you can't read the value from this parameter.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
class Customer { }
class Program
{
static void Main(string[] args)
{
dynamic a;
a = 1;
//a = "c# code";
//a = null;
//a = new Customer();
if (a is int)
Console.WriteLine("a is int");
else if (a is null)
Console.WriteLine("a is null");
else if (a is string)
Console.WriteLine("a is string");
else if (a is var _)
Console.WriteLine("unknown");
Console.ReadLine();
}
}
}
Pattern Matching
This is one of the most popular features of C# 7.0. As you can see in the example below, GetData method write in C# version < 7.0
public void GetData(dynamic value)
{
if (value is Student)
{
var s = value as Student;
Console.WriteLine($"Student Id : {s.Id} Student Name {s.FullName}");
}
if (value is Professor)
{
var p = p as Professor;
Console.WriteLine($"Professor Id : {p.Id} Professor Name {p.FullName}");
}
else
Console.WriteLine("Unknown !");
}
In C# 7.0 you can modify code as below
public void GetData(dynamic value)
{
if (value is Student s)
Console.WriteLine($"Student Id : {s.Id} Student Name {s.FullName}");
if (value is Professor p)
Console.WriteLine($"Professor Id : {p.Id} Professor Name {p.FullName}");
else
Console.WriteLine("Unknown !");
}
Tuple
A tuple is a data structure that has a specific number and sequence of elements. As you can see in the example below, GetProductPrice method write in C# version < 7.0
static Tuple<decimal, decimal> GetProductPrice(int id)
{
//Find product by id, then return price & discount
return new Tuple<decimal, decimal>(100, 10);
}
static void Main(string[] args)
{
var product = GetProductPrice(1);
Console.WriteLine($"Price {product.Item1}$, Discount {product.Item2}%");
Console.ReadLine();
}
You need install System.ValueTuple from nuget. You can rewrite the code above with C# 7.0 as below
static (decimal, decimal) GetProductPrice(int id)
{
//Find product by id, then return price & discount
return (100, 10);
}