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달리 이 클래스는 단일 키 아래에 여러 문자열 값을 저장할 수 있습니다.
이 클래스는 헤더, 쿼리 문자열 및 양식 데이터에 사용할 수 있습니다.
이 형식의 컬렉션은 요소의 순서를 유지하지 않으며 컬렉션을 열거할 때 특정 순서가 보장되지 않습니다.
용량 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) |
비어 있고, 지정된 초기 용량을 가지며, 지정된 개체를 사용하는 클래스의 새 인스턴스 NameValueCollection 를 IEqualityComparer 초기화합니다. |
| 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 |
|---|---|
| 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할 수 있습니다.