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.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) |
사용되지 않음.
직렬화할 수 있으며 지정된 SerializationInfo 및 StreamingContext를 사용하는 NameValueCollection 클래스의 새 인스턴스를 초기화합니다. |
속성
AllKeys |
NameValueCollection의 모든 키를 가져옵니다. |
Count |
NameObjectCollectionBase 인스턴스에 포함된 키/값 쌍의 수를 가져옵니다. (다음에서 상속됨 NameObjectCollectionBase) |
IsReadOnly |
NameObjectCollectionBase 인스턴스가 읽기 전용인지 여부를 나타내는 값을 가져오거나 설정합니다. (다음에서 상속됨 NameObjectCollectionBase) |
Item[Int32] |
NameValueCollection의 지정된 인덱스에 있는 엔트리를 가져옵니다. |
Item[String] |
NameValueCollection에서 지정된 키를 가지는 엔트리를 가져오거나 설정합니다. |
Keys |
NameObjectCollectionBase.KeysCollection 인스턴스의 모든 키를 포함하는 NameObjectCollectionBase 인스턴스를 가져옵니다. (다음에서 상속됨 NameObjectCollectionBase) |
메서드
명시적 인터페이스 구현
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) |
IEnumerable을 IQueryable로 변환합니다. |
적용 대상
스레드 보안
공용 정적 (Shared
Visual Basic의)이 형식의 멤버는 스레드로부터 안전 합니다. 인스턴스 구성원은 스레드로부터의 안전성이 보장되지 않습니다.
이 구현은 에 대해 동기화된(스레드로부터 안전한) 래퍼를 NameValueCollection제공하지 않지만 파생 클래스는 클래스의 NameValueCollectionNameObjectCollectionBase 속성을 사용하여 SyncRoot 자체 동기화된 버전의 를 만들 수 있습니다.
컬렉션을 열거 되지 본질적으로 스레드로부터 안전한 프로시저가 있습니다. 컬렉션이 동기화되어 있을 때 다른 스레드에서 해당 컬렉션을 수정할 수 있으므로 이렇게 되면 열거자에서 예외가 throw됩니다. 열거하는 동안 스레드로부터 안전을 보장하려면 전체 열거를 수행하는 동안 컬렉션을 잠그거나 다른 스레드에서 변경된 내용으로 인해 발생한 예외를 catch하면 됩니다.
추가 정보
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기