NameObjectCollectionBase NameObjectCollectionBase NameObjectCollectionBase NameObjectCollectionBase Class

定義

提供 abstract 基底類別給可以用索引鍵或索引來存取之相關聯的 String 索引鍵和 Object 值集合。Provides the abstract base class for a collection of associated String keys and Object values that can be accessed either with the key or with the index.

public ref class NameObjectCollectionBase abstract : 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 ISerializable
    interface IDeserializationCallback
    interface IEnumerable
Public MustInherit Class NameObjectCollectionBase
Implements ICollection, IDeserializationCallback, ISerializable
繼承
NameObjectCollectionBaseNameObjectCollectionBaseNameObjectCollectionBaseNameObjectCollectionBase
衍生
屬性
實作

範例

下列程式碼範例示範如何執行和使用NameObjectCollectionBase類別。The following code example shows how to implement and use the NameObjectCollectionBase class.

#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;

public ref class MyCollection : public NameObjectCollectionBase  {

private:
   DictionaryEntry^ _de;

   // Creates an empty collection.
public:
   MyCollection()  {
      _de = gcnew DictionaryEntry();
   }

   // Adds elements from an IDictionary into the new collection.
   MyCollection( IDictionary^ d, Boolean bReadOnly )  {

      _de = gcnew DictionaryEntry();

      for each ( DictionaryEntry^ de in d )  {
         this->BaseAdd( (String^) de->Key, de->Value );
      }
      this->IsReadOnly = bReadOnly;
   }

   // Gets a key-and-value pair (DictionaryEntry) using an index.
   property DictionaryEntry^ default[ int ]  {
      DictionaryEntry^ get(int index)  {
         _de->Key = this->BaseGetKey(index);
         _de->Value = this->BaseGet(index);
         return( _de );
      }
   }

   // Gets or sets the value associated with the specified key.
   property Object^ default[ String^ ]  {
      Object^ get(String^ key)  {
         return( this->BaseGet( key ) );
      }
      void set( String^ key, Object^ value )  {
         this->BaseSet( key, value );
      }
   }

   // Gets a String array that contains all the keys in the collection.
   property array<String^>^ AllKeys  {
      array<String^>^ get()  {
         return( (array<String^>^)this->BaseGetAllKeys() );
      }
   }

   // Gets an Object array that contains all the values in the collection.
   property Array^ AllValues  {
      Array^ get()  {
         return( this->BaseGetAllValues() );
      }
   }

   // Gets a String array that contains all the values in the collection.
   property array<String^>^ AllStringValues  {
      array<String^>^ get()  {
         return( (array<String^>^) this->BaseGetAllValues(  String ::typeid ));
      }
   }

   // Gets a value indicating if the collection contains keys that are not null.
   property Boolean HasKeys  {
      Boolean get()  {
         return( this->BaseHasKeys() );
      }
   }

   // Adds an entry to the collection.
   void Add( String^ key, Object^ value )  {
      this->BaseAdd( key, value );
   }

   // Removes an entry with the specified key from the collection.
   void Remove( String^ key )  {
      this->BaseRemove( key );
   }

   // Removes an entry in the specified index from the collection.
   void Remove( int index )  {
      this->BaseRemoveAt( index );
   }

   // Clears all the elements in the collection.
   void Clear()  {
      this->BaseClear();
   }
};

