4.3. Fields
The fields of a generic class follow all the same syntax rules that are applied to non-generic fields. The primary incremental change here is the ability to reference type parameters in the declaration of your class's fields. Here's an example where this is applied:
[VB code] Public Class MyType(Of T, U) Private _myFirstDataMember As T Private _mySecondDataMember As U Public Sub New(ByVal val1 As T, ByVal val2 As U) Me._myFirstDataMember = val1 Me._mySecondDataMember = val2 End Sub Public Function GetFirstDataMember() As T Return Me._myFirstDataMember End Function Public Function GetSecondDataMember() As U Return Me._mySecondDataMember
End Function End Class Public Class MyApp Shared Sub Main() Dim testType As New MyType(Of String, String)("val1", "Val2") Console.WriteLine(testType.GetFirstDataMember()) Console.WriteLine(testType.GetSecondDataMember()) End Sub End Class [C# code] using System; class MyType<T, U> { private T _myFirstDataMember; private U _mySecondDataMember; public MyType(T val1, U val2) { this._myFirstDataMember = val1; this._mySecondDataMember = val2; } public T GetFirstDataMember() { return this._myFirstDataMember; } public U GetSecondDataMember() { return this._mySecondDataMember; } } class MyApp { static void main(String[] args) { MyType<string, string> testType = new MyType<string, string>("Val1", "Val2"); Console.WriteLine(testType.GetFirstDataMember()); Console.WriteLine(testType.GetSecondDataMember()); } }
As you can see, the generic class ...
Get Professional .NET 2.0 Generics now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.