public string First { get; set; } = "Jane";
public string Last { get; set; } = "Doe";using static System.Console;
using static System.Math;
class Program
{
static void Main()
{
WriteLine(Sqrt(3*3 + 4*4));
}
}using static System.DayOfWeek;
//...
WriteLine(Friday - Monday); var numbers = new Dictionary<int, string>
{
[7] = "seven",
[9] = "nine",
[13] = "thirteen"
};public int Plus(int x, int y) => x + y;
public Point Move(int dx, int dy)
=> new Point(x + dx, y + dy);
public static Foo operator +(Foo a, Foo b)
=> a.Add(b);
public static implicit operator string(Person p)
=> p.First + " " + p.Last;public string Name => First + " " + Last;
public Person this[long id] => _people.Find(id);
public static decimal BaseRate => 0.08m;var then = String.Format("{0} is {1} years old",
p.Name,
p.Age);
var now = $"{p.Name} is {p.Age} years old";
var sum = var s = $"{x} + {y} is {x + y}";var s = $"{p.Name,20} is {p.Age:D3} years old"//oops, wrong order:
var oops = String.Format("Name: {0}, {1} {2}",
p.First,
p.Last,
p.Middle);
//more readable:
var ok = $"Name: {p.Last}, {p.First} {p.Middle}";var s = $"{p.Name} is {p.Age}" +
$" year{(p.Age == 1 ? "" : "s")}" +
" old";var s = $"{p.Name} is " +
$"{(Enum.Parse(typeof(DateIncrement, incr)))}" +
$"{((p?.GetAge() ?? 0) == 1 ? "" : "s")}" +
" old";try
{
// You could do this.
res = await Resource.OpenAsync(...);
}
catch(ResourceException e)
{
// Now you can do this...
await Resource.LogAsync(res, e);
}
finally
{
// ...and this.
if (res != null) await res.CloseAsync();
}try
{
//...
}
catch (MyException e) when (myfilter(e))
{
//...
}try
{
//...
}
catch (Exception e) when (Log(e))
{
//this part never runs
}
//...
private static bool Log(Exception e)
{
//log it, and then:
return false;
}if (x == null)
{
throw new ArgumentNullException(nameof(x));
}
// prints "ZipCode":
WriteLine(nameof(person.Address.ZipCode)); public string First { get; } = "Jane";public class Customer
{
public string Name { get; }
public Customer(string first, string last)
{
Name = first + " " + last;
}
}// null if customers is null
int? length = customers?.Length;
// null if customers is null
Customer first = customers?[0];
// 0 if customers is null
int length = customers?.Length ?? 0;
// null if customers is null
int? first = customers?[0].Orders.Count();
// null if Orders is null
// NullReferenceException if customers is null
int? first = customers[0].Orders?.Count();
// a is...
// null - if b is null
// null - if b[0] is null
// null - if b[0].c is null
// (int?)d - if d is an int
int? a = b?[0]?.c?.d;// does not compile
int? count = customers.Count?();
//but that's fine, since this will never be true
customers.Count == null// does not compile
var result = myDelegate?(x);
// works just fine
var result = myDelegate?.Invoke(x);This is more than syntactic sugar. There's no perfect equivalent that I'm aware of in previous versions of C#.
// C# 6.0 version
var a1 = b()?.c()?.d()?.e;
// C# < 6 version
var a2 = b() != null &&
b().c() != null &&
b().c().d() != null
? b().c().d().e
: null;In the example, both a1 and a2 receive the same value, but the C# 5 version evaluated b() four times, c() three times, and d() twice. The C# 6 version called each method exactly once.