CollectionBase Klas

Definitie

Biedt de abstract basisklasse voor een sterk getypte verzameling.

public ref class CollectionBase abstract : System::Collections::IList
public abstract class CollectionBase : System.Collections.IList
[System.Serializable]
public abstract class CollectionBase : System.Collections.IList
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class CollectionBase : System.Collections.IList
type CollectionBase = class
    interface ICollection
    interface IEnumerable
    interface IList
[<System.Serializable>]
type CollectionBase = class
    interface IList
    interface ICollection
    interface IEnumerable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CollectionBase = class
    interface IList
    interface ICollection
    interface IEnumerable
Public MustInherit Class CollectionBase
Implements IList
Overname
CollectionBase
Afgeleid
Kenmerken
Implementeringen

Voorbeelden

In het volgende codevoorbeeld wordt de CollectionBase klasse geïmplementeerd en wordt deze implementatie gebruikt om een verzameling Int16 objecten te maken.

using System;
using System.Collections;

public class Int16Collection : CollectionBase  {

   public Int16 this[ int index ]  {
      get  {
         return( (Int16) List[index] );
      }
      set  {
         List[index] = value;
      }
   }

   public int Add( Int16 value )  {
      return( List.Add( value ) );
   }

   public int IndexOf( Int16 value )  {
      return( List.IndexOf( value ) );
   }

   public void Insert( int index, Int16 value )  {
      List.Insert( index, value );
   }

   public void Remove( Int16 value )  {
      List.Remove( value );
   }

   public bool Contains( Int16 value )  {
      // If value is not of type Int16, this will return false.
      return( List.Contains( value ) );
   }

   protected override void OnInsert( int index, Object value )  {
      // Insert additional code to be run only when inserting values.
   }

   protected override void OnRemove( int index, Object value )  {
      // Insert additional code to be run only when removing values.
   }

   protected override void OnSet( int index, Object oldValue, Object newValue )  {
      // Insert additional code to be run only when setting values.
   }

   protected override void OnValidate( Object value )  {
      if ( value.GetType() != typeof(System.Int16) )
         throw new ArgumentException( "value must be of type Int16.", "value" );
   }
}

public class SamplesCollectionBase  {

   public static void Main()  {

      // Create and initialize a new CollectionBase.
      Int16Collection myI16 = new Int16Collection();

      // Add elements to the collection.
      myI16.Add( (Int16) 1 );
      myI16.Add( (Int16) 2 );
      myI16.Add( (Int16) 3 );
      myI16.Add( (Int16) 5 );
      myI16.Add( (Int16) 7 );

      // Display the contents of the collection using foreach. This is the preferred method.
      Console.WriteLine( "Contents of the collection (using foreach):" );
      PrintValues1( myI16 );

      // Display the contents of the collection using the enumerator.
      Console.WriteLine( "Contents of the collection (using enumerator):" );
      PrintValues2( myI16 );

      // Display the contents of the collection using the Count property and the Item property.
      Console.WriteLine( "Initial contents of the collection (using Count and Item):" );
      PrintIndexAndValues( myI16 );

      // Search the collection with Contains and IndexOf.
      Console.WriteLine( "Contains 3: {0}", myI16.Contains( 3 ) );
      Console.WriteLine( "2 is at index {0}.", myI16.IndexOf( 2 ) );
      Console.WriteLine();

      // Insert an element into the collection at index 3.
      myI16.Insert( 3, (Int16) 13 );
      Console.WriteLine( "Contents of the collection after inserting at index 3:" );
      PrintIndexAndValues( myI16 );

      // Get and set an element using the index.
      myI16[4] = 123;
      Console.WriteLine( "Contents of the collection after setting the element at index 4 to 123:" );
      PrintIndexAndValues( myI16 );

      // Remove an element from the collection.
      myI16.Remove( (Int16) 2 );

      // Display the contents of the collection using the Count property and the Item property.
      Console.WriteLine( "Contents of the collection after removing the element 2:" );
      PrintIndexAndValues( myI16 );
   }

   // Uses the Count property and the Item property.
   public static void PrintIndexAndValues( Int16Collection myCol )  {
      for ( int i = 0; i < myCol.Count; i++ )
         Console.WriteLine( "   [{0}]:   {1}", i, myCol[i] );
      Console.WriteLine();
   }

   // Uses the foreach statement which hides the complexity of the enumerator.
   // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
   public static void PrintValues1( Int16Collection myCol )  {
      foreach ( Int16 i16 in myCol )
         Console.WriteLine( "   {0}", i16 );
      Console.WriteLine();
   }

