NameObjectCollectionBase クラス

定義

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

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
[<System.Serializable>]
type NameObjectCollectionBase = class
    interface ICollection
    interface ISerializable
    interface IDeserializationCallback
    interface IEnumerable
Public MustInherit Class NameObjectCollectionBase
Implements ICollection
Public MustInherit Class NameObjectCollectionBase
Implements ICollection, IDeserializationCallback, ISerializable
継承
NameObjectCollectionBase
派生
属性
実装

次のコード例は、 クラスを実装して使用する方法を NameObjectCollectionBase 示しています。

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

注釈

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

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

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

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

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

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

キーまたは値としてnullを許可します。

注意事項

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

コンストラクター

NameObjectCollectionBase()

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

NameObjectCollectionBase(IEqualityComparer)

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

NameObjectCollectionBase(IHashCodeProvider, IComparer)
古い.
古い.

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

NameObjectCollectionBase(Int32)

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

NameObjectCollectionBase(Int32, IEqualityComparer)

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

NameObjectCollectionBase(Int32, IHashCodeProvider, IComparer)
古い.
古い.

空で、指定した初期量を備え、指定したハッシュ コード プロバイダーと指定した比較子を使用する、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()

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

BaseGetAllValues()

NameObjectCollectionBase インスタンス内のすべての値を格納する Object 配列を返します。

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)

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

ICollection.IsSynchronized

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

ICollection.SyncRoot

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

拡張メソッド

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

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

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象

スレッド セーフ

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

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

コレクションの列挙は、本質的にスレッド セーフな手続きではありません。 コレクションが同期されていても、他のスレッドがコレクションを変更する場合があり、このときは列挙子から例外がスローされます。 列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。

こちらもご覧ください