public ref class SamplesNameObjectCollectionBase  {

public:
   static void Main()  {
      // Creates and initializes a new MyCollection that is read-only.
      IDictionary^ d = gcnew ListDictionary();
      d->Add( "red", "apple" );
      d->Add( "yellow", "banana" );
      d->Add( "green", "pear" );
      MyCollection^ myROCol = gcnew 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 = gcnew 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.
   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.
   static void PrintKeysAndValues2( MyCollection^ myCol )  {
      for each ( String^ s in myCol->AllKeys )  {
         Console::WriteLine( "{0}, {1}", s, myCol[s] );
      }
   }
};

int main()
{
    SamplesNameObjectCollectionBase::Main();
}

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

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

備註

這個類別的基礎結構是雜湊表。The underlying structure for this class is a hash table.

每個元素都是索引鍵/值組。Each element is a key/value pair.

的容量NameObjectCollectionBaseNameObjectCollectionBase可以保存的元素數目。The capacity of a NameObjectCollectionBase is the number of elements the NameObjectCollectionBase can hold. 當專案新增至時NameObjectCollectionBase,會視需要透過重新配置來自動增加容量。As elements are added to a NameObjectCollectionBase, the capacity is automatically increased as required through reallocation.

雜湊程式碼提供者會不表達NameObjectCollectionBase實例中之索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the NameObjectCollectionBase instance. 預設雜湊程式碼提供者CaseInsensitiveHashCodeProvider為。The default hash code provider is the CaseInsensitiveHashCodeProvider.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. 預設比較子是CaseInsensitiveComparerThe default comparer is the CaseInsensitiveComparer.

在 .NET Framework 版本1.0 中,這個類別會使用區分文化特性的字串比較。In .NET Framework version 1.0, this class uses culture-sensitive string comparisons. 不過,在 .NET Framework 1.1 版和更新版本中,此CultureInfo.InvariantCulture類別會在比較字串時使用。However, in .NET Framework version 1.1 and later, this class uses CultureInfo.InvariantCulture when comparing strings. 如需文化特性如何影響比較和排序的詳細資訊,請參閱執行不區分文化特性的字串作業For more information about how culture affects comparisons and sorting, see Performing Culture-Insensitive String Operations.

null允許做為索引鍵或值。null is allowed as a key or as a value.

警告

方法不會null區分傳回的,因為找不到指定的索引鍵,而且null會傳回,因為與索引鍵相關聯的值null是。 BaseGetThe BaseGet method does not distinguish between null which is returned because the specified key is not found and null which is returned because the value associated with the key is null.

建構函式

NameObjectCollectionBase() NameObjectCollectionBase() NameObjectCollectionBase() NameObjectCollectionBase()

初始化 NameObjectCollectionBase 類別的新執行個體,這個執行個體是空白的。Initializes a new instance of the NameObjectCollectionBase class that is empty.

NameObjectCollectionBase(IEqualityComparer) NameObjectCollectionBase(IEqualityComparer) NameObjectCollectionBase(IEqualityComparer) NameObjectCollectionBase(IEqualityComparer)

初始化 NameObjectCollectionBase 類別的新執行個體,這個執行個體是空白的、具有預設的初始容量,並使用指定的 IEqualityComparer 物件。Initializes a new instance of the NameObjectCollectionBase class that is empty, has the default initial capacity, and uses the specified IEqualityComparer object.

NameObjectCollectionBase(IHashCodeProvider, IComparer) NameObjectCollectionBase(IHashCodeProvider, IComparer) NameObjectCollectionBase(IHashCodeProvider, IComparer) NameObjectCollectionBase(IHashCodeProvider, IComparer)

初始化 NameObjectCollectionBase 類別的新執行個體,這個執行個體是空白的、具有預設的初始容量,並使用指定的雜湊碼提供者和指定的比較子。Initializes a new instance of the NameObjectCollectionBase class that is empty, has the default initial capacity, and uses the specified hash code provider and the specified comparer.

NameObjectCollectionBase(Int32) NameObjectCollectionBase(Int32) NameObjectCollectionBase(Int32) NameObjectCollectionBase(Int32)

初始化 NameObjectCollectionBase 類別的新執行個體,這個執行個體是空白的、具有指定的初始容量,並使用預設雜湊碼提供者和預設比較子。Initializes a new instance of the NameObjectCollectionBase class that is empty, has the specified initial capacity, and uses the default hash code provider and the default comparer.

NameObjectCollectionBase(Int32, IEqualityComparer) NameObjectCollectionBase(Int32, IEqualityComparer) NameObjectCollectionBase(Int32, IEqualityComparer) NameObjectCollectionBase(Int32, IEqualityComparer)

初始化 NameObjectCollectionBase 類別的新執行個體,這個執行個體是空白的、具有指定的初始容量,並使用指定的 IEqualityComparer 物件。Initializes a new instance of the NameObjectCollectionBase class that is empty, has the specified initial capacity, and uses the specified IEqualityComparer object.

NameObjectCollectionBase(Int32, IHashCodeProvider, IComparer) NameObjectCollectionBase(Int32, IHashCodeProvider, IComparer) NameObjectCollectionBase(Int32, IHashCodeProvider, IComparer) NameObjectCollectionBase(Int32, IHashCodeProvider, IComparer)

初始化 NameObjectCollectionBase 類別的新執行個體,這個執行個體是空白的、具有指定的初始容量,並使用指定的雜湊碼提供者和指定的比較子。Initializes a new instance of the NameObjectCollectionBase class that is empty, has the specified initial capacity and uses the specified hash code provider and the specified comparer.

NameObjectCollectionBase(SerializationInfo, StreamingContext) NameObjectCollectionBase(SerializationInfo, StreamingContext) NameObjectCollectionBase(SerializationInfo, StreamingContext) NameObjectCollectionBase(SerializationInfo, StreamingContext)

初始化 NameObjectCollectionBase 類別的新執行個體,這個執行個體可序列化,並使用指定的 SerializationInfoStreamingContextInitializes a new instance of the NameObjectCollectionBase class that is serializable and uses the specified SerializationInfo and StreamingContext.

屬性

Count Count Count Count

取得 NameObjectCollectionBase 執行個體中包含的索引鍵/值組數目。Gets the number of key/value pairs contained in the NameObjectCollectionBase instance.

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

取得或設定值,表示 NameObjectCollectionBase 執行個體是否為唯讀。Gets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.

Keys Keys Keys Keys

取得 NameObjectCollectionBase.KeysCollection 執行個體,其中包含 NameObjectCollectionBase 執行個體中的所有索引鍵。Gets a NameObjectCollectionBase.KeysCollection instance that contains all the keys in the NameObjectCollectionBase instance.

方法

BaseAdd(String, Object) BaseAdd(String, Object) BaseAdd(String, Object) BaseAdd(String, Object)

將具有指定索引鍵和值的項目加入 NameObjectCollectionBase 執行個體。Adds an entry with the specified key and value into the NameObjectCollectionBase instance.

BaseClear() BaseClear() BaseClear() BaseClear()

將所有項目從 NameObjectCollectionBase 執行個體中移除。Removes all entries from the NameObjectCollectionBase instance.

BaseGet(Int32) BaseGet(Int32) BaseGet(Int32) BaseGet(Int32)

取得 NameObjectCollectionBase 執行個體指定索引處之項目的值。Gets the value of the entry at the specified index of the NameObjectCollectionBase instance.

BaseGet(String) BaseGet(String) BaseGet(String) BaseGet(String)

NameObjectCollectionBase 執行個體取得具有指定索引鍵之第一個項目的值。Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance.

BaseGetAllKeys() BaseGetAllKeys() BaseGetAllKeys() BaseGetAllKeys()

傳回 String 陣列,其中包含 NameObjectCollectionBase 執行個體中的所有索引鍵。Returns a String array that contains all the keys in the NameObjectCollectionBase instance.

BaseGetAllValues() BaseGetAllValues() BaseGetAllValues() BaseGetAllValues()

傳回 Object 陣列,其中包含 NameObjectCollectionBase 執行個體中的所有值。Returns an Object array that contains all the values in the NameObjectCollectionBase instance.

BaseGetAllValues(Type) BaseGetAllValues(Type) BaseGetAllValues(Type) BaseGetAllValues(Type)

傳回指定類型的陣列,其中包含 NameObjectCollectionBase 執行個體中的所有值。Returns an array of the specified type that contains all the values in the NameObjectCollectionBase instance.

BaseGetKey(Int32) BaseGetKey(Int32) BaseGetKey(Int32) BaseGetKey(Int32)

取得 NameObjectCollectionBase 執行個體指定索引處之項目的索引鍵。Gets the key of the entry at the specified index of the NameObjectCollectionBase instance.

BaseHasKeys() BaseHasKeys() BaseHasKeys() BaseHasKeys()

取得值,表示 NameObjectCollectionBase 執行個體是否包含其索引鍵不是 null 的項目。Gets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not null.

BaseRemove(String) BaseRemove(String) BaseRemove(String) BaseRemove(String)

將具有指定索引鍵的項目從 NameObjectCollectionBase 中移除。Removes the entries with the specified key from the NameObjectCollectionBase instance.

BaseRemoveAt(Int32) BaseRemoveAt(Int32) BaseRemoveAt(Int32) BaseRemoveAt(Int32)

移除 NameObjectCollectionBase 執行個體指定索引處的項目。Removes the entry at the specified index of the NameObjectCollectionBase instance.

BaseSet(Int32, Object) BaseSet(Int32, Object) BaseSet(Int32, Object) BaseSet(Int32, Object)

設定 NameObjectCollectionBase 執行個體指定索引處之項目的值。Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.

BaseSet(String, Object) BaseSet(String, Object) BaseSet(String, Object) BaseSet(String, Object)

設定 NameObjectCollectionBase 執行個體中具有指定索引鍵之第一個項目的值 (如果有找到),否則將具有指定索引鍵和值的項目加入 NameObjectCollectionBase 執行個體。Sets the value of the first entry with the specified key in the NameObjectCollectionBase instance, if found; otherwise, adds an entry with the specified key and value into the NameObjectCollectionBase instance.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

傳回在 NameObjectCollectionBase 中逐一查看的列舉值。Returns an enumerator that iterates through the NameObjectCollectionBase.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

實作 ISerializable 介面,並傳回序列化 NameObjectCollectionBase 執行個體所需的資料。Implements the ISerializable interface and returns the data needed to serialize the NameObjectCollectionBase instance.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
OnDeserialization(Object) OnDeserialization(Object) OnDeserialization(Object) OnDeserialization(Object)

實作 ISerializable 介面,並於還原序列化完成時引發還原序列化事件。Implements the ISerializable interface and raises the deserialization event when the deserialization is complete.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

從目標陣列的指定索引開始,將整個 NameObjectCollectionBase 複製到相容的一維 ArrayCopies the entire NameObjectCollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

取得值,表示是否要同步處理 (執行緒安全) 對 NameObjectCollectionBase 物件的存取。Gets a value indicating whether access to the NameObjectCollectionBase object is synchronized (thread safe).

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

取得可用來同步處理對 NameObjectCollectionBase 物件之存取的物件。Gets an object that can be used to synchronize access to the NameObjectCollectionBase object.

擴充方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

執行緒安全性

此類型的Shared公用靜態(在 Visual Basic 中)成員是安全線程。Public static (Shared in Visual Basic) members of this type are thread safe. 不保證任何執行個體成員是安全執行緒。Any instance members are not guaranteed to be thread safe.

這個執行並不提供的同步處理(安全線程)包裝NameObjectCollectionBase函式,但衍生的類別可以使用SyncRoot屬性,建立自己NameObjectCollectionBase的同步處理版本。This implementation does not provide a synchronized (thread safe) wrapper for a NameObjectCollectionBase, but derived classes can create their own synchronized versions of the NameObjectCollectionBase using the SyncRoot property.

透過集合進行列舉,本質上並不是安全線程的程式。Enumerating through a collection is intrinsically not a thread safe procedure. 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

另請參閱