   // Uses the enumerator.
   // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
   public static void PrintValues2( Int16Collection myCol )  {
      System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "   {0}", myEnumerator.Current );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

Contents of the collection (using foreach):
   1
   2
   3
   5
   7

Contents of the collection (using enumerator):
   1
   2
   3
   5
   7

Initial contents of the collection (using Count and Item):
   [0]:   1
   [1]:   2
   [2]:   3
   [3]:   5
   [4]:   7

Contains 3: True
2 is at index 1.

Contents of the collection after inserting at index 3:
   [0]:   1
   [1]:   2
   [2]:   3
   [3]:   13
   [4]:   5
   [5]:   7

Contents of the collection after setting the element at index 4 to 123:
   [0]:   1
   [1]:   2
   [2]:   3
   [3]:   13
   [4]:   123
   [5]:   7

Contents of the collection after removing the element 2:
   [0]:   1
   [1]:   3
   [2]:   13
   [3]:   123
   [4]:   7

*/
Imports System.Collections


Public Class Int16Collection
   Inherits CollectionBase


   Default Public Property Item(index As Integer) As Int16
      Get
         Return CType(List(index), Int16)
      End Get
      Set
         List(index) = value
      End Set
   End Property


   Public Function Add(value As Int16) As Integer
      Return List.Add(value)
   End Function 'Add

   Public Function IndexOf(value As Int16) As Integer
      Return List.IndexOf(value)
   End Function 'IndexOf


   Public Sub Insert(index As Integer, value As Int16)
      List.Insert(index, value)
   End Sub


   Public Sub Remove(value As Int16)
      List.Remove(value)
   End Sub


   Public Function Contains(value As Int16) As Boolean
      ' If value is not of type Int16, this will return false.
      Return List.Contains(value)
   End Function 'Contains


   Protected Overrides Sub OnInsert(index As Integer, value As Object)
      ' Insert additional code to be run only when inserting values.
   End Sub


   Protected Overrides Sub OnRemove(index As Integer, value As Object)
      ' Insert additional code to be run only when removing values.
   End Sub


   Protected Overrides Sub OnSet(index As Integer, oldValue As Object, newValue As Object)
      ' Insert additional code to be run only when setting values.
   End Sub


   Protected Overrides Sub OnValidate(value As Object)
      If Not GetType(System.Int16).IsAssignableFrom(value.GetType()) Then
         Throw New ArgumentException("value must be of type Int16.", "value")
      End If
   End Sub

End Class


Public Class SamplesCollectionBase

   Public Shared Sub Main()

      ' Creates and initializes a new CollectionBase.
      Dim myI16 As New Int16Collection()

      ' Adds elements to the collection.
      myI16.Add( 1 )
      myI16.Add( 2 )
      myI16.Add( 3 )
      myI16.Add( 5 )
      myI16.Add( 7 )

      ' Display the contents of the collection using For Each. This is the preferred method.
      Console.WriteLine("Contents of the collection (using For Each):")
      PrintValues1(myI16)
      
      ' Display the contents of the collection using the enumerator.
      Console.WriteLine("Contents of the collection (using enumerator):")
      PrintValues2(myI16)
      
      ' Display the contents of the collection using the Count property and the Item property.
      Console.WriteLine("Initial contents of the collection (using Count and Item):")
      PrintIndexAndValues(myI16)
      
      ' Searches the collection with Contains and IndexOf.
      Console.WriteLine("Contains 3: {0}", myI16.Contains(3))
      Console.WriteLine("2 is at index {0}.", myI16.IndexOf(2))
      Console.WriteLine()
      
      ' Inserts an element into the collection at index 3.
      myI16.Insert(3, 13)
      Console.WriteLine("Contents of the collection after inserting at index 3:")
      PrintIndexAndValues(myI16)
      
      ' Gets and sets an element using the index.
      myI16(4) = 123
      Console.WriteLine("Contents of the collection after setting the element at index 4 to 123:")
      PrintIndexAndValues(myI16)
      
      ' Removes an element from the collection.
      myI16.Remove(2)

      ' Display the contents of the collection using the Count property and the Item property.
      Console.WriteLine("Contents of the collection after removing the element 2:")
      PrintIndexAndValues(myI16)

    End Sub


