NameObjectCollectionBase Sınıf

Tanım

abstract Anahtarla veya dizinle erişilebilen ilişkili String anahtarlar ve Object değerler koleksiyonu için temel sınıfı sağlar.

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
public abstract class NameObjectCollectionBase : System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Serializable]
public abstract class NameObjectCollectionBase : System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type NameObjectCollectionBase = class
    interface ICollection
    interface IEnumerable
type NameObjectCollectionBase = class
    interface ICollection
    interface IEnumerable
    interface IDeserializationCallback
    interface ISerializable
type NameObjectCollectionBase = class
    interface ICollection
    interface IEnumerable
    interface ISerializable
    interface IDeserializationCallback
[<System.Serializable>]
type NameObjectCollectionBase = class
    interface ICollection
    interface IEnumerable
    interface ISerializable
    interface IDeserializationCallback
Public MustInherit Class NameObjectCollectionBase
Implements ICollection
Public MustInherit Class NameObjectCollectionBase
Implements ICollection, IDeserializationCallback, ISerializable
Devralma
NameObjectCollectionBase
Türetilmiş
Öznitelikler
Uygulamalar

Örnekler

Aşağıdaki kod örneği, sınıfın NameObjectCollectionBase nasıl uygulanıp kullanılacağını gösterir.

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):

Açıklamalar

Bu sınıfın temel yapısı bir karma tablodur.

Her öğe bir anahtar/değer çiftidir.

'nin NameObjectCollectionBase kapasitesi, barındırabileceği öğelerin NameObjectCollectionBase sayısıdır. öğesine öğeler eklendikçe NameObjectCollectionBasekapasite, yeniden ayırma aracılığıyla gerektiği gibi otomatik olarak artırılır.

Karma kod sağlayıcısı örnekteki NameObjectCollectionBase anahtarlar için karma kodları dağıtır. Varsayılan karma kod sağlayıcısıdır CaseInsensitiveHashCodeProvider.

Karşılaştırıcı, iki anahtarın eşit olup olmadığını belirler. Varsayılan karşılaştırıcıdır CaseInsensitiveComparer.

.NET Framework sürüm 1.0'da bu sınıf kültüre duyarlı dize karşılaştırmaları kullanır. Ancak, .NET Framework sürüm 1.1 ve sonraki sürümlerinde bu sınıf, dizeleri karşılaştırırken CultureInfo.InvariantCulture kullanır. Kültürün karşılaştırmaları ve sıralamayı nasıl etkilediği hakkında daha fazla bilgi için bkz. Culture-Insensitive Dize İşlemleri Gerçekleştirme.

null anahtar veya değer olarak izin verilir.

Caution

yöntemi, BaseGet belirtilen anahtar bulunamadığından null ve anahtarla ilişkili değer olduğundan döndürülen değeri nullarasında null ayrım yapmaz.

Oluşturucular

Name Description
NameObjectCollectionBase()

Sınıfın NameObjectCollectionBase boş olan yeni bir örneğini başlatır.

NameObjectCollectionBase(IEqualityComparer)

Sınıfın NameObjectCollectionBase boş, varsayılan başlangıç kapasitesine sahip yeni bir örneğini başlatır ve belirtilen IEqualityComparer nesneyi kullanır.

NameObjectCollectionBase(IHashCodeProvider, IComparer)
Geçersiz.
Geçersiz.

Sınıfın NameObjectCollectionBase boş, varsayılan başlangıç kapasitesine sahip yeni bir örneğini başlatır ve belirtilen karma kod sağlayıcısını ve belirtilen karşılaştırıcıyı kullanır.

NameObjectCollectionBase(Int32, IEqualityComparer)

Sınıfın NameObjectCollectionBase boş, belirtilen başlangıç kapasitesine sahip yeni bir örneğini başlatır ve belirtilen IEqualityComparer nesneyi kullanır.

NameObjectCollectionBase(Int32, IHashCodeProvider, IComparer)
Geçersiz.
Geçersiz.

Sınıfın NameObjectCollectionBase boş, belirtilen ilk kapasiteye sahip yeni bir örneğini başlatır ve belirtilen karma kod sağlayıcısını ve belirtilen karşılaştırıcıyı kullanır.

NameObjectCollectionBase(Int32)

Sınıfın NameObjectCollectionBase boş, belirtilen ilk kapasiteye sahip yeni bir örneğini başlatır ve varsayılan karma kod sağlayıcısını ve varsayılan karşılaştırıcıyı kullanır.

NameObjectCollectionBase(SerializationInfo, StreamingContext)
Geçersiz.

sınıfının serileştirilebilir yeni bir örneğini NameObjectCollectionBase başlatır ve belirtilen SerializationInfo ve StreamingContextkullanır.

Özellikler

Name Description
Count

Örnekte bulunan anahtar/değer çiftlerinin NameObjectCollectionBase sayısını alır.

IsReadOnly

Örneğin salt okunur olup olmadığını NameObjectCollectionBase belirten bir değer alır veya ayarlar.

Keys

Örnekteki tüm anahtarları NameObjectCollectionBase.KeysCollection içeren bir NameObjectCollectionBase örneği alır.

