|
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.5.1. System.Collections.Generic.Stack(Of T)Naast de breed inzetbare System.Collections.Stack collectie, met members als :
- Sub Push(ByVal obj As Object) - Function Pop() As Object - Function Peek() As Object - Function Contains(ByVal obj As Object) As Boolean
Bestaat er ook een strongly typed variant, namelijk Stack(Of T) uit de namespace System.Collections.Generic. Deze werkt niet met elementen van type Object maar met het actueel generisch parametertype :
- Sub Push(ByVal item As T) - Function Pop() As T - Function Peek() As T - Function Contains(ByVal item As T) As Boolean
Zo staat in onderstaand voorbeeld de Push method enkel argumenten van type Person toe, en levert de Pop method het laatst toegevoegde element in Person vorm op : Visual Basic 2010 Broncode Option Strict OnImports System.Collections.Generic 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 PropertyEnd ClassNamespace StackOfTExample Public Class Client1 Public Shared Sub Main() Dim stack1 As New Stack( Of Person) stack1.Push( New Person With {.Name = "John"}) Console.WriteLine(stack1.Pop().Name) Console.ReadLine() End Sub End ClassEnd NamespaceDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output John De Stack(Of T) klasse implementeert de interface ICollection en bevat dus ook implementaties voor ondermeer ICollection.Count en CopyTo. De ICollection.CopyTo method is Private geïmplementeerd :
Private Sub CopyTo(ByVal array As Array, _ ByVal arrayIndex As Integer) _ Implements ICollection.CopyTo
Dit om in de publieke interface enkel een strongly typed CopyTo te voorzien :
Public Sub CopyTo(ByVal array As T(), ByVal arrayIndex As Integer) Visual Basic 2010 Broncode Namespace StackOfTExample Public Class Client2 Public Shared Sub Main() Dim stack1 As New Stack( Of Person) stack1.Push( New Person With {.Name = "John"}) stack1.Push( New Person With {.Name = "Jane"}) Dim personsArray(1) As Person stack1.CopyTo(personsArray, 0) Console.WriteLine(personsArray(0).Name) personsArray = stack1.ToArray() Console.WriteLine(personsArray(1).Name) Console.ReadLine() End Sub End ClassEnd NamespaceDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output Jane
John Bemerk dat er ook een strongly typed ToArray method is, die zelf een array van elementen van type T ( of in dit geval Person ) aanmaakt en oplevert. boven
23.5.2. System.Collections.Generic.Queue(Of T)Ook van de System.Collections.Queue klasse bestaat een strongly typed variant, namelijk System.Collections.Generic.Queue(Of T), ook deze beschikt over typesafe members als :
- Sub Enqueue(ByVal item As T) - Function Dequeue() As T - Function Peek() As T - Function Contains(ByVal item As T) As Boolean - Sub CopyTo(ByVal array As T(), ByVal arrayIndex As Integer) - Function ToArray() As T() Visual Basic 2010 Broncode Namespace QueueOfTExample Public Class Client Public Shared Sub Main() Dim queue1 As New Queue( Of Person) queue1.Enqueue( New Person With {.Name = "John"}) queue1.Enqueue( New Person With {.Name = "Jane"}) Console.WriteLine(queue1.Peek().Name) Dim personsArray(1) As Person queue1.CopyTo(personsArray, 0) Console.WriteLine(personsArray(0).Name) personsArray = queue1.ToArray() Console.WriteLine(personsArray(1).Name) Console.ReadLine() End Sub End ClassEnd NamespaceDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output John
John
Jane boven
23.5.3. System.Collections.Generic.IEnumerable(Of T)Zowel Stack(Of T) als Queue(Of T) erven zowel de IEnumerable als de IEnumerable(Of T) interface over.
Dit maakt dat ze naast IEnumerable.GetEnumerator() As IEnumerator ook IEnumerable(Of T).GetEnumerator() As IEnumerator(Of T) moeten implementeren.
Deze worden hier steeds Private geïmplementeerd terwijl er aan de publieke interfaces methods worden toegevoegd als GetEnumerator() As Stack(Of T).Enumerator voor Stack(Of T) en GetEnumerator() As Queue(Of T).Enumerator voor Queue(Of T)
Deze zijn uiteraard strongly typed, de expressies stack1Enumerator.Current en queue1Enumerator.Current in onderstaand voorbeeld leveren het item dan ook steeds in Person vorm op : Visual Basic 2010 Broncode Namespace StackAndQueueOfTGetEnumeratorExample Public Class Client Public Shared Sub Main() Dim stack1 As New Stack( Of Person) stack1.Push( New Person With {.Name = "John"}) Dim queue1 As New Queue( Of Person)(stack1) Dim stack1Enumerator As Stack( Of Person).Enumerator = _ stack1.GetEnumerator() stack1Enumerator.MoveNext() Console.WriteLine(stack1Enumerator.Current.Name) Dim queue1Enumerator As Queue( Of Person).Enumerator = _ queue1.GetEnumerator() queue1Enumerator.MoveNext() Console.WriteLine(queue1Enumerator.Current.Name) Console.ReadLine() End Sub End ClassEnd NamespaceDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output John
John
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
|