NameObjectCollectionBase Klas
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
public ref class NameObjectCollectionBase abstract : System::Collections::ICollection
public ref class NameObjectCollectionBase abstract : System::Collections::ICollection, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
public abstract class NameObjectCollectionBase : System.Collections.ICollection
[System.Serializable]
public abstract class NameObjectCollectionBase : System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
public abstract class NameObjectCollectionBase : System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type NameObjectCollectionBase = class
interface ICollection
interface IEnumerable
[<System.Serializable>]
type NameObjectCollectionBase = class
interface ICollection
interface IEnumerable
interface ISerializable
interface IDeserializationCallback
type NameObjectCollectionBase = class
interface ICollection
interface IEnumerable
interface IDeserializationCallback
interface ISerializable
Public MustInherit Class NameObjectCollectionBase
Implements ICollection
Public MustInherit Class NameObjectCollectionBase
Implements ICollection, IDeserializationCallback, ISerializable
- Overname
-
NameObjectCollectionBase
- Afgeleid
- Kenmerken
- Implementeringen
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u de NameObjectCollectionBase klasse implementeert en gebruikt.
using System;
using System.Collections;
using System.Collections.Specialized;
public class MyCollection : NameObjectCollectionBase
{
// Creates an empty collection.
public MyCollection() {
}
// Adds elements from an IDictionary into the new collection.
public MyCollection( IDictionary d, Boolean bReadOnly ) {
foreach ( DictionaryEntry de in d ) {
this.BaseAdd( (String) de.Key, de.Value );
}
this.IsReadOnly = bReadOnly;
}
// Gets a key-and-value pair (DictionaryEntry) using an index.
public DictionaryEntry this[ int index ] {
get {
return ( new DictionaryEntry(
this.BaseGetKey(index), this.BaseGet(index) ) );
}
}
// Gets or sets the value associated with the specified key.
public Object this[ String key ] {
get {
return( this.BaseGet( key ) );
}
set {
this.BaseSet( key, value );
}
}
// Gets a String array that contains all the keys in the collection.
public String[] AllKeys {
get {
return( this.BaseGetAllKeys() );
}
}
// Gets an Object array that contains all the values in the collection.
public Array AllValues {
get {
return( this.BaseGetAllValues() );
}
}
// Gets a String array that contains all the values in the collection.
public String[] AllStringValues {
get {
return( (String[]) this.BaseGetAllValues( typeof( string ) ));
}
}
// Gets a value indicating if the collection contains keys that are not null.
public Boolean HasKeys {
get {
return( this.BaseHasKeys() );
}
}
// Adds an entry to the collection.
public void Add( String key, Object value ) {
this.BaseAdd( key, value );
}
// Removes an entry with the specified key from the collection.
public void Remove( String key ) {
this.BaseRemove( key );
}
// Removes an entry in the specified index from the collection.
public void Remove( int index ) {
this.BaseRemoveAt( index );
}
// Clears all the elements in the collection.
public void Clear() {
this.BaseClear();
}
}
public class SamplesNameObjectCollectionBase {
public static void Main() {
// Creates and initializes a new MyCollection that is read-only.
IDictionary d = new ListDictionary();
d.Add( "red", "apple" );
d.Add( "yellow", "banana" );
d.Add( "green", "pear" );
MyCollection myROCol = new MyCollection( d, true );
// Tries to add a new item.
try {
myROCol.Add( "blue", "sky" );
}
catch ( NotSupportedException e ) {
Console.WriteLine( e.ToString() );
}
// Displays the keys and values of the MyCollection.
Console.WriteLine( "Read-Only Collection:" );
PrintKeysAndValues( myROCol );
// Creates and initializes an empty MyCollection that is writable.
MyCollection myRWCol = new MyCollection();
// Adds new items to the collection.
myRWCol.Add( "purple", "grape" );
myRWCol.Add( "orange", "tangerine" );
myRWCol.Add( "black", "berries" );
Console.WriteLine( "Writable Collection (after adding values):" );
PrintKeysAndValues( myRWCol );
// Changes the value of one element.
myRWCol["orange"] = "grapefruit";
Console.WriteLine( "Writable Collection (after changing one value):" );
PrintKeysAndValues( myRWCol );
// Removes one item from the collection.
myRWCol.Remove( "black" );
Console.WriteLine( "Writable Collection (after removing one value):" );
PrintKeysAndValues( myRWCol );
// Removes all elements from the collection.
myRWCol.Clear();
Console.WriteLine( "Writable Collection (after clearing the collection):" );
PrintKeysAndValues( myRWCol );
}
// Prints the indexes, keys, and values.
public static void PrintKeysAndValues( MyCollection myCol ) {
for ( int i = 0; i < myCol.Count; i++ ) {
Console.WriteLine( "[{0}] : {1}, {2}", i, myCol[i].Key, myCol[i].Value );
}
}
// Prints the keys and values using AllKeys.
public static void PrintKeysAndValues2( MyCollection myCol ) {
foreach ( String s in myCol.AllKeys ) {
Console.WriteLine( "{0}, {1}", s, myCol[s] );
}
}
}
/*
This code produces the following output.
System.NotSupportedException: Collection is read-only.
at System.Collections.Specialized.NameObjectCollectionBase.BaseAdd(String name, Object value)
at SamplesNameObjectCollectionBase.Main()
Read-Only Collection:
[0] : red, apple
[1] : yellow, banana
[2] : green, pear
Writable Collection (after adding values):
[0] : purple, grape
[1] : orange, tangerine
[2] : black, berries
Writable Collection (after changing one value):
[0] : purple, grape
[1] : orange, grapefruit
[2] : black, berries
Writable Collection (after removing one value):
[0] : purple, grape
[1] : orange, grapefruit
Writable Collection (after clearing the collection):
*/
Imports System.Collections
Imports System.Collections.Specialized
Public Class MyCollection
Inherits NameObjectCollectionBase
' Creates an empty collection.
Public Sub New()
End Sub
' Adds elements from an IDictionary into the new collection.
Public Sub New(d As IDictionary, bReadOnly As Boolean)
Dim de As DictionaryEntry
For Each de In d
Me.BaseAdd(CType(de.Key, String), de.Value)
Next de
Me.IsReadOnly = bReadOnly
End Sub
' Gets a key-and-value pair (DictionaryEntry) using an index.
Default Public ReadOnly Property Item(index As Integer) As DictionaryEntry
Get
return new DictionaryEntry( _
me.BaseGetKey(index), me.BaseGet(index) )
End Get
End Property
' Gets or sets the value associated with the specified key.
Default Public Property Item(key As String) As Object
Get
Return Me.BaseGet(key)
End Get
Set
Me.BaseSet(key, value)
End Set
End Property
' Gets a String array that contains all the keys in the collection.
Public ReadOnly Property AllKeys() As String()
Get
Return Me.BaseGetAllKeys()
End Get
End Property
' Gets an Object array that contains all the values in the collection.
Public ReadOnly Property AllValues() As Array
Get
Return Me.BaseGetAllValues()
End Get
End Property
' Gets a String array that contains all the values in the collection.
Public ReadOnly Property AllStringValues() As String()
Get
Return CType(Me.BaseGetAllValues(GetType(String)), String())
End Get
End Property
' Gets a value indicating if the collection contains keys that are not null.
Public ReadOnly Property HasKeys() As Boolean
Get
Return Me.BaseHasKeys()
End Get
End Property
' Adds an entry to the collection.
Public Sub Add(key As String, value As Object)
Me.BaseAdd(key, value)
End Sub
' Removes an entry with the specified key from the collection.
Overloads Public Sub Remove(key As String)
Me.BaseRemove(key)
End Sub
' Removes an entry in the specified index from the collection.
Overloads Public Sub Remove(index As Integer)
Me.BaseRemoveAt(index)
End Sub
' Clears all the elements in the collection.
Public Sub Clear()
Me.BaseClear()
End Sub
End Class
Public Class SamplesNameObjectCollectionBase
Public Shared Sub Main()
' Creates and initializes a new MyCollection that is read-only.
Dim d As New ListDictionary()
d.Add("red", "apple")
d.Add("yellow", "banana")
d.Add("green", "pear")
Dim myROCol As New MyCollection(d, True)
' Tries to add a new item.
Try
myROCol.Add("blue", "sky")
Catch e As NotSupportedException
Console.WriteLine(e.ToString())
End Try
' Displays the keys and values of the MyCollection.
Console.WriteLine("Read-Only Collection:")
PrintKeysAndValues(myROCol)
' Creates and initializes an empty MyCollection that is writable.
Dim myRWCol As New MyCollection()
' Adds new items to the collection.
myRWCol.Add("purple", "grape")
myRWCol.Add("orange", "tangerine")
myRWCol.Add("black", "berries")
Console.WriteLine("Writable Collection (after adding values):")
PrintKeysAndValues(myRWCol)
' Changes the value of one element.
myRWCol("orange") = "grapefruit"
Console.WriteLine("Writable Collection (after changing one value):")
PrintKeysAndValues(myRWCol)
' Removes one item from the collection.
myRWCol.Remove("black")
Console.WriteLine("Writable Collection (after removing one value):")
PrintKeysAndValues(myRWCol)
' Removes all elements from the collection.
myRWCol.Clear()
Console.WriteLine("Writable Collection (after clearing the collection):")
PrintKeysAndValues(myRWCol)
End Sub
' Prints the indexes, keys, and values.
Public Shared Sub PrintKeysAndValues(myCol As MyCollection)
Dim i As Integer
For i = 0 To myCol.Count - 1
Console.WriteLine("[{0}] : {1}, {2}", i, myCol(i).Key, myCol(i).Value)
Next i
End Sub
' Prints the keys and values using AllKeys.
Public Shared Sub PrintKeysAndValues2(myCol As MyCollection)
Dim s As String
For Each s In myCol.AllKeys
Console.WriteLine("{0}, {1}", s, myCol(s))
Next s
End Sub
End Class
'This code produces the following output.
'
'System.NotSupportedException: Collection is read-only.
' at System.Collections.Specialized.NameObjectCollectionBase.BaseAdd(String name, Object value)
' at SamplesNameObjectCollectionBase.Main()
'Read-Only Collection:
'[0] : red, apple
'[1] : yellow, banana
'[2] : green, pear
'Writable Collection (after adding values):
'[0] : purple, grape
'[1] : orange, tangerine
'[2] : black, berries
'Writable Collection (after changing one value):
'[0] : purple, grape
'[1] : orange, grapefruit
'[2] : black, berries
'Writable Collection (after removing one value):
'[0] : purple, grape
'[1] : orange, grapefruit
'Writable Collection (after clearing the collection):
Opmerkingen
De onderliggende structuur voor deze klasse is een hash-tabel.
Elk element is een sleutel-waardepaar.
De capaciteit van een NameObjectCollectionBase is het aantal elementen dat de NameObjectCollectionBase kan bevatten. Als er elementen aan een NameObjectCollectionBaseworden toegevoegd, wordt de capaciteit automatisch verhoogd als vereist via herlocatie.
De hashcodeprovider dispenseert hash-codes voor sleutels in het NameObjectCollectionBase exemplaar. De standaard-hashcodeprovider is de CaseInsensitiveHashCodeProvider.
De vergelijkingsfunctie bepaalt of twee sleutels gelijk zijn. De standaard comparer is de CaseInsensitiveComparer.
In .NET Framework versie 1.0 gebruikt deze klasse cultuurgevoelige tekenreeksvergelijkingen. In .NET Framework versie 1.1 en hoger gebruikt deze klasse echter CultureInfo.InvariantCulture bij het vergelijken van tekenreeksen. Zie Culture-Insensitive Tekenreeksbewerkingen uitvoeren voor meer informatie over hoe cultuur van invloed is op vergelijkingen en sorteren.
null is toegestaan als een sleutel of als een waarde.
Caution
De BaseGet methode maakt geen onderscheid tussen null die geretourneerd omdat de opgegeven sleutel niet wordt gevonden en null die wordt geretourneerd omdat de waarde die is gekoppeld aan de sleutel is null.
Constructors
| Name | Description |
|---|---|
| NameObjectCollectionBase() |
Initialiseert een nieuw exemplaar van de NameObjectCollectionBase klasse die leeg is. |
| NameObjectCollectionBase(IEqualityComparer) |
Initialiseert een nieuw exemplaar van de NameObjectCollectionBase klasse die leeg is, heeft de standaardinitiële capaciteit en gebruikt het opgegeven IEqualityComparer object. |
| NameObjectCollectionBase(IHashCodeProvider, IComparer) |
Verouderd.
Initialiseert een nieuw exemplaar van de NameObjectCollectionBase klasse die leeg is, heeft de standaardinitiële capaciteit en gebruikt de opgegeven hashcodeprovider en de opgegeven vergelijkingsfunctie. |
| NameObjectCollectionBase(Int32, IEqualityComparer) |
Initialiseert een nieuw exemplaar van de NameObjectCollectionBase klasse die leeg is, heeft de opgegeven initiële capaciteit en gebruikt het opgegeven IEqualityComparer object. |
| NameObjectCollectionBase(Int32, IHashCodeProvider, IComparer) |
Verouderd.
Initialiseert een nieuw exemplaar van de NameObjectCollectionBase klasse die leeg is, heeft de opgegeven initiële capaciteit en maakt gebruik van de opgegeven hashcodeprovider en de opgegeven vergelijkingsfunctie. |
| NameObjectCollectionBase(Int32) |
Initialiseert een nieuw exemplaar van de NameObjectCollectionBase klasse die leeg is, heeft de opgegeven initiële capaciteit en gebruikt de standaard-hashcodeprovider en de standaardvergelijker. |
| NameObjectCollectionBase(SerializationInfo, StreamingContext) |
Initialiseert een nieuw exemplaar van de NameObjectCollectionBase klasse die serialiseerbaar is en maakt gebruik van de opgegeven SerializationInfo en StreamingContext. |
Eigenschappen
| Name | Description |
|---|---|
| Count |
Hiermee haalt u het aantal sleutel-/waardeparen op dat is opgenomen in het NameObjectCollectionBase exemplaar. |
| IsReadOnly |
Hiermee wordt een waarde opgehaald of ingesteld die aangeeft of het NameObjectCollectionBase exemplaar het kenmerk Alleen-lezen heeft. |
| Keys |
Hiermee haalt u een NameObjectCollectionBase.KeysCollection exemplaar op dat alle sleutels in het NameObjectCollectionBase exemplaar bevat. |
Methoden
| Name | Description |
|---|---|
| BaseAdd(String, Object) |
Voegt een vermelding met de opgegeven sleutel en waarde toe aan het NameObjectCollectionBase exemplaar. |
| BaseClear() |
Hiermee verwijdert u alle vermeldingen uit het NameObjectCollectionBase exemplaar. |
| BaseGet(Int32) |
Hiermee haalt u de waarde op van de vermelding in de opgegeven index van het NameObjectCollectionBase exemplaar. |
| BaseGet(String) |
Hiermee haalt u de waarde op van de eerste vermelding met de opgegeven sleutel van het NameObjectCollectionBase exemplaar. |
| BaseGetAllKeys() |
Retourneert een String matrix die alle sleutels in het NameObjectCollectionBase exemplaar bevat. |
| BaseGetAllValues() |
Retourneert een Object matrix die alle waarden in het NameObjectCollectionBase exemplaar bevat. |
| BaseGetAllValues(Type) |
Retourneert een matrix van het opgegeven type dat alle waarden in het NameObjectCollectionBase exemplaar bevat. |
| BaseGetKey(Int32) |
Hiermee haalt u de sleutel van de vermelding op in de opgegeven index van het NameObjectCollectionBase exemplaar. |
| BaseHasKeys() |
Hiermee wordt een waarde opgehaald die aangeeft of het NameObjectCollectionBase exemplaar vermeldingen bevat waarvan de sleutels niet |
| BaseRemove(String) |
Hiermee verwijdert u de vermeldingen met de opgegeven sleutel uit het NameObjectCollectionBase exemplaar. |
| BaseRemoveAt(Int32) |
Hiermee verwijdert u de vermelding in de opgegeven index van het NameObjectCollectionBase exemplaar. |
| BaseSet(Int32, Object) |
Hiermee stelt u de waarde van de vermelding in op de opgegeven index van het NameObjectCollectionBase exemplaar. |
| BaseSet(String, Object) |
Hiermee stelt u de waarde van de eerste vermelding in met de opgegeven sleutel in het NameObjectCollectionBase exemplaar, indien gevonden; anders voegt u een vermelding toe met de opgegeven sleutel en waarde in het NameObjectCollectionBase exemplaar. |
| Equals(Object) |
Bepaalt of het opgegeven object gelijk is aan het huidige object. (Overgenomen van Object) |
| GetEnumerator() |
Retourneert een enumerator die door de NameObjectCollectionBase. |
| GetHashCode() |
Fungeert als de standaardhashfunctie. (Overgenomen van Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
Implementeert de ISerializable interface en retourneert de gegevens die nodig zijn om het NameObjectCollectionBase exemplaar te serialiseren. |
| 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) |
| OnDeserialization(Object) |
Implementeert de ISerializable interface en verhoogt de deserialisatie-gebeurtenis wanneer de deserialisatie is voltooid. |
| ToString() |
Retourneert een tekenreeks die het huidige object vertegenwoordigt. (Overgenomen van Object) |
Expliciete interface-implementaties
| Name | Description |
|---|---|
| ICollection.CopyTo(Array, Int32) |
Kopieert het hele NameObjectCollectionBase 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 het NameObjectCollectionBase object wordt gesynchroniseerd (thread safe). |
| ICollection.SyncRoot |
Hiermee haalt u een object op dat kan worden gebruikt om de toegang tot het NameObjectCollectionBase object te synchroniseren. |
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 NameObjectCollectionBase, maar afgeleide klassen kunnen hun eigen gesynchroniseerde versies van de NameObjectCollectionBaseSyncRoot 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.