NameValueCollection クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
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
- 継承
- 派生
- 属性
例
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.InvariantCulture、StringComparer.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) |
古い.
シリアル化可能で、指定したNameValueCollectionとSerializationInfoを使用するStreamingContext クラスの新しいインスタンスを初期化します。 |
プロパティ
| 名前 | 説明 |
|---|---|
| AllKeys |
NameValueCollection内のすべてのキーを取得します。 |
| Count |
NameObjectCollectionBase インスタンスに含まれるキーと値のペアの数を取得します。 (継承元 NameObjectCollectionBase) |
| IsReadOnly |
NameObjectCollectionBase インスタンスが読み取り専用かどうかを示す値を取得または設定します。 (継承元 NameObjectCollectionBase) |
| Item[Int32] |
NameValueCollectionの指定したインデックス位置にあるエントリを取得します。 |
| Item[String] |
NameValueCollectionで指定したキーを持つエントリを取得または設定します。 |
| Keys |
NameObjectCollectionBase.KeysCollection インスタンス内のすべてのキーを含むNameObjectCollectionBase インスタンスを取得します。 (継承元 NameObjectCollectionBase) |
メソッド
明示的なインターフェイスの実装
| 名前 | 説明 |
|---|---|
| ICollection.CopyTo(Array, Int32) |
ターゲット配列の指定したインデックスから始まる互換性のある 1 次元NameObjectCollectionBaseに、Array全体をコピーします。 (継承元 NameObjectCollectionBase) |
| ICollection.IsSynchronized |
NameObjectCollectionBase オブジェクトへのアクセスが同期されているかどうかを示す値を取得します (スレッド セーフ)。 (継承元 NameObjectCollectionBase) |
| ICollection.SyncRoot |
NameObjectCollectionBase オブジェクトへのアクセスを同期するために使用できるオブジェクトを取得します。 (継承元 NameObjectCollectionBase) |
拡張メソッド
| 名前 | 説明 |
|---|---|
| AsParallel(IEnumerable) |
クエリの並列化を有効にします。 |
| AsQueryable(IEnumerable) |
IEnumerable を IQueryableに変換します。 |
| Cast<TResult>(IEnumerable) |
IEnumerable の要素を指定した型にキャストします。 |
| OfType<TResult>(IEnumerable) |
指定した型に基づいて、IEnumerable の要素をフィルター処理します。 |
適用対象
スレッド セーフ
この型のパブリック静的 (Visual Basic のShared ) メンバーはスレッド セーフです。 インスタンス メンバーがスレッド セーフであるとは限りません。
この実装では、NameValueCollectionの同期 (スレッド セーフ) ラッパーは提供されませんが、派生クラスは、NameValueCollection クラスの SyncRoot プロパティを使用して、独自の同期バージョンのNameObjectCollectionBaseを作成できます。
コレクションを通じて列挙することは、本質的にスレッド セーフなプロシージャではありません。 コレクションが同期されている場合でも、他のスレッドはコレクションを変更できるため、列挙子は例外をスローします。 列挙中のスレッド セーフを保証するには、列挙全体の間にコレクションをロックするか、他のスレッドによって行われた変更によって発生する例外をキャッチします。