Yöntemler

Name Description
BaseAdd(String, Object)

Örneğe belirtilen anahtar ve değere NameObjectCollectionBase sahip bir girdi ekler.

BaseClear()

Örnekteki NameObjectCollectionBase tüm girişleri kaldırır.

BaseGet(Int32)

Örneğin belirtilen dizinindeki girdinin NameObjectCollectionBase değerini alır.

BaseGet(String)

Örnekten belirtilen anahtara sahip ilk girdinin NameObjectCollectionBase değerini alır.

BaseGetAllKeys()

Örnekteki tüm anahtarları String içeren bir NameObjectCollectionBase dizi döndürür.

BaseGetAllValues()

Örnekteki tüm değerleri Object içeren bir NameObjectCollectionBase dizi döndürür.

BaseGetAllValues(Type)

Örnekteki tüm değerleri NameObjectCollectionBase içeren belirtilen türde bir dizi döndürür.

BaseGetKey(Int32)

Örneğin belirtilen dizininde girdinin NameObjectCollectionBase anahtarını alır.

BaseHasKeys()

Örneğin anahtarları olmayan NameObjectCollectionBasegirdiler içerip içermediğini null belirten bir değer alır.

BaseRemove(String)

Belirtilen anahtara sahip girişleri örnekten NameObjectCollectionBase kaldırır.

BaseRemoveAt(Int32)

Örneğin belirtilen dizinindeki girdiyi NameObjectCollectionBase kaldırır.

BaseSet(Int32, Object)

Girdinin değerini örneğin belirtilen dizininde NameObjectCollectionBase ayarlar.

BaseSet(String, Object)

Örnekte belirtilen anahtarla NameObjectCollectionBase ilk girdinin değerini ayarlar; bulunursa, aksi takdirde örneğe belirtilen anahtar ve değere NameObjectCollectionBase sahip bir girdi ekler.

Equals(Object)

Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler.

(Devralındığı yer: Object)
GetEnumerator()

aracılığıyla NameObjectCollectionBaseyineleyen bir numaralandırıcı döndürür.

GetHashCode()

Varsayılan karma işlevi işlevi görür.

(Devralındığı yer: Object)
GetObjectData(SerializationInfo, StreamingContext)
Geçersiz.

Arabirimini ISerializable uygular ve örneği serileştirmek NameObjectCollectionBase için gereken verileri döndürür.

GetType()

Geçerli örneğin Type alır.

(Devralındığı yer: Object)
MemberwiseClone()

Geçerli Objectbasit bir kopyasını oluşturur.

(Devralındığı yer: Object)
OnDeserialization(Object)

Arabirimini ISerializable uygular ve seri durumdan çıkarma işlemi tamamlandığında seri durumdan çıkarma olayını başlatır.

ToString()

Geçerli nesneyi temsil eden bir dize döndürür.

(Devralındığı yer: Object)

Belirtik Arabirim Kullanımları

Name Description
ICollection.CopyTo(Array, Int32)

Hedef dizinin belirtilen dizininden başlayarak tamamını NameObjectCollectionBase uyumlu bir tek boyutluya Arraykopyalar.

ICollection.IsSynchronized

Nesneye erişimin NameObjectCollectionBase eşitlenip eşitlenmediğini belirten bir değer alır (iş parçacığı güvenli).

ICollection.SyncRoot

Nesneye erişimi NameObjectCollectionBase eşitlemek için kullanılabilecek bir nesne alır.

Uzantı Metotları

Name Description
AsParallel(IEnumerable)

Sorgunun paralelleştirilmesini etkinleştirir.

AsQueryable(IEnumerable)

bir IEnumerable öğesine IQueryabledönüştürür.

Cast<TResult>(IEnumerable)

öğesinin IEnumerable öğelerini belirtilen türe yazar.

OfType<TResult>(IEnumerable)

Belirtilen türe göre bir IEnumerable öğesinin öğelerini filtreler.

Şunlara uygulanır

İş Parçacığı Güvenliği

Bu türün genel statik (Shared Visual Basic'te) üyeleri iş parçacığı güvenlidir. Hiçbir örnek üyesi için iş parçacığı güvenliği garanti edilmez.

Bu uygulama, için NameObjectCollectionBaseeşitlenmiş (iş parçacığı güvenli) sarmalayıcı sağlamaz, ancak türetilmiş sınıflar özelliğini kullanarak NameObjectCollectionBase kendi eşitlenmiş sürümlerini SyncRoot oluşturabilir.

Bir koleksiyonda numaralandırma, iş parçacığı güvenli bir yordam değildir. Bir koleksiyon eşitlendiğinde bile, diğer iş parçacıkları yine de koleksiyonu değiştirebilir ve bu da numaralandırıcının bir özel durum oluşturmasına neden olur. Numaralandırma sırasında iş parçacığı güvenliğini garanti etmek için, tüm numaralandırma sırasında koleksiyonu kilitleyebilir veya diğer iş parçacıkları tarafından yapılan değişikliklerden kaynaklanan özel durumları yakalayabilirsiniz.

Ayrıca bkz.