1950 1960 1970 1980 1990 Basic C C
開発言語の歴史 1950 1960 1970 1980 1990 Basic C# C++ Fortran 2010 VB. NET Visual Basic MSBasic 2000 C Java ALGOL Lisp Ruby Small talk Python PHP COBOL Perl 6 F#
C# 実装の仕組み コンパイラの機能として実現 void Method 1(string a, string b = "b", string c = "c") void Method 1( string a, [Optional, Default. Parameter. Value("b")] string b, [Optional, Default. Parameter. Value("c")] string c) 呼び出し側は常に展開される Method 1("a"); Method 1("a", "b", "c"); 12
メソッド呼び出しの解決順序 C# こんな呼び出しをした場合 Method 1("a"); 解決順序 14 1 void Method 1(string a){} 2 void Method 1(string a, string b = "b"){} 3 void Method 1(string a, params string[] b){}
COM 相互運用 (Before) C# 遅延バインディング 嵐 の ン ョ シ ク レ フ リ Type type = Type. Get. Type. From. Prog. ID("Excel. Application"); object excel. App = Activator. Create. Instance(type); object work. Books = excel. App. Get. Type(). Invoke. Member("Workbooks", Binding. Flags. Get. Property, null, excel. App, null); work. Books. Get. Type(). Invoke. Member("Add", Binding. Flags. Invoke. Method, null, work. Books, null); excel. App. Get. Type(). Invoke. Member("Visible", Binding. Flags. Set. Property, null, excel. App, new object[] {true}); 煩雑な引数 嵐 の g n i s s i M. Type excel. App. get_Range("A 1", "B 4"). Auto. Format(my. Format, Type. Missing, Type. Missing); 18
COM 相互運用 (After) C# 遅延バインディング (dynamic) dynamic excel. App = new Application(); excel. App. Workbooks. Add(); excel. App. Visible = true; オプション引数 excel. App. Range["A 1", "B 4"]. Auto. Format(Format: my. Format); 19
コレクション初期化子 From キーワード Dim items As New List(Of String) From {"Foo", "Bar"} 内部的には Add メソッドが呼ばれる Dim items As New List(Of String) items. Add("Foo") items. Add("Bar") 22 VB
オブジェクト初期化子との組合せ VB 要素にオブジェクト初期化子を利用 Public Items As New List(Of Sample 1) From { New Sample 1 With {. Foo = "Foo", . Bar = "Bar"}, New Sample 1 With {. Foo = "Foo 1", . Bar = "Bar 1"}} コレクション自体の初期化では組合せ不可 Dim days = New List(Of String) With {. Capacity = 7} From {"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"} 23
VB 暗黙の行連結 “_” なしの改行が可能 ただし、どこでも良いわけではない Sub Go( By. Val x As Integer, By. Val y As Integer, By. Val z As Integer ) Dim query = From n In {123, 456, 789} Order By n Select n + x End Sub 24 改行可能な条件は Appendix をご確認 ください。
複数行のラムダ式とサブルーチン VB 複数行のラムダ式 複雑なラムダ式の記述が可能に ' 単一行 Dim increment 1 = Function(x) x + 1 ' 複数行 Dim increment 2 = Function(x) Return x + 2 End Function ラムダ式によるサブルーチン Dim writeline 1 = Sub(x) Console. Write. Line(x) Dim writeline 2 = Sub(x) Console. Write. Line(x) End Sub 25
ラムダ式における戻り値の型解決 VB Dim type. Def 1 = Function(x) x + 1 '型推論 Dim type. Def 2 = Function(x) As Long '型指定 Return x + 1 End Function Dim type. Def 3 = Function(x) If (x >= 0) Then Return Integer. Parse(x) Else Return Double. Parse(x) End If End Function '全ての型に拡大変換可能な型に型推論 この例は Double 26
[参考]式ツリー + . NET Framework 3. 5 まで x Function(x) x + 1 . NET Framework 4 から Function(x) For i = 1 To 10 x += i Next Return x End Function For = i Return <= 1 i {} 10 x += x 28 1 i 注:上記式ツリーはイメージです。実際に生成されるツリーとは異なります。
C#/ C# VB 動的言語のサポート (DLR) Iron. Python Iron. Ruby C# VB. NET Others … Dynamic Language Runtime 動的オブジェクト の相互運用性 式ツリー Object Binder 30 Java. Script Binder Python Binder 呼び出し元 のキャッシュ Ruby Binder COM Binder
共変性と反変性 (デリゲート) delegate object Base. Method(string x); // 共変性 public static string Method 1(string x) { return x; } Base. Method base. Method 1 = Method 1; //反変性 public static object Method 2(object x) { return x. To. String(); } Base. Method base. Method 2 = Method 2; C# 2. 0 で実現されていました 33 C#
共変性と反変性 (デリゲート) VB Delegate Function Base. Method(By. Val x As String) As Object '共変性 Public Shared Function Deli. Meth 1(By. Val x As String) As String Return x End Function Dim base. Method 1 As Base. Method = Address. Of Deli. Meth 1 '反変性 Public Shared Function Deli. Meth 2(By. Val x As Object) As Object Return x End Function Dim base. Method 2 As Base. Method = Address. Of Deli. Meth 2 34
共変性と反変性 C#/ C# VB (ジェネリック インターフェイス) C# 共変性:out キーワード 反変性:in キーワード // 共変性の定義の例 public interface IEnumerable<out T> : IEnumerable // 反変性の定義の例 public interface IComparable<in T> VB ‘ 共変性の定義の例 Public Interface IEnumerable(Of Out T) Inherits IEnumerable ‘ 反変性の定義の例 Public Interface IComparable(Of In T) 35
共変性と反変性 (ジェネリック) C# // 共変性 IEnumerable<String> strings = new List<String>(); IEnumerable<Object> objects = strings; // 反変性 IComparable<string> str. Compare = new Obj. Compare(); class Obj. Compare : IComparable<object> { public int Compare. To(object other){…} } str. Compare. To("Tech. Ed Japan"); 36
共変性と反変性 (ジェネリック) VB ‘ 共変性 Dim strings As IEnumerable(Of String) = New List(Of String) Dim objects As IEnumerable(Of Object) = strings ‘ 反変性 Dim str. Compare As IComparable(Of String) = New Obj. Compar() Class Obj. Compar Implements IComparable(Of Object) Public Function Compare. To(By. Val other As Object) As Integer ・・・・ End Function End Class str. Compare. To("Tech. Ed Japan") 37
事前条件 C#/ C# VB メソッド開始前の状態をチェック C# public string Method 1(string name){ Contract. Requires(!String. Is. Null. Or. Empty(name)); Contract. Requires<Argument. Exception> (name. Length < 128, "Too long string"); こちらは IL に組み込まれる VB Public Function Method 1(By. Val name As String) As String Contract. Requires(Not String. Is. Null. Or. Empty(name)) Contract. Requires(Of Argument. Exception) (name. Length < 128, "Too long string") こちらは IL に組み込まれる 39
事前条件 (移行) C# C#/ C# VB 既存の引数チェックを Contract 化 public void Method 2(string name){ if (String. Is. Null. Or. Empty(name)) throw new Argument. Exception(); Contract. End. Contract. Block(); VB Public Sub Meshotd 2(By. Val name As String) If (String. Is. Null. Or. Empty(name)) Then Throw New Argument. Exception() End If Contract. End. Contract. Block() 40 Contract 化しても既存のコードは IL 上にも反映されます。
事後条件 C#/ C# VB メソッド実行後の状態をチェック C# 正常終了時:Ensures 例外終了時:Ensures. On. Throw Contract. Ensures(Contract. Old. Value<int> (this. list. Count) == this. list. Count -1); Contract. Ensures. On. Throw<Exception> (this. list. Count != 0); VB Contract. Ensures( Contract. Old. Value(Of Int 32)(Me. list. Count) = Me. list. Count - 1) Contract. Ensures. On. Throw(Of Exception)( Me. list. Count <> 0) 41
不変条件 C# C#/ C# VB 全てのクラスに対する処理時に状態をチェ ック [Contract. Invariant. Method] protected void Invaliant. Method() { Contract. Invariant(list != null); } VB <Contract. Invariant. Method()> Protected Sub Invaliant. Method() Contract. Invariant(Me. greetings Is. Nothing) End Sub 42
リファレンス. NET Framework デベロッパー センター http: //msdn. microsoft. com/ja-jp/netframework/default. aspx Visual Studio デベロッパー センター http: //msdn. microsoft. com/ja-jp/vstudio/default. aspx . NET 開発コード サンプル集 Code Recipe http: //msdn. microsoft. com/ja-jp/samplecode. recipe. aspx Days with Microsoft Web Platform (Blog) http: //blogs. msdn. com/tashinmu/ 56
Appendix
VB 10 暗黙の改行の条件 (1/3) 構文要素 コンマ (, ) の後。 例 Public Function Get. Username(By. Val username As String, By. Val delimiter As Char, By. Val position As Integer) As String Return username. Split(delimiter)(position) End Function Dim username = Get. Username( Security. Principal. Windows. Identity. Get. Current(). Name, 左かっこ (() の後、または右かっこ ()) の前。 CChar(""), 1 ) Dim customer = New Customer With {. Name = "Terry Adams", 左中かっこ ({) の後、または右中かっこ (}) の前。. Company = "Adventure Works", . Email = "terry@www. adventure-works. com" } Dim customer. Xml = <Customer> <Name> <%= customer. Name %> XML リテラル内の埋め込み式の開始記号 (<%=) の後、または埋め込み </Name> 式の終了記号 (%>) の前。 <Email> <%= customer. Email %> </Email> </Customer> cmd. Command. Text = "SELECT * FROM Titles JOIN Publishers " & 連結演算子 (&) の後。 "ON Publishers. Pub. Id = Titles. Pub. ID " & "WHERE Publishers. State = 'CA'" 60
VB 10 暗黙の改行の条件 (2/3) 構文要素 例 Dim file. Stream = My. Computer. File. System. Open. Text. File. Reader(file. Path) Dim memory. In. Use = My. Computer. Info. Total. Physical. Memory + 式内の二項演算子 (+、-、/、*、Mod、<>、<、>、<=、>=、^、 My. Computer. Info. Total. Virtual. Memory >>、<<、And. Also、Or、Or. Else、Like、Xor) の後。 My. Computer. Info. Available. Physical. Memory My. Computer. Info. Available. Virtual. Memory If Type. Of in. Stream Is IO. File. Stream And. Also in. Stream Is. Not Is 演算子および Is. Not 演算子の後。 Nothing Then Read. File(in. Stream) End If Dim query = From n In {123, 456, 789} Order By n Select n + x Dim file. Stream = メンバーの修飾子文字 (. ) の後、メンバー名の前。ただし、With ステー My. Computer. File. System. トメントを使用する場合や型の初期化リストの値を指定する場合は、メ Open. Text. File. Reader(file. Path) ンバーの修飾子文字の後に行連結文字 (_) を含める必要があります。 With ステートメントやオブジェクト初期化リストを使用する場合は、代 Dim a. Type = New With {. Property. Name = 入演算子 (= など) の後で改行することをお勧めします。 "Value"} Dim log As New Event. Log() With log. Source = "Application" End With 代入演算子 (=、&=、: =、+=、-=、*=、/=、=、^=、<<=、 >>=) の後。 61
VB 10 暗黙の改行の条件 (3/3) 構文要素 例 Dim customer. Name = customer. Xml. XML 軸プロパティ修飾子 (. 、. @、. . . ) の後。ただし、With キーワード <Name>. Value を使用する場合は、メンバーの修飾子を指定する際に行連結文字 (_) を 含める必要があります。 Dim customer. Email = customer. Xml. . . <Email>. Value < Serializable() 属性を指定する際の小なり記号 (<) の後、または大なり記号 (>) の前。 > また、属性を指定する際の大なり記号 (>) の後。ただし、アセンブリ レ Public Class Customer ベルまたはモジュール レベルの属性を指定する場合は、行連結文字 (_) Public Property Name As String を含める必要があります。 Public Property Company As String Public Property Email As String End Class クエリ演算子 (Aggregate、Distinct、From、Group By、Group Join、 Dim vs. Processes = From proc In Join、Let、Order By、Select、Skip While、Take While、 Process. Get. Processes Where、In、Into、On、Ascending、および Descending) の前後。複 Where proc. Main. Window. Title. Contains(“Hello!") 数のキーワードで構成されるクエリ演算子 (Order By、Group Join、 Select proc. Process. Name, proc. Id, Take While、および Skip While) のキーワード間で改行することはでき proc. Main. Window. Title ません。 For Each p In vs. Processes Console. Write. Line("{0}" & vb. Tab & "{1}" & vb. Tab & "{2}", For Each ステートメントの In キーワードの後。 p. Process. Name, p. Id, p. Main. Window. Title) Next Dim days = New List(Of String) From { コレクション初期化子の From キーワードの後。 "Mo", "Tu", "We", "Th", "F", "Sa", "Su" } 62
共変性、反変性を提供する ジェネリック インターフェイスとデリゲート 型 共変の型パラメーター 反変の型パラメーター Action<(Of <(T>)>) ~ Action<(Of <(T 1, T 2, T 3, T 4, T 5, T 6, T 7, T 8, T 9, T 10, T 11, T 12, T 13, T 14, T 15, T 16>)>) ○ Comparison<(Of <(T>)>) ○ Converter<(Of <(TInput, TOutput>)>) ○ Func<(Of <(TResult>)>) ○ Func<(Of <(T, TResult>)>) ~ Func<(Of <(T 1, T 2, T 3, T 4, T 5, T 6, T 7, T 8, T 9, T 10, T 11, T 12, T 13, T 14, T 15, T 16, TResult>)>) ○ Predicate<(Of <(T>)>) IComparer<(Of <(T>)>) IEnumerator<(Of <(T>)>) ○ ○ ○ IEquality. Comparer<(Of <(T>)>) IGrouping<(Of <(TKey, TElement>)>) ○ IOrdered. Enumerable<(Of <(TElement>)>) ○ IOrdered. Queryable<(Of <(T>)>) ○ IQueryable<(Of <(T>)>) ○ 63 ○ ○ IComparable<(Of <(T>)>) IEnumerable<(Of <(T>)>) ○
- Slides: 62