4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VB 2015 の新機能まとめ

4
Last updated at Posted at 2015-07-28

Visual Studio 2015 が利用できるようになりましたが、VB 2015 の新機能について試してみました。

Nothing 判別演算子 ?.

演算子 ?. が追加になりました。これは、ある変数の値が Nothing でない場合のみ ?. に続くメソッドを実行します。

Dim n As Integer? = 1000
Console.WriteLine(n?.ToString())
n = Nothing
Console.WriteLine(n?.ToString())

変数の埋め込み

$ を先頭に付けた文字列の中では、{ } で囲まれた文字列は変数とみなされて、その変数の値が文字列に埋め込まれます。

Dim a1 = If(args.Length > 1, args(1), "undefined")
Console.WriteLine($"Args(1) = {a1}")

改行を含む文字列 (ヒアドキュメント)

従来のバージョンでは改行を含む文字列は & 演算子などを使って記述し、直接記述できませんでした (C# は可能)。VB 2015 ではそれが可能になりました。

Dim mlstr = "{
 id:100,
 name:'Apple',
 amount:5
}"
Console.WriteLine(mlstr)

LINQ でのコメント

従来のバージョンでは LINQ 文の途中にコメントを入れることはできませんでした。このバージョンからは可能になったので、長い LINQ 文でも見やすく記述できます。

Dim Range As Integer() = { 1, 3, 5, 7, 9, 11, 13 }
' LINQ でのコメント。LINQ 文の途中にコメントを入れることが可能。
Dim col As IEnumerable(Of Int32) = From i In Range  ' Range から
                              Where i > 5  ' 5 より大きい要素を
                              Select i  ' 選びます。
' 結果を表示
For Each i In col
    Console.WriteLine(i)
Next

NameOf 演算子

通常のアプリケーションではあまり使い道がなさそうですが、変数の名前自体を得ることができるようです。Roslyn などで使用するらしい。

Dim sample As String = ""
Console.WriteLine(NameOf(sample))  ' sample と表示される。

MSDN Magazine
 Visual Basic .NET - 14 Top Improvements in Visual Basic 14

Channel9 MSDN
What's New in Visual Basic 14

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?