8. Feature Example C# VB
Auto-property initializers public int X { get; set; } = x; Added Exists
Read-only auto-properties public int Y { get; } = y; Added Added
Ctor assignment to getter-only autoprops Y = 15 Added Added
Static imports using static System.Console; … Write(4); Added Exists
Index initializer new JObject { ["x"] = 3 } Added No
Await in catch/finally try … catch { await … } finally { await … } Added No
Exception filters catch(E e) when (e.Count > 5) { … } Added Exists
Partial modules Partial Module M1 N/A Added
Partial interfaces Partial Interface I1 Exists Added
Multiline string literals "Hello<newline>World" Exists Added
Year-first date literals Dim d = #2014-04-03# N/A Added
Comments after implicit line continuation Dim addrs = From c in Customers ' comment N/A Added
TypeOf ... IsNot ... If TypeOf x IsNot Customer Then … N/A Added
Expression-bodied members public double Dist => Sqrt(X * X + Y * Y); Added No
Null-conditional operators customer?.Orders?[5] Added Added
String interpolation $"{p.Name} is {p.Age} years old." Added Added
nameof operator string s = nameof(Console.Write); Added Added
#pragma #Disable Warning BC40008 Added Added
Smart name resolution N/A Added
Read-write props can implement read-only interface properties Exists Added
#Region inside methods Exists Added
Overloads inferred from Overrides N/A Added
CObj in attributes Exists Added
CRef and parameter name Exists Added
Extension Add in collection initializers Added Exists
Improved overload resolution Added N/A
この中で、VBに関係があるのは…
9. Feature Example
Read-only auto-properties public int Y { get; } = y;
Ctor assignment to getter-only autoprops Y = 15;
Null-conditional operators customer?.Orders?[5]
String interpolation $"{p.Name} is {p.Age} years old."
nameof operator string s = nameof(Console.Write);
#pragma #pragma warning disable
Partial interfaces Partial Interface I1
Multiline string literals "Hello<newline>World"
Read-write props can implement read-only interface properties
#Region inside methods
CObj in attributes
Partial modules Partial Module M1
Year-first date literals Dim d = #2014-04-03#
Comments after implicit line continuation Dim addrs = From c in Customers ' comment
TypeOf ... IsNot ... If TypeOf x IsNot Customer Then …
Smart name resolution
Overloads inferred from Overrides
こんな感じ
10. Read-only auto-properties
Ctor assignment to getter-only autoprops
Class Point
ReadOnly Property X As Integer
ReadOnly Property Y As Integer
ReadOnly Property Name As String = NameOf(Point)
Sub New(x As Integer, y As Integer)
Me.X = x
Me.Y = y
End Sub
End Class
11. Class Point
Private _x As Integer
ReadOnly Property X As Integer
Get
Return _x
End Get
End Property
Private _name As Integer
ReadOnly Property Name As String
Get
Return _x
End Get
End Property Sub New(x As Integer, y As Integer)
_name = “Point”
_x = x
End Sub
End Class
コンパイル時には上記のように解釈されます。
セッションではReadOnlyなPrivateフィールドに格納されると言ったな?
あれは嘘だ!(と言うか、C#は確かにreadonlyなフィールドに格納されるんだけど…)
まさか(こんなことが)違うとは思わず、確認を怠っておりました。申し訳ない…
12. Null-conditional operators
Private Sub IntroduceNullConditionalOperators(
ps As IReadOnlyList(Of Point),
Optional act As Action = Nothing)
Dim p = ps?(0) 'これはIndexer
Dim s = p?.ToString()
WriteLine(If(s, "Null"))
Dim x = p?.X
WriteLine(If(x.HasValue, x.ToString(), "Null"))
act?.Invoke() 'delegateの場合はInvoke()を使用する
End Sub
13. String interpolation
Private Sub IntroduceStringInterpolation()
Dim formated = $“{1000:C}” ‘String.Formatに展開される
WriteLine(formated) ‘(日本語環境での)実行結果は 1,000
With New Object()
Dim format1 As IFormattable = $"{1000:C}“
Dim ci = CultureInfo.GetCultureInfo("en-us")
WriteLine(format1.ToString(Nothing, ci)) ‘実行結果は$1,000.00
End With
End Sub
14. nameof operator
Private Sub IntroduceNameOf(Optional arg As String = Nothing)
If arg Is Nothing Then
Throw New ArgumentNullException(NameOf(arg))
End If
WriteLine($"{NameOf(arg)}:{arg}")
WriteLine("おまけ")
WriteLine($"{NameOf(DateTime.Now)}")
WriteLine($"{NameOf(DateTime)}")
WriteLine($"{NameOf(Date.Today)}")
'WriteLine($"{NameOf(Date)}") これはBuild error
End Sub
15. 他の実例を挙げると、
INotifyPropertyChangedの実装でしょうか
今まで
Property FamilyName As String
(略)
Set(value As String)
If _familyName = value Then Return
_familyName = value
RaisePropertyChanged()
RaisePropertyChanged("FullName")
End Set
End Property
他にはExpression使ったり…
これから
Property FamilyName As String
(略)
Set(value As String)
If _familyName = value Then Return
_familyName = value
RaisePropertyChanged()
RaisePropertyChanged(NameOf(FullName))
End Set
End Property
こんなの書きまして、
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Sub RaisePropertyChanged(<CallerMemberName> Optional propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub