13. C#4.0 オプション引数/名前付き引数
int Sum(int x = 0, int y = 0, int z = 0)
{
return x + y + z;
}
int s1 = Sum();
int s2 = Sum(1);
int s3 = Sum(1, 2);
int s4 = Sum(y: 1);
int s5 = Sum(y: 1, z: 2, x: 3);
19. C#6.0 ラムダ式本体によるメンバーの記述
private int[] _values;
public int Count => _values.Length;
public int this[int i] => _values[i];
public int Hoge(int index) => _values[i] * 2;
20. C#6.0 null 条件演算子
public class Sample
{
public string Name { get; set; }
public static int? NameLength(Sample s) =>s?.Name?.Length;
}
以前の書き方
public static int? NameLength(Sample s)
{
if (s == null) return null;
if (s.Name == null) return null;
return s.Name.Length;
}
21. C#6.0 文字列挿入
var value = $"({x}, {y})";
以前の書き方
var value = string.Format("({0}, {1})", x, y);