10. C#6.0の実装状況
10
要点例進捗どうですか
Primary constructors class Point(int x, int y) { … } Done
Auto-property initializers public int X { get; set; } = x; Done
Getter-only auto-properties public int Y { get; } = y; Done
Using static members using System.Console; … Write(4); Done
Dictionary initializer new JObject { ["x"] = 3, ["y"] = 7 } Done
Indexed member initializer new JObject { $x = 3, $y = 7 } Withdrawn
Indexed member access c.$name = c.$first + " " + c.$last; Withdrawn
Declaration expressions int.TryParse(s, out var x); Done
Await in catch/finally try … catch { await … } finally { await … } Done
Exception filters catch(E e) if (e.Count > 5) { … } Done
http://roslyn.codeplex.com/wikipage?title=Languag
e%20feature%20status&referringTitle=Home
Exists できてた
Done できた
Planned やってます
Maybe 多分する
Withdrawn やりたい
No 今は無理
N/A 必要なし
11. C#6.0の実装状況
11
要点例進捗どうですか
Typecase Select Case o : Case s As String : … No
Guarded cases Select Case i : Case Is > 0 When i Mod 2 = 0 No
Partial modules Partial Module M1 N/A
Partial interfaces Partial Interface I1 Exists
Multiline string literals "Hello<newline>World" Exists
Year-first date literals Dim d = #2014-04-03# N/A
Binary literals 0b00000100 Planned
Digit separators 0xEF_FF_00_A0 Planned
Line continuation comments Dim addrs = From c in Customers ' comment N/A
TypeOf IsNot If TypeOf x IsNot Customer Then … N/A
Expression-bodied members public double Dist => Sqrt(X * X + Y * Y); Planned
Event initializers new Customer { Notify += MyHandler }; Planned
Null propagation customer?.Orders?[5]?.$price Done
Semicolon operator (var x = Foo(); Write(x); x * x) Maybe
Private protected private protected string GetId() { … } Withdrawn
Params IEnumerable
int Avg(params IEnumerable<int> numbers) { …
}
Planned
Constructor Inference new Tuple(3, "three", true); Maybe
String interpolation "{p.First} {p.Last} is {p.Age} years old." Maybe
TryCast for nullable Dim x = TryCast(u, Integer?) Exists
Delegate combination with + d1 += d2 Exists
12. C#6.0の実装状況
12
要点例進捗どうですか
Implicit implementation Class C : Implicitly Implements I Exists
nameof operator string s = nameof(Console.Write); Exists
Strict modules Strict Module M Exists
Faster CInt Dim x = CInt(Math.Truncate(d)) | Exists
#pragma #Disable Warning BC40008 Exists
Checked and Unchecked blocks Checked : x += 1 : End Checked Exists
Field targets on autoprops <Field: Serializable> Property p As Integer Planned
If(b,e1,e2) uses type context Dim x As Integer? = If(b,1,Nothing) N/A
13. C#6.0の実装状況
13
要点例
Primary constructors class Point(int x, int y) { … }
Auto-property initializers public int X { get; set; } = x;
Getter-only auto-properties public int Y { get; } = y;
Using static members using System.Console; … Write(4);
Dictionary initializer new JObject { ["x"] = 3, ["y"] = 7 }
Declaration expressions int.TryParse(s, out var x);
Await in catch/finally try … catch { await … } finally { await … }
Exception filters catch(E e) if (e.Count > 5) { … }
Partial interfaces Partial Interface I1
Multiline string literals "Hello<newline>World"
Null propagation customer?.Orders?[5]?.$price
TryCast for nullable Dim x = TryCast(u, Integer?)
Delegate combination with + d1 += d2
Implicit implementation Class C : Implicitly Implements I
nameof operator string s = nameof(Console.Write);
Strict modules Strict Module M
Faster CInt Dim x = CInt(Math.Truncate(d)) |
#pragma #Disable Warning BC40008
Checked and Unchecked blocks Checked : x += 1 : End Checked
19機能が実装済
14. Auto-property initializers
14
自動実装プロパティの初期化がコンストラクタで可能になりました!
Before
class Person
{
public int Age { get;set; }
public Person()
{
Age = 20;
}
}
Next
class Person
{
public int Age { get;set; } = 20;
}
15. Getter-only auto-properties
15
Getterのみの自動実装プロパティの初期化もできます
Before
class Person
{
public int Age { get { return this._age; } }
private int _age = 20;
}
Next
class Person
{
public int Age { get; } = 20;
}
16. Primary constructors
16
クラス定義と同時にコンストラクタを宣言する
Before
class Person
{
public string Name { get;set; }
public Person(string name)
{
this.Name = name;
}
}
Next
class Person(string name)
{
public string Name { get;set; } = name;
}
17. Primary constructors
17
コンストラクタと同じように処理も記述できます
Before
class Person
{
public string Name { get; set; }
public Person(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("name is NullOrEmpty");
this.Name = name;
}
}
18. Primary constructors
18
コンストラクタと同じように処理も記述できます
After
class PersonNext(string name)
{
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("name is NullOrEmpty");
}
public string Name { get;set; } = name;
}
19. Using static members
19
静的クラスの静的メソッドは省略可能になります
Before
class Person
{
public void Say()
{
System.Console.WriteLine("Name is " + this.Name);
}
}
After
using System.Console;
class Person
{
public void Say()
{
Console.WriteLine("Name is " + this.Name);
}
}
20. Declaration expressions
20
Parseする時に無駄な変数を宣言しなくてよくなります。
Before
public void Say()
{
int number;
if (int.TryParse("20", out number))
{
System.Console.WriteLine(number + "歳です");
}
}
After
public void Say()
{
if (int.TryParse("20", out var number))
{
Console.WriteLine(number + "歳です");
}
Console.WriteLine(number + “歳です”); //スコープ外なのでエラー
}
22. Exception filters
22
Catch句に条件式を付与する事ができるようになりました
After
try
{
//HogeHoge
}
catch (Exception e)
if (e.Data.Count > 10)
{
//例外処理
}
Before
try
{
//HogeHoge
}
catch (Exception e)
{
if (e.Data.Count > 10)
{
//例外処理
}
}
23. Null propagation
23
nullチェックをシンプルに記述できます
Before
public int? GetNameLength(Person person)
{
if (person == null) return null;
if (person.Name == null) return null;
return person.Name.Length;
}
After
public int? GetNameLength(Person person)
{
return person?.Name?.Length;
}
24. nameof operator
24
メンバや変数の名前を返す演算子
Before
class Person
{
public string Name { get; set; }
public Person(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("name" + " is NullOrEmpty");
this.Name = name;
}
}
文字列指定
25. nameof operator
25
メンバや変数の名前を返す演算子
After
public string Name { get; set; }
public Person(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException(nameof(name) + "is NullOrEmpty");
this.Name = name;
}
nameof演算子で
タイプセーフ