IGrouping<TKey,TElement>.Key Właściwość

Definicja

Pobiera klucz .IGrouping<TKey,TElement>

public:
 property TKey Key { TKey get(); };
public TKey Key { get; }
member this.Key : 'Key
Public ReadOnly Property Key As TKey

Wartość właściwości

TKey

Klucz obiektu IGrouping<TKey,TElement>.

Przykłady

W poniższym przykładzie pokazano, jak używać Key właściwości do etykietowania każdego IGrouping<TKey,TElement> obiektu w sekwencji IGrouping<TKey,TElement> obiektów. Metoda GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) służy do uzyskiwania sekwencji IGrouping<TKey,TElement> obiektów. Pętla foreach w języku Visual C# lub For Each w języku Visual Basic wykonuje iterację po każdym IGrouping<TKey,TElement> obiekcie, wyprowadzając jego klucz i liczbę zawartych w nim wartości.

// Get a sequence of IGrouping objects.
IEnumerable<IGrouping<System.Reflection.MemberTypes, System.Reflection.MemberInfo>> memberQuery =
    typeof(String).GetMembers().
    GroupBy(member => member.MemberType);

// Output the key of each IGrouping object and the count of values.
foreach (IGrouping<System.Reflection.MemberTypes, System.Reflection.MemberInfo> group in memberQuery)
    Console.WriteLine("(Key) {0} (Member count) {1}", group.Key, group.Count());

// The output is similar to:
// (Key) Method (Member count) 113
// (Key) Constructor (Member count) 8
// (Key) Property (Member count) 2
// (Key) Field (Member count) 1
' Get a sequence of IGrouping objects.
Dim memberQuery As  _
IEnumerable(Of IGrouping(Of System.Reflection.MemberTypes, System.Reflection.MemberInfo)) = _
    Type.GetType("String").GetMembers(). _
    GroupBy(Function(ByVal member) member.MemberType)

' Output the key of each IGrouping object and the count of values.
For Each group As  _
IGrouping(Of System.Reflection.MemberTypes, System.Reflection.MemberInfo) In memberQuery
    MsgBox(String.Format("(Key) {0} (Member count) {1}", group.Key, group.Count()))
Next

' The output is similar to:
' (Key) Method (Member count) 113
' (Key) Constructor (Member count) 8
' (Key) Property (Member count) 2
' (Key) Field (Member count) 1

Uwagi

Klucz elementu IGrouping<TKey,TElement> reprezentuje atrybut wspólny dla każdej wartości w elemecie IGrouping<TKey,TElement>.

Dotyczy