NameValueCollection クラス

定義

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

public ref class NameValueCollection : System::Collections::Specialized::NameObjectCollectionBase
public class NameValueCollection : System.Collections.Specialized.NameObjectCollectionBase
[System.Serializable]
public class NameValueCollection : System.Collections.Specialized.NameObjectCollectionBase
type NameValueCollection = class
    inherit NameObjectCollectionBase
[<System.Serializable>]
type NameValueCollection = class
    inherit NameObjectCollectionBase
Public Class NameValueCollection
Inherits NameObjectCollectionBase
継承
NameValueCollection
派生
属性

using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesNameValueCollection  {

   public static void Main()  {

      // Creates and initializes a new NameValueCollection.
      NameValueCollection myCol = new NameValueCollection();
      myCol.Add( "red", "rojo" );
      myCol.Add( "green", "verde" );
      myCol.Add( "blue", "azul" );
      myCol.Add( "red", "rouge" );

      // Displays the values in the NameValueCollection in two different ways.
      Console.WriteLine( "Displays the elements using the AllKeys property and the Item (indexer) property:" );
      PrintKeysAndValues( myCol );
      Console.WriteLine( "Displays the elements using GetKey and Get:" );
      PrintKeysAndValues2( myCol );

      // Gets a value either by index or by key.
      Console.WriteLine( "Index 1 contains the value {0}.", myCol[1] );
      Console.WriteLine( "Key \"red\" has the value {0}.", myCol["red"] );
      Console.WriteLine();

      // Copies the values to a string array and displays the string array.
      String[] myStrArr = new String[myCol.Count];
      myCol.CopyTo( myStrArr, 0 );
      Console.WriteLine( "The string array contains:" );
      foreach ( String s in myStrArr )
         Console.WriteLine( "   {0}", s );
      Console.WriteLine();

      // Searches for a key and deletes it.
      myCol.Remove( "green" );
      Console.WriteLine( "The collection contains the following elements after removing \"green\":" );
      PrintKeysAndValues( myCol );

      // Clears the entire collection.
      myCol.Clear();
      Console.WriteLine( "The collection contains the following elements after it is cleared:" );
      PrintKeysAndValues( myCol );
   }

   public static void PrintKeysAndValues( NameValueCollection myCol )  {
      Console.WriteLine( "   KEY        VALUE" );
      foreach ( String s in myCol.AllKeys )
         Console.WriteLine( "   {0,-10} {1}", s, myCol[s] );
      Console.WriteLine();
   }

   public static void PrintKeysAndValues2( NameValueCollection myCol )  {
      Console.WriteLine( "   [INDEX] KEY        VALUE" );
      for ( int i = 0; i < myCol.Count; i++ )
         Console.WriteLine( "   [{0}]     {1,-10} {2}", i, myCol.GetKey(i), myCol.Get(i) );
      Console.WriteLine();
   }
}

/*

This code produces the following output.

Displays the elements using the AllKeys property and the Item (indexer) property:
   KEY        VALUE
   red        rojo,rouge
   green      verde
   blue       azul

Displays the elements using GetKey and Get:
   [INDEX] KEY        VALUE
   [0]     red        rojo,rouge
   [1]     green      verde
   [2]     blue       azul

Index 1 contains the value verde.
Key "red" has the value rojo,rouge.

The string array contains:
   rojo,rouge
   verde
   azul

The collection contains the following elements after removing "green":
   KEY        VALUE
   red        rojo,rouge
   blue       azul

The collection contains the following elements after it is cleared:
   KEY        VALUE


*/
' The following code example demonstrates several of the properties and methods of ListDictionary.

Imports System.Collections
Imports System.Collections.Specialized


Public Class SamplesNameValueCollection

    Public Shared Sub Main()

        ' Creates and initializes a new NameValueCollection.
        Dim myCol As New NameValueCollection()
        myCol.Add("red", "rojo")
        myCol.Add("green", "verde")
        myCol.Add("blue", "azul")
        myCol.Add("red", "rouge")

        ' Displays the values in the NameValueCollection in two different ways.
        Console.WriteLine("Displays the elements using the AllKeys property and the Item (indexer) property:")
        PrintKeysAndValues(myCol)
        Console.WriteLine("Displays the elements using GetKey and Get:")
        PrintKeysAndValues2(myCol)

        ' Gets a value either by index or by key.
        Console.WriteLine("Index 1 contains the value {0}.", myCol(1))
        Console.WriteLine("Key ""red"" has the value {0}.", myCol("red"))
        Console.WriteLine()

        ' Copies the values to a string array and displays the string array.
        Dim myStrArr(myCol.Count) As String
        myCol.CopyTo(myStrArr, 0)
        Console.WriteLine("The string array contains:")
        Dim s As String
        For Each s In myStrArr
            Console.WriteLine("   {0}", s)
        Next s
        Console.WriteLine()

        ' Searches for a key and deletes it.
        myCol.Remove("green")
        Console.WriteLine("The collection contains the following elements after removing ""green"":")
        PrintKeysAndValues(myCol)

        ' Clears the entire collection.
        myCol.Clear()
        Console.WriteLine("The collection contains the following elements after it is cleared:")
        PrintKeysAndValues(myCol)

    End Sub

    Public Shared Sub PrintKeysAndValues(myCol As NameValueCollection)
        Console.WriteLine("   KEY        VALUE")
        Dim s As String
        For Each s In  myCol.AllKeys
            Console.WriteLine("   {0,-10} {1}", s, myCol(s))
        Next s
        Console.WriteLine()
    End Sub

    Public Shared Sub PrintKeysAndValues2(myCol As NameValueCollection)
        Console.WriteLine("   [INDEX] KEY        VALUE")
        Dim i As Integer
        For i = 0 To myCol.Count - 1
            Console.WriteLine("   [{0}]     {1,-10} {2}", i, myCol.GetKey(i), myCol.Get(i))
        Next i
        Console.WriteLine()
    End Sub

End Class


'This code produces the following output.
'
'Displays the elements using the AllKeys property and the Item (indexer) property:
'   KEY        VALUE
'   red        rojo,rouge
'   green      verde
'   blue       azul
'
'Displays the elements using GetKey and Get:
'   [INDEX] KEY        VALUE
'   [0]     red        rojo,rouge
'   [1]     green      verde
'   [2]     blue       azul
'
'Index 1 contains the value verde.
'Key "red" has the value rojo,rouge.
'
'The string array contains:
'   red
'   green
'   blue
'
'
'The collection contains the following elements after removing "green":
'   KEY        VALUE
'   red        rojo,rouge
'   blue       azul
'
'The collection contains the following elements after it is cleared:
'   KEY        VALUE
'
'

注釈

このコレクションは、 NameObjectCollectionBase クラスに基づいています。 コレクションの各要素は、キーと値のペアです。 ただし、 NameObjectCollectionBaseとは異なり、このクラスは 1 つのキーの下に複数の文字列値を格納できます。

このクラスは、ヘッダー、クエリ文字列、フォーム データに使用できます。

この型のコレクションは要素の順序を保持せず、コレクションを列挙するときに特定の順序は保証されません。

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

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

比較子は、2 つのキーが等しいかどうかを判断します。 既定の比較子は、CaseInsensitiveComparerの規則を使用するです。つまり、キー比較では、既定では大文字と小文字が区別されません。 大文字と小文字を区別するキーの比較を実行するには、NameValueCollection.NameValueCollection(IEqualityComparer) コンストラクターを呼び出し、StringComparer.CurrentCulture引数としてStringComparer.InvariantCultureStringComparer.Ordinal、またはequalityComparerの値を指定します。 カルチャが比較と並べ替えに与える影響の詳細については、「 Culture-Insensitive 文字列操作の実行」を参照してください。

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

Caution

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

コンストラクター

名前 説明
NameValueCollection()

空の NameValueCollection クラスの新しいインスタンスを初期化し、既定の初期容量を持ち、既定の大文字と小文字を区別しないハッシュ コード プロバイダーと既定の大文字と小文字を区別しない比較子を使用します。

NameValueCollection(IEqualityComparer)

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

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

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

NameValueCollection(Int32, IEqualityComparer)

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

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

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

NameValueCollection(Int32, NameValueCollection)

