13. C#の新機能を使う
静的 Using ステートメント
• クラスをusingすることで、静的メソッドの呼び出し時に省
略可能に
using System.Console;
namespace CSharpVNext
{
class Program
{
static void Main()
{
WriteLine("Hello");
Read();
}
}
Imports System.Console
Module Module1
Sub Main()
WriteLine("Hello")
Read()
End Sub
End Module
VB は前からできてたC#
14. C#の新機能を使う
プロパティ、コンストラクタ
• プライマリ コンストラクター
• プロパティ
using System.Math;
namespace CSharpVNext
{
class Circle(int radius) //--- プライマリ コンストラクタ
{
public int Radius { get; } = radius; // 自動プロパティ初期化子
public double Area2 => this.Radius * this.Radius * PI; // ラムダ
}
}
VB はまだC#
16. C#の新機能を使う
例外フィルター
• Exception の catch に条件を追加可能に
• DB サーバーのエラーコードで特定の処理をするなど
try
{
throw new ArgumentException(“Exception”, “aaa”);
}
catch (ArgumentException ex) if (ex.ParamName == "aaa")
{
WriteLine("aaaはいるでしょ。");
}
Try
Throw New ArgumentException("Exception", "aaa")
Catch ex As ArgumentException When ex.ParamName = "aaa"
WriteLine("aaaはいるでしょ。")
End Try
C
#
V
B
は
前
か
ら