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.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


*/
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단일 키 아래에 여러 문자열 값을 저장할 수 있습니다.

이 클래스는 헤더, 쿼리 문자열 및 양식 데이터에 사용할 수 있습니다.

이 형식의 컬렉션은 요소의 순서를 유지하지 않으며 컬렉션을 열거할 때 특정 순서가 보장되지 않습니다.

NameValueCollection 용량은 가 보유할 수 있는 NameValueCollection 요소의 수입니다. 요소가 추가되면 재할당을 통해 필요에 따라 해당 용량이 자동으로 증가합니다.

해시 코드 공급자는 의 키 NameValueCollection에 대한 해시 코드를 분배합니다. 기본 해시 코드 공급자는 입니다 CaseInsensitiveHashCodeProvider.

비교자는 두 키가 같은지 여부를 결정합니다. 기본 비교자는 고정 문화권의 규칙을 사용하는 입니다CaseInsensitiveComparer. 즉, 키 비교는 기본적으로 대/소문자를 구분하지 않습니다. 대/소문자를 구분하는 키 비교를 수행하려면 생성자를 호출 NameValueCollection.NameValueCollection(IEqualityComparer) 하고 , StringComparer.InvariantCulture또는 StringComparer.Ordinal 값을 StringComparer.CurrentCulture인수로 equalityComparer 제공합니다. 문화권이 비교 및 정렬에 미치는 영향에 대한 자세한 내용은 Culture-Insensitive 문자열 작업 수행을 참조하세요.

null 는 키 또는 값으로 허용됩니다.

주의

Get 지정된 키를 찾을 수 없기 때문에 반환되는 와 키와 null 연결된 값이 이므로 반환되는 null값을 구분 null 하지 않습니다.

생성자

NameValueCollection()

비어 있는 상태이고 기본 초기 용량을 가지며 대/소문자를 구분하지 않는 기본 해시 코드 공급자와 대/소문자를 구분하지 않는 기본 비교자를 사용하는 NameValueCollection 클래스의 새 인스턴스를 초기화합니다.

NameValueCollection(IEqualityComparer)

기본 초기 용량을 갖고 있고 지정된 NameValueCollection 개체를 사용하는 비어 있는 IEqualityComparer 클래스의 새 인스턴스를 초기화합니다.

NameValueCollection(IHashCodeProvider, IComparer)
사용되지 않음.
사용되지 않음.

비어 있는 상태이고 기본 초기 용량을 가지며 지정된 해시 코드 공급자와 지정된 비교자를 사용하는 NameValueCollection 클래스의 새 인스턴스를 초기화합니다.

NameValueCollection(Int32)

비어 있는 상태이고 지정한 초기 용량을 가지며 대/소문자를 구분하지 않는 기본 해시 코드 공급자와 대/소문자를 구분하지 않는 기본 비교자를 사용하는 NameValueCollection 클래스의 새 인스턴스를 초기화합니다.

NameValueCollection(Int32, IEqualityComparer)

지정된 초기 용량을 갖고 있고 지정된 NameValueCollection 개체를 사용하는 비어 있는 IEqualityComparer 클래스의 새 인스턴스를 초기화합니다.

NameValueCollection(Int32, IHashCodeProvider, IComparer)
사용되지 않음.
사용되지 않음.

비어 있는 상태이고 지정된 초기 용량을 가지며 지정된 해시 코드 공급자와 지정된 비교자를 사용하는 NameValueCollection 클래스의 새 인스턴스를 초기화합니다.

NameValueCollection(Int32, NameValueCollection)

지정된 NameValueCollection의 엔트리를 새 NameValueCollection에 복사합니다. 이 컬렉션은 지정된 초기 용량을 가지거나 복사되는 엔트리의 수와 같은 초기 용량을 가지며 대/소문자를 구분하지 않는 기본 해시 코드 공급자와 대/소문자를 구분하지 않는 기본 비교자를 사용합니다.

