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달리 이 클래스는 단일 키 아래에 여러 문자열 값을 저장할 수 있습니다.

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

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

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

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

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

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

Caution

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

생성자

Name Description
NameValueCollection()

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

NameValueCollection(IEqualityComparer)

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

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

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

NameValueCollection(Int32, IEqualityComparer)

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

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

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

NameValueCollection(Int32, NameValueCollection)

지정된 초기 용량 또는 복사된 NameValueCollection 항목 수와 동일한 초기 용량을 사용하여 지정된 항목에서 새 NameValueCollection 항목으로 항목을 복사합니다. 그 중 더 크고 기본 대/소문자를 구분하지 않는 해시 코드 공급자와 기본 대/소문자를 구분하지 않는 비교자를 사용합니다.

NameValueCollection(Int32)

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

NameValueCollection(NameValueCollection)

원본 컬렉션과 동일한 해시 코드 공급자 및 동일한 비교자를 사용하여 복사된 NameValueCollection 항목 수와 동일한 초기 용량을 사용하여 지정된 항목의 항목을 새 NameValueCollection 항목으로 복사합니다.

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

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

속성

Name Description
AllKeys

의 모든 키를 NameValueCollection가져옵니다.

Count

인스턴스에 포함된 NameObjectCollectionBase 키/값 쌍의 수를 가져옵니다.

(다음에서 상속됨 NameObjectCollectionBase)
IsReadOnly

인스턴스가 읽기 전용인지 여부를 NameObjectCollectionBase 나타내는 값을 가져오거나 설정합니다.

(다음에서 상속됨 NameObjectCollectionBase)
Item[Int32]

지정된 인덱 NameValueCollection스의 항목을 가져옵니다.

Item[String]

에 지정된 키를 NameValueCollection가진 항목을 가져오거나 설정합니다.

Keys

인스턴스의 NameObjectCollectionBase.KeysCollection 모든 키를 포함하는 인스턴스를 NameObjectCollectionBase 가져옵니다.

(다음에서 상속됨 NameObjectCollectionBase)

메서드

Name Description
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)

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

Get(String)

쉼표로 구분된 하나의 목록으로 결합된 목록에서 NameValueCollection 지정된 키와 연결된 값을 가져옵니다.

GetEnumerator()

를 반복하는 열거자를 반환합니다 NameObjectCollectionBase.

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

기본 해시 함수로 사용됩니다.

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

지정된 인덱스의 NameValueCollection키를 가져옵니다.

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

인터페이스를 ISerializable 구현하고 인스턴스를 serialize하는 데 필요한 데이터를 반환합니다 NameObjectCollectionBase .

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

현재 인스턴스의 Type 가져옵니다.

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

지정된 인덱스의 NameValueCollection값을 가져옵니다.

GetValues(String)

에서 지정된 키 NameValueCollection와 연결된 값을 가져옵니다.

HasKeys()

키가 포함되지 않은NameValueCollection지 여부를 null 나타내는 값을 가져옵니다.

InvalidateCachedArrays()

컬렉션 null의 캐시된 배열을 .로 다시 설정합니다.

MemberwiseClone()

현재 Object단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
OnDeserialization(Object)

역직렬화가 ISerializable 완료되면 인터페이스를 구현하고 역직렬화 이벤트를 발생합니다.

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

인스턴스에서 NameObjectCollectionBase 지정된 키를 가진 항목을 제거합니다.

Set(String, String)

에 있는 NameValueCollection항목의 값을 설정합니다.

ToString()

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

(다음에서 상속됨 Object)

명시적 인터페이스 구현

Name Description
ICollection.CopyTo(Array, Int32)

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

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

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

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

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

(다음에서 상속됨 NameObjectCollectionBase)

확장명 메서드

Name Description
AsParallel(IEnumerable)

쿼리의 병렬 처리를 사용하도록 설정합니다.

AsQueryable(IEnumerable)

IEnumerable IQueryable변환합니다.

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

적용 대상

스레드 보안

이 형식의 공용 정적(Shared Visual Basic) 멤버는 스레드로부터 안전합니다. 모든 인스턴스 멤버는 스레드로부터 안전하게 보호되지 않습니다.

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

컬렉션을 열거하는 것은 본질적으로 스레드로부터 안전한 프로시저가 아닙니다. 컬렉션이 동기화된 경우에도 다른 스레드는 컬렉션을 수정할 수 있으므로 열거자가 예외를 throw합니다. 열거 중 스레드 안전을 보장하기 위해 전체 열거 중에 컬렉션을 잠그거나 다른 스레드의 변경으로 인한 예외를 catch할 수 있습니다.

추가 정보