为关联的 String 键和 Object 值的集合(可通过键或索引来访问它)提供 abstract 基类。
命名空间:System.Collections.Specialized
程序集:System(在 system.dll 中)
语法:
[SerializableAttribute] public abstract class NameObjectCollectionBase : ICollection, IEnumerable, ISerializable, IDeserializationCallback
此类的基础结构是哈希表。
每个元素都是一个键/值对。
NameObjectCollectionBase 的容量是 NameObjectCollectionBase 可以保存的元素数。NameObjectCollectionBase 的默认初始容量为零。随着向 NameObjectCollectionBase 中添加元素,容量通过重新分配按需自动增加。
哈希代码提供程序为 NameObjectCollectionBase 实例中的键分配哈希代码。默认的哈希代码提供程序是 CaseInsensitiveHashCodeProvider。
比较器确定两个键是否相等。默认比较器是 CaseInsensitiveComparer。
在 .NET Framework 1.0 版中,此类使用区分区域性资源的字符串比较。但在 .NET Framework 1.1 版及更高版本中,此类在比较字符串时使用 CultureInfo.InvariantCulture。有关区域性如何影响比较和排序的更多信息,请参见 针对特定区域性对数据进行比较和排序针对特定区域性对数据进行比较和排序 和 执行不区分区域性的字符串操作。
空引用(在 Visual Basic 中为 Nothing) 可以是键或值。
BaseGet 方法在由于未找到指定键而返回的 空引用(在 Visual Basic 中为 Nothing) 和由于与键关联的值为 空引用(在 Visual Basic 中为 Nothing) 而返回的 空引用(在 Visual Basic 中为 Nothing) 之间没有区别。
下面的代码示例演示如何实现和使用 NameObjectCollectionBase 类
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): */
System.Object
System.Collections.Specialized.NameObjectCollectionBase
派生类
此类型的公共静态(在 Visual Basic 中为 Shared)成员是线程安全的。但不能保证任何实例成员是线程安全的。
此实现不为 NameObjectCollectionBase 提供同步(线程安全)包装,但派生类可使用 SyncRoot 属性创建它们各自的 NameObjectCollectionBase 同步版本。
从头到尾对一个集合进行枚举本质上并不是一个线程安全的过程。即使一个集合已进行同步,其他线程仍可以修改该集合,这将导致枚举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。
转载于:https://blog.51cto.com/jiaojusuimu/1882347