指定した NameValueCollection のエントリを新しい NameValueCollection にコピーします。初期容量は、コピーされたエントリの数のいずれか大きい方と同じで、既定の大文字と小文字を区別しないハッシュ コード プロバイダーと既定の大文字と小文字を区別しない比較子を使用します。

NameValueCollection(Int32)

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

NameValueCollection(NameValueCollection)

コピーされたエントリの数と同じ初期容量を持ち、ソース コレクションと同じハッシュ コード プロバイダーと同じ比較子を使用して、指定した NameValueCollection から新しい NameValueCollection にエントリをコピーします。

NameValueCollection(SerializationInfo, StreamingContext)
古い.

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

プロパティ

名前 説明
AllKeys

NameValueCollection内のすべてのキーを取得します。

Count

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

(継承元 NameObjectCollectionBase)
IsReadOnly

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

(継承元 NameObjectCollectionBase)
Item[Int32]

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

Item[String]

NameValueCollectionで指定したキーを持つエントリを取得または設定します。

Keys

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

(継承元 NameObjectCollectionBase)

メソッド

名前 説明
Add(NameValueCollection)

指定した NameValueCollection のエントリを現在の NameValueCollectionにコピーします。

Add(String, String)

指定した名前と値を持つエントリを NameValueCollectionに追加します。

BaseAdd(String, Object)

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

(継承元 NameObjectCollectionBase)
BaseClear()

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

(継承元 NameObjectCollectionBase)
BaseGet(Int32)

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

(継承元 NameObjectCollectionBase)
BaseGet(String)

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

(継承元 NameObjectCollectionBase)
BaseGetAllKeys()

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

(継承元 NameObjectCollectionBase)
BaseGetAllValues()

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

(継承元 NameObjectCollectionBase)
BaseGetAllValues(Type)

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

(継承元 NameObjectCollectionBase)
BaseGetKey(Int32)

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

(継承元 NameObjectCollectionBase)
BaseHasKeys()

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

(継承元 NameObjectCollectionBase)
BaseRemove(String)

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

(継承元 NameObjectCollectionBase)
BaseRemoveAt(Int32)

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

(継承元 NameObjectCollectionBase)
BaseSet(Int32, Object)

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

(継承元 NameObjectCollectionBase)
BaseSet(String, Object)

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

(継承元 NameObjectCollectionBase)
Clear()

キャッシュされた配列を無効にし、 NameValueCollectionからすべてのエントリを削除します。

CopyTo(Array, Int32)

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

Equals(Object)

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

(継承元 Object)
Get(Int32)

1 つのコンマ区切りリストに結合された NameValueCollection の指定したインデックス位置にある値を取得します。

Get(String)

1 つのコンマ区切りリストに結合された NameValueCollection から、指定したキーに関連付けられている値を取得します。

GetEnumerator()

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

(継承元 NameObjectCollectionBase)
GetHashCode()

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

(継承元 Object)
GetKey(Int32)

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

GetObjectData(SerializationInfo, StreamingContext)
古い.

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

(継承元 NameObjectCollectionBase)
GetType()

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

(継承元 Object)
GetValues(Int32)

NameValueCollectionの指定したインデックス位置にある値を取得します。

GetValues(String)

指定したキーに関連付けられている値を NameValueCollectionから取得します。

HasKeys()

NameValueCollectionnullされていないキーが含まれているかどうかを示す値を取得します。

InvalidateCachedArrays()

コレクションのキャッシュされた配列を nullにリセットします。

MemberwiseClone()

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

(継承元 Object)
OnDeserialization(Object)

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

(継承元 NameObjectCollectionBase)
Remove(String)

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

Set(String, String)

NameValueCollection内のエントリの値を設定します。

ToString()

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

(継承元 Object)

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

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

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

(継承元 NameObjectCollectionBase)
ICollection.IsSynchronized

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

(継承元 NameObjectCollectionBase)
ICollection.SyncRoot

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

(継承元 NameObjectCollectionBase)

拡張メソッド

名前 説明
AsParallel(IEnumerable)

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

AsQueryable(IEnumerable)

IEnumerableIQueryableに変換します。

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

適用対象

スレッド セーフ

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

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

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

こちらもご覧ください