    ' Uses the Count property and the Item property.
    Public Shared Sub PrintIndexAndValues(myCol As Int16Collection)
      Dim i As Integer
      For i = 0 To myCol.Count - 1
          Console.WriteLine("   [{0}]:   {1}", i, myCol(i))
      Next i
      Console.WriteLine()
    End Sub


    ' Uses the For Each statement which hides the complexity of the enumerator.
    ' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection.
    Public Shared Sub PrintValues1(myCol As Int16Collection)
      Dim i16 As Int16
      For Each i16 In  myCol
          Console.WriteLine("   {0}", i16)
      Next i16
      Console.WriteLine()
    End Sub


    ' Uses the enumerator. 
    ' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection.
    Public Shared Sub PrintValues2(myCol As Int16Collection)
      Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()
      While myEnumerator.MoveNext()
          Console.WriteLine("   {0}", myEnumerator.Current)
      End While
      Console.WriteLine()
    End Sub

End Class


'This code produces the following output.
'
'Contents of the collection (using For Each):
'   1
'   2
'   3
'   5
'   7
'
'Contents of the collection (using enumerator):
'   1
'   2
'   3
'   5
'   7
'
'Initial contents of the collection (using Count and Item):
'   [0]:   1
'   [1]:   2
'   [2]:   3
'   [3]:   5
'   [4]:   7
'
'Contains 3: True
'2 is at index 1.
'
'Contents of the collection after inserting at index 3:
'   [0]:   1
'   [1]:   2
'   [2]:   3
'   [3]:   13
'   [4]:   5
'   [5]:   7
'
'Contents of the collection after setting the element at index 4 to 123:
'   [0]:   1
'   [1]:   2
'   [2]:   3
'   [3]:   13
'   [4]:   123
'   [5]:   7
'
'Contents of the collection after removing the element 2:
'   [0]:   1
'   [1]:   3
'   [2]:   13
'   [3]:   123
'   [4]:   7

Opmerkingen

Important

We raden u niet aan om de CollectionBase klasse te gebruiken voor nieuwe ontwikkeling. In plaats daarvan raden we u aan de algemene Collection<T> klasse te gebruiken. Zie Aangenaamde verzamelingen niet worden gebruikt op GitHub voor meer informatie.

Een CollectionBase exemplaar kan altijd worden gewijzigd. Zie ReadOnlyCollectionBase voor een alleen-lezen versie van deze klasse.

De capaciteit van een CollectionBase is het aantal elementen dat de CollectionBase kan bevatten. Als er elementen aan een CollectionBaseworden toegevoegd, wordt de capaciteit automatisch verhoogd als vereist via herlocatie. De capaciteit kan worden verlaagd door de Capacity eigenschap expliciet in te stellen.

Notities voor uitvoerders

Deze basisklasse wordt geleverd om het voor implementeerfuncties gemakkelijker te maken om een sterk getypte aangepaste verzameling te maken. Implementeerfuncties worden aangemoedigd om deze basisklasse uit te breiden in plaats van hun eigen klasse te maken.

Constructors

Name Description
CollectionBase()

Initialiseert een nieuw exemplaar van de CollectionBase klasse met de standaardinitiële capaciteit.

CollectionBase(Int32)

Initialiseert een nieuw exemplaar van de CollectionBase klasse met de opgegeven capaciteit.

Eigenschappen

Name Description
Capacity

Hiermee haalt u het aantal elementen op of CollectionBase stelt u dit in.

Count

Hiermee haalt u het aantal elementen op dat in het CollectionBase exemplaar is opgenomen. Deze eigenschap kan niet worden overschreven.

InnerList

Hiermee haalt u een ArrayList met de lijst met elementen in het CollectionBase exemplaar op.

List

Hiermee haalt u een IList met de lijst met elementen in het CollectionBase exemplaar op.

Methoden

Name Description
Clear()

Hiermee verwijdert u alle objecten uit het CollectionBase exemplaar. Deze methode kan niet worden overschreven.

Equals(Object)

Bepaalt of het opgegeven object gelijk is aan het huidige object.

(Overgenomen van Object)
GetEnumerator()

Retourneert een enumerator die door het CollectionBase exemplaar wordt herhaald.

GetHashCode()

Fungeert als de standaardhashfunctie.

(Overgenomen van Object)
GetType()

Hiermee haalt u de Type huidige instantie op.

(Overgenomen van Object)
MemberwiseClone()

Hiermee maakt u een ondiepe kopie van de huidige Object.

(Overgenomen van Object)
OnClear()

Voert extra aangepaste processen uit bij het wissen van de inhoud van het CollectionBase exemplaar.

OnClearComplete()

Voert extra aangepaste processen uit nadat de inhoud van het CollectionBase exemplaar is gewist.

OnInsert(Int32, Object)

Voert aanvullende aangepaste processen uit voordat u een nieuw element in het CollectionBase exemplaar invoegt.

OnInsertComplete(Int32, Object)

Voert extra aangepaste processen uit na het invoegen van een nieuw element in het CollectionBase exemplaar.

OnRemove(Int32, Object)

Voert extra aangepaste processen uit bij het verwijderen van een element uit het CollectionBase exemplaar.

OnRemoveComplete(Int32, Object)

Voert extra aangepaste processen uit nadat u een element uit het CollectionBase exemplaar hebt verwijderd.

OnSet(Int32, Object, Object)

Voert extra aangepaste processen uit voordat u een waarde instelt in het CollectionBase exemplaar.

OnSetComplete(Int32, Object, Object)

Voert extra aangepaste processen uit na het instellen van een waarde in het CollectionBase exemplaar.

OnValidate(Object)

Voert extra aangepaste processen uit bij het valideren van een waarde.

RemoveAt(Int32)

Hiermee verwijdert u het element in de opgegeven index van het CollectionBase exemplaar. Deze methode kan niet worden overschreven.

ToString()

Retourneert een tekenreeks die het huidige object vertegenwoordigt.

(Overgenomen van Object)

Expliciete interface-implementaties

Name Description
ICollection.CopyTo(Array, Int32)

Kopieert het hele CollectionBase naar een compatibele eendimensionale Arraywaarde, beginnend bij de opgegeven index van de doelmatrix.

ICollection.IsSynchronized

Hiermee wordt een waarde opgehaald die aangeeft of de toegang tot de CollectionBase synchronisatie is gesynchroniseerd (thread safe).

ICollection.SyncRoot

Hiermee haalt u een object op dat kan worden gebruikt om de toegang tot het CollectionBaseobject te synchroniseren.

IList.Add(Object)

Hiermee voegt u een object toe aan het einde van de CollectionBase.

IList.Contains(Object)

Bepaalt of het CollectionBase een specifiek element bevat.

IList.IndexOf(Object)

Zoekt naar de opgegeven Object en retourneert de op nul gebaseerde index van het eerste exemplaar binnen het hele CollectionBaseexemplaar.

IList.Insert(Int32, Object)

Hiermee voegt u een element in de CollectionBase opgegeven index in.

IList.IsFixedSize

Hiermee wordt een waarde opgehaald die aangeeft of de grootte van een CollectionBase vaste grootte is.

IList.IsReadOnly

Hiermee wordt een waarde opgehaald die aangeeft of het CollectionBase kenmerk Alleen-lezen is.

IList.Item[Int32]

Hiermee haalt u het element op de opgegeven index op of stelt u het in.

IList.Remove(Object)

Hiermee verwijdert u het eerste exemplaar van een specifiek object uit de CollectionBase.

Extensiemethoden

Name Description
AsParallel(IEnumerable)

Hiermee schakelt u parallelle uitvoering van een query in.

AsQueryable(IEnumerable)

Converteert een IEnumerable naar een IQueryable.

Cast<TResult>(IEnumerable)

Cast de elementen van een IEnumerable naar het opgegeven type.

OfType<TResult>(IEnumerable)

Hiermee filtert u de elementen van een IEnumerable op basis van een opgegeven type.

Van toepassing op

Veiligheid thread

Openbare statische (Shared in Visual Basic) leden van dit type zijn thread-veilig. Exemplaarleden zijn niet gegarandeerd thread-safe.

Deze implementatie biedt geen gesynchroniseerde wrapper (thread safe) voor een CollectionBase, maar afgeleide klassen kunnen hun eigen gesynchroniseerde versies van de CollectionBaseSyncRoot eigenschap maken.

Het inventariseren via een verzameling is intrinsiek geen threadveilige procedure. Zelfs wanneer een verzameling wordt gesynchroniseerd, kunnen andere threads de verzameling nog steeds wijzigen, waardoor de enumerator een uitzondering genereert. Om de veiligheid van threads tijdens de inventarisatie te garanderen, kunt u de verzameling vergrendelen tijdens de volledige inventarisatie of de uitzonderingen ondervangen die het gevolg zijn van wijzigingen die door andere threads zijn aangebracht.

Zie ook