NameObjectCollectionBase クラス

定義

キーまたはインデックスを使用してアクセスできる、関連付けられたabstract キーとString値のコレクションのObject基底クラスを提供します。

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
継承
NameObjectCollectionBase
派生
属性
実装

次のコード例は、 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):

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

注釈

このクラスの基になる構造体はハッシュ テーブルです。

各要素はキーと値のペアです。

NameObjectCollectionBaseの容量は、NameObjectCollectionBaseが保持できる要素の数です。 要素が NameObjectCollectionBaseに追加されると、再割り当てによって必要に応じて容量が自動的に増加します。

ハッシュ コード プロバイダーは、 NameObjectCollectionBase インスタンス内のキーのハッシュ コードを分配します。 既定のハッシュ コード プロバイダーは CaseInsensitiveHashCodeProviderです。

比較子は、2 つのキーが等しいかどうかを判断します。 既定の比較子は CaseInsensitiveComparerです。

.NET Framework バージョン 1.0 では、このクラスはカルチャに依存する文字列比較を使用します。 ただし、.NET Framework バージョン 1.1 以降では、このクラスは文字列を比較するときに CultureInfo.InvariantCulture を使用します。 カルチャが比較と並べ替えに与える影響の詳細については、「 Culture-Insensitive 文字列操作の実行」を参照してください。

null はキーまたは値として使用できます。

Caution

BaseGet メソッドは、指定したキーが見つからないために返されるnullと、キーに関連付けられた値がnullされているために返されるnullを区別しません。

コンストラクター

名前 説明
NameObjectCollectionBase()

空の NameObjectCollectionBase クラスの新しいインスタンスを初期化します。

NameObjectCollectionBase(IEqualityComparer)

空の NameObjectCollectionBase クラスの新しいインスタンスを初期化し、既定の初期容量を持ち、指定した IEqualityComparer オブジェクトを使用します。

NameObjectCollectionBase(IHashCodeProvider, IComparer)
古い.

空で、既定の初期容量を持ち、指定したハッシュ コード プロバイダーと指定した比較子を使用する、 NameObjectCollectionBase クラスの新しいインスタンスを初期化します。

NameObjectCollectionBase(Int32, IEqualityComparer)

空の NameObjectCollectionBase クラスの新しいインスタンスを初期化し、指定した初期容量を持ち、指定した IEqualityComparer オブジェクトを使用します。

NameObjectCollectionBase(Int32, IHashCodeProvider, IComparer)
古い.

空の NameObjectCollectionBase クラスの新しいインスタンスを初期化し、指定した初期容量を持ち、指定したハッシュ コード プロバイダーと指定された比較子を使用します。

NameObjectCollectionBase(Int32)

空の NameObjectCollectionBase クラスの新しいインスタンスを初期化し、指定された初期容量を持ち、既定のハッシュ コード プロバイダーと既定の比較子を使用します。

NameObjectCollectionBase(SerializationInfo, StreamingContext)

シリアル化可能で、指定したSerializationInfoStreamingContextを使用するNameObjectCollectionBase クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
Count

NameObjectCollectionBase インスタンスに含まれるキーと値のペアの数を取得します。

IsReadOnly

NameObjectCollectionBase インスタンスが読み取り専用かどうかを示す値を取得または設定します。

Keys

NameObjectCollectionBase.KeysCollection インスタンス内のすべてのキーを含むNameObjectCollectionBase インスタンスを取得します。

メソッド

名前 説明
BaseAdd(String, Object)

指定したキーと値を持つエントリを NameObjectCollectionBase インスタンスに追加します。

BaseClear()

NameObjectCollectionBase インスタンスからすべてのエントリを削除します。

BaseGet(Int32)

NameObjectCollectionBase インスタンスの指定したインデックス位置にあるエントリの値を取得します。

BaseGet(String)

NameObjectCollectionBase インスタンスから、指定したキーを持つ最初のエントリの値を取得します。

BaseGetAllKeys()

String インスタンス内のすべてのキーを含むNameObjectCollectionBase配列を返します。

BaseGetAllValues()

Object インスタンス内のすべての値を含むNameObjectCollectionBase配列を返します。

BaseGetAllValues(Type)

NameObjectCollectionBase インスタンス内のすべての値を含む、指定した型の配列を返します。

BaseGetKey(Int32)

NameObjectCollectionBase インスタンスの指定したインデックス位置にあるエントリのキーを取得します。

BaseHasKeys()

キーがNameObjectCollectionBaseされていないエントリがnull インスタンスに含まれているかどうかを示す値を取得します。

BaseRemove(String)

指定したキーを持つエントリを NameObjectCollectionBase インスタンスから削除します。

BaseRemoveAt(Int32)

NameObjectCollectionBase インスタンスの指定したインデックス位置にあるエントリを削除します。

BaseSet(Int32, Object)

NameObjectCollectionBase インスタンスの指定したインデックス位置にあるエントリの値を設定します。

BaseSet(String, Object)

見つかった場合は、 NameObjectCollectionBase インスタンス内の指定したキーを持つ最初のエントリの値を設定します。それ以外の場合は、指定したキーと値を持つエントリを NameObjectCollectionBase インスタンスに追加します。

Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetEnumerator()

NameObjectCollectionBaseを反復処理する列挙子を返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetObjectData(SerializationInfo, StreamingContext)

ISerializable インターフェイスを実装し、NameObjectCollectionBase インスタンスのシリアル化に必要なデータを返します。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
OnDeserialization(Object)

ISerializable インターフェイスを実装し、逆シリアル化が完了したときに逆シリアル化イベントを発生させます。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

名前 説明
ICollection.CopyTo(Array, Int32)

ターゲット配列の指定したインデックスから始まる互換性のある 1 次元NameObjectCollectionBaseに、Array全体をコピーします。

ICollection.IsSynchronized

NameObjectCollectionBase オブジェクトへのアクセスが同期されているかどうかを示す値を取得します (スレッド セーフ)。

ICollection.SyncRoot

NameObjectCollectionBase オブジェクトへのアクセスを同期するために使用できるオブジェクトを取得します。

拡張メソッド

名前 説明
AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryableに変換します。

Cast<TResult>(IEnumerable)

IEnumerable の要素を指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定した型に基づいて、IEnumerable の要素をフィルター処理します。

適用対象

スレッド セーフ

この型のパブリック静的 (Visual Basic のShared ) メンバーはスレッド セーフです。 インスタンス メンバーがスレッド セーフであるとは限りません。

この実装では、NameObjectCollectionBaseの同期 (スレッド セーフ) ラッパーは提供されませんが、派生クラスでは、NameObjectCollectionBase プロパティを使用して、独自の同期バージョンのSyncRootを作成できます。

コレクションを通じて列挙することは、本質的にスレッド セーフなプロシージャではありません。 コレクションが同期されている場合でも、他のスレッドはコレクションを変更できるため、列挙子は例外をスローします。 列挙中のスレッド セーフを保証するには、列挙全体の間にコレクションをロックするか、他のスレッドによって行われた変更によって発生する例外をキャッチします。

こちらもご覧ください