|
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
23.8.1. System.Collections.Generic.IList(Of T)Naast de basisinterface voor list collecties System.Collections.IList, die er zo uit ziet : Public Interface IList : Inherits ICollection, IEnumerable Function Add(ByVal value As Object) As Integer Sub Clear() Function Contains(ByVal value As Object) As Boolean Function IndexOf(ByVal value As Object) As Integer Sub Insert(ByVal index As Integer, ByVal value As Object) ReadOnly Property IsFixedSize() As Boolean ReadOnly Property IsReadOnly As Boolean Default Property Item(ByVal index As Integer) As Object Sub Remove(ByVal value As Object) Sub RemoveAt(ByVal index As Integer) End Interface Bestaat er ook een strongly typed variant, namelijk System.Collections.Generic.IList(Of T), die als volgt is gedefinieerd : Public Interface IList(Of T) : Inherits ICollection(Of T), _ IEnumerable(Of T), IEnumerable Function IndexOf(ByVal item As T) As Integer Sub Insert(ByVal index As Integer, ByVal item As T) Default Property Item(ByVal index As Integer) As T Sub RemoveAt(ByVal index As Integer) End Interface Het valt misschien meteen op dat IList(Of T) in tegenstelling tot IList zelf geen members als Add, Remove en Clear definieert. Dit was niet nodig gezien IList(Of T) deze overerft van ICollection(Of T). IList daarintegen erfde die niet over van ICollection. Ter herinnering nog even een overzicht van de members van ICollection en ICollection(Of T) : Public Interface ICollection : Inherits IEnumerable Sub CopyTo(ByVal array As Array, ByVal index As Integer) ReadOnly Property Count() As Integer End Interface
Public Interface ICollection(Of T) : Inherits IEnumerable(Of T), IEnumerable Sub Add(ByVal item As T) Sub Clear() Function Contains(ByVal item As T) As Boolean Sub CopyTo(ByVal array As T(), ByVal arrayIndex As Integer) ReadOnly Property Count() As Integer ReadOnly Property IsReadOnly() As Boolean Function Remove(ByVal item As T) As Boolean End Interface boven
23.8.2. System.Collections.Generic.IEnumerable(Of T)Ook voor IList(Of T) is het zo, net als bij ICollection(Of T), dat deze zowel van de IEnumerable als van de IEnumerable(Of T) interface overerven. Wat maakt dat een type die IList(Of T) implementeert naast GetEnumerator() As IEnumerator ook GetEnumerator() As IEnumerator(Of T) moet implementeren, en dus een typesafe enumerator kan opleveren. boven
23.8.3. System.Array en AfgeleidenAlle concrete soorten van arrays zijn afgeleiden van System.Array.
Zoals onderstaand voorbeeld illustreert implementeert Array de interfaces ICloneable, IEnumerable, ICollection en IList. De afgeleiden van Array implementeren daarboven ook nog eens de strongly typed interfaces IEnumerable(Of T), ICollection(Of T) en IList(Of T).
Bemerkt dus ook het verschil tussen de "gewone" ( niet strongly typed ) enumerator ( IEnumerator ) die onze System.Array ( of dus IList ) oplevert (1), en de strongly typed enumerator ( IEnumerator(Of T) ) die onze Person() ( of dus IList(Of T) ) oplevert (3). Current van de gewone IEnumerator levert het element in Object vorm op (2) en moeten we nog casten naar een Person : Visual Basic 2010 Broncode Option Strict OnImports System.Collections.Generic Namespace ArrayImplementsIListOfTExample Public Class Person Private _Name As String Public Property Name() As String Get Name = _Name End Get Set( ByVal value As String) _Name = value End Set End Property End Class Public Class Client Public Shared Sub Main() Dim personArray As Person() = _ New Person() { New Person With {.Name = "John"}, _ New Person With {.Name = "Jane"}} PrintArray(personArray) PrintPersonArray(personArray) Console.WriteLine() PrintType(personArray.GetType()) PrintType(personArray.GetType().BaseType()) Console.ReadLine() End Sub Public Shared Sub PrintType( ByVal type As System. Type) Console.WriteLine( "Class " & type.Name) Console.WriteLine( "Inherits : " & type.BaseType.Name) Console.WriteLine( "Implements : ") Dim interfaces As Type() = type.GetInterfaces() For Each [interface] As Type In interfaces Console.Write([interface].Name & " ") Next Console.WriteLine() Console.WriteLine() End Sub Public Shared Sub PrintArray( ByVal array As System. Array) Dim list As IList = array Dim enumerator As IEnumerator = list.GetEnumerator() Do While enumerator.MoveNext() Dim currentObject As Object = enumerator.Current Dim currentPerson As Person = DirectCast(currentObject, Person) Console.Write(currentPerson.Name & " ") Loop enumerator.Reset() Console.WriteLine() End Sub Public Shared Sub PrintPersonArray( ByVal array As Person()) Dim list As IList( Of Person) = array Dim enumerator As IEnumerator( Of Person) = list.GetEnumerator() Do While enumerator.MoveNext() Dim currentPerson As Person = enumerator.Current Console.Write(currentPerson.Name & " ") Loop enumerator.Reset() Console.WriteLine() End Sub End ClassEnd NamespaceDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output John Jane
John Jane
Class Person[]
Inherits : Array
Implements :
ICloneable IList ICollection IEnumerable IList`1 ICollection`1 IEnumerable`1
Class Array
Inherits : Object
Implements :
ICloneable IList ICollection IEnumerable
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
|