NameValueCollection クラスとは? わかりやすく解説

NameValueCollection クラス

関連付けられた String キーおよび String 値のコレクション表します。これらのキーおよび値には、キーまたはインデックスいずれか使用してアクセスできます

名前空間: System.Collections.Specialized
アセンブリ: System (system.dll 内)
構文構文

<SerializableAttribute> _
Public Class NameValueCollection
    Inherits NameObjectCollectionBase
Dim instance As NameValueCollection
[SerializableAttribute] 
public class NameValueCollection : NameObjectCollectionBase
[SerializableAttribute] 
public ref class NameValueCollection : public
 NameObjectCollectionBase
/** @attribute SerializableAttribute() */ 
public class NameValueCollection extends NameObjectCollectionBase
SerializableAttribute 
public class NameValueCollection extends
 NameObjectCollectionBase
解説解説

このコレクション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.Object
   System.Collections.Specialized.NameObjectCollectionBase
    System.Collections.Specialized.NameValueCollection
       System.Net.WebHeaderCollection
       System.Web.HttpClientCertificate
スレッド セーフスレッド セーフ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
NameValueCollection メンバ
System.Collections.Specialized 名前空間
NameObjectCollectionBase クラス
その他の技術情報
カルチャを認識しい文字操作実行


このページでは「.NET Framework クラス ライブラリ リファレンス」からNameValueCollection クラスを検索した結果を表示しています。
Weblioに収録されているすべての辞書からNameValueCollection クラスを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からNameValueCollection クラスを検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「NameValueCollection クラス」の関連用語

NameValueCollection クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



NameValueCollection クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS