.NETで作る!

.NETに関するあれこれ(C#、VB.NET)

VB14の新機能をつらつらと

.NET Compiler Platform ("Roslyn") - New language features in VB14

VB14(Visual Studio 2015)の新機能の情報が載っていたのでメモ。

The ?. operator

?演算子

Dim x = customer.Address?.Country

' is shorthand for
Dim x = If(customer.Address IsNot Nothing, customer.Address.Country, Nothing)

Nothingオブジェクトに対してプロパティ、メソッドをコールしてもNullReferenceExceptionにならない(Nothingが返される)

うーん、特に興味なし。

ReadOnly auto-implemented properties

ReadOnlyプロパティの自動実装

Class Customer
   Public ReadOnly Property Tags As New List(Of String)
   Public ReadOnly Property Name As String = ""
   Public ReadOnly Property File As String

   Sub New(file As String)
      Me.File = file
   End Sub
End Class

最高!待ち望んでたよ…ReadOnlyProperty記述するのすんごい面倒なのよ… 現在のコードと見比べればいかに素晴らしい(というか今が冗長)なのがよくわかる。

'Visual Studio 2013
Class Customer
    Private _tags As New List(Of String)
    Public ReadOnly Property Tags As List(Of String)
        Get
            Return _tags
        End Get
    End Property
    Private _name As String = ""
    Public ReadOnly Property Name As String
        Get
            Return _name
        End Get
    End Property
    Private _file As String
    Public ReadOnly Property File As String
        Get
            Return _file
        End Get
    End Property

    Sub New(file As String)
        _file = file
    End Sub
End Class

_file = file個人的にこのコードが嫌い。

Multiline string literals

複数行の文字列リテラル

Dim x = "hello
world"

素晴らしい!これでStringBuilder.AppendLine連発から解放される…SQLデバッグが捗りそう。

String interpolation

String内挿

Dim s = $"hello {p.Name} you are {p.Height:0.00}m tall"

' is shorthand for
Dim s = String.Format("hello {0} you are {1:0.00}m tall", p.Name, p.Height)

いいね!これはコーディングが捗る。

NameOf operator

NameOf演算子

Private _age As Integer
Property Age As Integer
   Get
      Return _age
   End Get
   Set
      _age = value
      Me.RaisePropertyChanged(Me, NameOf(Age))
   End Set
End Property

ぶっちゃけ、INotifyPropertyChanged専用ですね。悪くないです。ただ、Setter内に書くってのが結構面倒なんですよね。イベント発生させる必要なければ、Property Age As Integerの1行で済むんですから。もうちょいなんとかしてほしかった。

Comments within LINQ expressions and after implicit line continutions

LINQにコメント書ける。 個人的にそんな激しいLINQ使わない(LINQの1行前に1行コメントで済むことが大半)ので、ふーん程度。

Smart name resolution

名前空間が整理されるらしい。

Structures allow Sub New()

構造体興味なし。

Year-first date literals

Dim d = #2014-11-12#

' is shorthand for
Dim d = Date.Parse("2014-11-12")

上の方がわかりやすい。時刻も指定できるそうです。

ReadOnly interface properties can be implemented by ReadWrite props

インターフェイスでReadOnlyのプロパティにGetterを書いてもよくなる。これはいい!

Interface I
    ReadOnly Property P As Integer
End Interface

Class C : Implements I
    Public Property P As Integer Implements I.P
End Class

ちなみに今はこんな感じの悲しいコードを書くしかない。

'Visual Studio 2013
Class C : Implements I
    Private _p
    Public ReadOnly Property P As Integer Implements I.P
        Get
            Return _p
        End Get
    End Property
    Public WriteOnly Property PSetter As Integer
        Set(value As Integer)
            _p = value
        End Set
    End Property
End Class

TypeOf IsNot

型比較にIsNot演算子が使えるようになる。というか、使えない今までが変だった。

'#Disable Warning' and '#Enable Warning'

即座に理解できなかったのでパス。非同期(Async)に関係しているらしい。

XML doc-comment improvements

触ってないのでちょい理解できず。パラメータもリファクタリング(名前の変更)してくれるってことかな…

Partial module and interface declarations

モジュールもパーシャルできる。あんま使わないけど、拡張メソッドがModuleにしかかけないので、使う時がくるかもしれない。

'#Region' directives inside method bodies

#Regionをメソッド内にも記述できる。まぁ便利かもしれない。ただ、サブルーチンじゃなくて全部これで書く 人とか出てきそうで怖い。

'Overloads' modifier inferred from 'Overrides' modifier

Overrides修飾子からOverloads修飾子を推測する。らしい。

CObj allowed in attribute arguments

属性の引数にCObjが指定できる。前までは数値か文字列しか指定できなかったから、結構いいかもしれない。

Declaration and consumption of ambiguous methods from unrelated interfaces

わかるようなわからんような話なのでパス。

所感

ReadOnlyに関する記述の強化はうれしいです。面倒すぎて使いたくなかったですもん。

. .