KeyValuePair<TKey,TValue> 구조체

정의

설정하거나 검색할 수 있는 키/값 쌍을 정의합니다.

generic <typename TKey, typename TValue>
public value class KeyValuePair
public struct KeyValuePair<TKey,TValue>
public readonly struct KeyValuePair<TKey,TValue>
[System.Serializable]
public struct KeyValuePair<TKey,TValue>
type KeyValuePair<'Key, 'Value> = struct
[<System.Serializable>]
type KeyValuePair<'Key, 'Value> = struct
Public Structure KeyValuePair(Of TKey, TValue)

형식 매개 변수

TKey

키의 형식입니다.

TValue

값의 유형입니다.

상속
KeyValuePair<TKey,TValue>
특성

예제

다음 코드 예제에서는 구조체를 사용하여 KeyValuePair<TKey,TValue> 사전의 키와 값을 열거하는 방법을 보여줍니다.

이 코드는 클래스에 제공된 더 큰 예제의 Dictionary<TKey,TValue> 일부입니다.

// When you use foreach to enumerate dictionary elements,
// the elements are retrieved as KeyValuePair objects.
Console::WriteLine();
for each( KeyValuePair<String^, String^> kvp in openWith )
{
    Console::WriteLine("Key = {0}, Value = {1}",
        kvp.Key, kvp.Value);
}
// When you use foreach to enumerate dictionary elements,
// the elements are retrieved as KeyValuePair objects.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
{
    Console.WriteLine("Key = {0}, Value = {1}",
        kvp.Key, kvp.Value);
}
' When you use foreach to enumerate dictionary elements,
' the elements are retrieved as KeyValuePair objects.
Console.WriteLine()
For Each kvp As KeyValuePair(Of String, String) In openWith
    Console.WriteLine("Key = {0}, Value = {1}", _
        kvp.Key, kvp.Value)
Next kvp

설명

속성은 Dictionary<TKey,TValue>.Enumerator.Current 이 형식의 인스턴스를 반환합니다.

C# 언어for each(C++, For Each Visual Basic의 경우)의 문은 foreach 컬렉션에 있는 요소 형식의 개체를 반환합니다. 를 기반으로 IDictionary<TKey,TValue> 하는 컬렉션의 각 요소는 키/값 쌍이므로 요소 형식은 키의 형식이나 값 형식이 아닙니다. 대신 요소 형식은 입니다 KeyValuePair<TKey,TValue>. 예를 들면 다음과 같습니다.

for each(KeyValuePair<String^, String^> kvp in myDictionary)
{
    Console::WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
foreach( KeyValuePair<string, string> kvp in myDictionary )
{
    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
For Each kvp As KeyValuePair(Of String, String) In myDictionary
    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value)
Next kvp

문은 foreach 열거자를 둘러싼 래퍼로, 컬렉션에 쓰는 것이 아니라 읽는 것만 허용합니다.

생성자

KeyValuePair<TKey,TValue>(TKey, TValue)

지정한 키 및 값을 사용하여 KeyValuePair<TKey,TValue> 구조체의 새 인스턴스를 초기화합니다.

속성

Key

키/값 쌍의 키를 가져옵니다.

Value

키/값 쌍의 값을 가져옵니다.

메서드

Deconstruct(TKey, TValue)

현재 KeyValuePair<TKey,TValue>를 분해합니다.

ToString()

키와 값에 대한 문자열 표현을 사용하여 KeyValuePair<TKey,TValue>에 대한 문자열 표현을 반환합니다.

적용 대상

추가 정보