NameValueCollection(NameValueCollection)

지정된 NameValueCollection의 엔트리를 초기 용량이 복사되는 엔트리의 수와 같고 소스 컬렉션과 같은 해시 코드 공급자 및 같은 비교자를 사용하는 새 NameValueCollection에 복사합니다.

NameValueCollection(SerializationInfo, StreamingContext)
사용되지 않음.

직렬화할 수 있으며 지정된 SerializationInfoStreamingContext를 사용하는 NameValueCollection 클래스의 새 인스턴스를 초기화합니다.

속성

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()

NameObjectCollectionBase 인스턴스의 모든 키를 포함하는 String 배열을 반환합니다.

(다음에서 상속됨 NameObjectCollectionBase)
BaseGetAllValues()

NameObjectCollectionBase 인스턴스의 모든 값을 포함하는 Object 배열을 반환합니다.

(다음에서 상속됨 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)

대상 배열의 지정된 인덱스에서 시작하여 전체 NameValueCollection을 호환되는 1차원 Array에 복사합니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
Get(Int32)

하나의 쉼표로 구분된 목록에 결합된 NameValueCollection의 지정된 인덱스에서 값을 가져옵니다.

Get(String)

하나의 쉼표로 구분된 목록에 결합된 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 인터페이스를 구현하고, deserialization이 완료되면 deserialization 이벤트를 발생시킵니다.

(다음에서 상속됨 NameObjectCollectionBase)
Remove(String)

NameObjectCollectionBase 인스턴스에서 지정한 키를 가지는 엔트리를 제거합니다.

Set(String, String)

NameValueCollection에 엔트리의 값을 설정합니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

ICollection.CopyTo(Array, Int32)

대상 배열의 지정된 인덱스에서 시작하여 전체 NameObjectCollectionBase을 호환되는 1차원 Array에 복사합니다.

(다음에서 상속됨 NameObjectCollectionBase)
ICollection.IsSynchronized

NameObjectCollectionBase 개체에 대한 액세스가 동기화되어 스레드로부터 안전하게 보호되는지 여부를 나타내는 값을 가져옵니다.

(다음에서 상속됨 NameObjectCollectionBase)
ICollection.SyncRoot

NameObjectCollectionBase 개체에 대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다.

(다음에서 상속됨 NameObjectCollectionBase)

확장 메서드

Cast<TResult>(IEnumerable)

IEnumerable의 요소를 지정된 형식으로 캐스팅합니다.

OfType<TResult>(IEnumerable)

지정된 형식에 따라 IEnumerable의 요소를 필터링합니다.

AsParallel(IEnumerable)

쿼리를 병렬화할 수 있도록 합니다.

AsQueryable(IEnumerable)

IEnumerableIQueryable로 변환합니다.

적용 대상

스레드 보안

공용 정적 (Shared Visual Basic의)이 형식의 멤버는 스레드로부터 안전 합니다. 인스턴스 구성원은 스레드로부터의 안전성이 보장되지 않습니다.

이 구현은 에 대해 동기화된(스레드로부터 안전한) 래퍼를 NameValueCollection제공하지 않지만 파생 클래스는 클래스의 NameValueCollectionNameObjectCollectionBase 속성을 사용하여 SyncRoot 자체 동기화된 버전의 를 만들 수 있습니다.

컬렉션을 열거 되지 본질적으로 스레드로부터 안전한 프로시저가 있습니다. 컬렉션이 동기화되어 있을 때 다른 스레드에서 해당 컬렉션을 수정할 수 있으므로 이렇게 되면 열거자에서 예외가 throw됩니다. 열거하는 동안 스레드로부터 안전을 보장하려면 전체 열거를 수행하는 동안 컬렉션을 잠그거나 다른 스레드에서 변경된 내용으로 인해 발생한 예외를 catch하면 됩니다.

추가 정보