Default Value Expressions
A default value expression produces the default value for a type. Default value expressions are particularly useful in generic classes and methods
In C# 7.0
static void Main(string[] args)
{
int a = default(int);
int b = 2;
Console.WriteLine($"a = {a}");
Console.WriteLine($"a + b = {a + b}");
int Add(int x, int y = default(int))
{
return x + y;
}
Console.WriteLine($"x + y = {Add(5)}");
Console.ReadLine();
}
In C# 7.1 enhancement has been done for Default Value Expressions and Type Inference and you can write the previous statement as follows
static void Main(string[] args)
{
int a = default;
int b = 2;
Console.WriteLine($"a = {a}");
Console.WriteLine($"a + b = {a + b}");
int Add(int x, int y = default)
{
return x + y;
}
Console.WriteLine($"x + y = {Add(5)}");
Console.ReadLine();
}
Inferred Tuple Element Names
As you know, a tuple is a data structure that has a specific number and sequence of elements.
For example using Tuple in C# 7.0
static (decimal, decimal) GetPrice(int id)
{
//Find product by id, then return price & discount
return (100, 10);
}
Now, you can rewrite the code above in C# 7.1 as below
using System;
namespace ConsoleApp
{
class Program
{
static (decimal price, decimal discount) GetPrice(int id)
{
//Find product by id, then return price & discount
return (100, 10);
}
static void Main(string[] args)
{
var product = GetPrice(1);
Console.WriteLine($"Price {product.price}$, Discount {product.discount}%");
Console.ReadLine();
}
}
}
If you compile error, you need to change the C# version by right-clicking on your project->Properties->Build->Advanced->Select C# version > 7.0