NameValueCollection クラス
アセンブリ: System (system.dll 内)
構文
このコレクションは NameObjectCollectionBase クラスに基づいています。ただし、NameObjectCollectionBase とは異なり、このクラスには 1 つのキーに複数の文字列値を格納します。
このクラスは、ヘッダー、クエリ文字列、およびフォーム データに使用できます。
NameValueCollection の容量は、NameValueCollection が保持できる要素数になります。NameValueCollection の既定の初期量はゼロです。NameValueCollection に要素を追加すると、必要に応じて、再割り当てを行うことによって容量が自動的に増加します。
ハッシュ コード プロバイダは、NameValueCollection 内のキーにハッシュ コードを提供します。既定のハッシュ コード プロバイダは CaseInsensitiveHashCodeProvider です。
比較演算子は 2 つのキーが等しいかどうかを判断します。既定の比較演算子は CaseInsensitiveComparer です。
.NET Framework Version 1.0 の場合、このクラスはカルチャに依存した文字列比較を使用します。ただし、.NET Framework Version 1.1 以降の場合、このクラスは文字列を比較するときに CultureInfo.InvariantCulture を使用します。カルチャが比較と並べ替えに与える影響の詳細については、「固有カルチャのデータの比較と並べ替え固有カルチャのデータの比較と並べ替え」および「カルチャを認識しない文字列操作の実行」を参照してください。
キーまたは値として null 参照 (Visual Basic では Nothing) を使用できます。
注意 |
---|
Get メソッドでは、指定したキーが見つからないために返される null 参照 (Visual Basic では Nothing) と、キーに関連付けられている値が null 参照 (Visual Basic では Nothing) であるために返される null 参照 (Visual Basic では Nothing) とが区別されません。 |
' The following code example demonstrates several of the properties and methods of ListDictionary. Imports System 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 'Main Public Shared Sub PrintKeysAndValues(myCol As NameValueCollection) Dim myEnumerator As IEnumerator = myCol.GetEnumerator() 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 'PrintKeysAndValues 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 'PrintKeysAndValues2 End Class 'SamplesNameValueCollection '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 ' '
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 ) { IEnumerator myEnumerator = myCol.GetEnumerator(); 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 */
#using <System.dll> using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void PrintKeysAndValues( NameValueCollection^ myCol ); void PrintKeysAndValues2( NameValueCollection^ myCol ); int main() { // Creates and initializes a new NameValueCollection. NameValueCollection^ myCol = gcnew 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. array<String^>^myStrArr = gcnew array<String^>(myCol->Count); myCol->CopyTo( myStrArr, 0 ); Console::WriteLine( "The string array contains:" ); for each ( 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 ); } void PrintKeysAndValues( NameValueCollection^ myCol ) { Console::WriteLine( " KEY VALUE" ); for each ( String^ s in myCol->AllKeys ) Console::WriteLine( " {0,-10} {1}", s, myCol[s] ); Console::WriteLine(); } 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 */
import System.*; import System.Collections.*; import System.Collections.Specialized.*; public class SamplesNameValueCollection { public static void main(String[] args) { // 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.get_Item(1)); Console.WriteLine("Key \"red\" has the value {0}.", myCol.get_Item("red")); Console.WriteLine(); // Copies the values to a string array and displays the string array. String myStrArr[] = new String[myCol.get_Count()]; myCol.CopyTo(myStrArr, 0); Console.WriteLine("The string array contains:"); for (int iCtr = 0; iCtr < myStrArr.get_Length(); iCtr++) { String s = myStrArr[iCtr]; 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); } //main public static void PrintKeysAndValues(NameValueCollection myCol) { IEnumerator myEnumerator = myCol.GetEnumerator(); Console.WriteLine(" KEY VALUE"); for (int iCtr = 0; iCtr < myCol.get_Count(); iCtr++) { String s = myCol.get_AllKeys()[iCtr]; Console.WriteLine(" {0,-10} {1}", s, myCol.get_Item(s)); } Console.WriteLine(); } //PrintKeysAndValues public static void PrintKeysAndValues2(NameValueCollection myCol) { Console.WriteLine(" [INDEX] KEY VALUE"); for (int i = 0; i < myCol.get_Count(); i++) { Console.WriteLine(" [{0}] {1,-10} {2}", System.Convert.ToString(i), System.Convert.ToString( myCol.GetKey(i)), System.Convert.ToString(myCol.Get(i))); } Console.WriteLine(); } //PrintKeysAndValues2 } //SamplesNameValueCollection /* 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 */
System.Collections.Specialized.NameObjectCollectionBase
System.Collections.Specialized.NameValueCollection
System.Net.WebHeaderCollection
System.Web.HttpClientCertificate
この型の public static (Visual Basic では Shared) メンバは、スレッド セーフです。すべてのインスタンス メンバがスレッド セーフになるかどうかは保証されていません。
この実装は、NameValueCollection 用の同期された (スレッド セーフな) ラッパーは提供しませんが、派生クラスでは、NameObjectCollectionBase クラスの SyncRoot プロパティを使用して、同期した NameValueCollection を独自に作成できます。
コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Weblioに収録されているすべての辞書からNameValueCollection クラスを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からNameValueCollection クラスを検索
- NameValueCollection クラスのページへのリンク