IGrouping<TKey,TElement>.Key Vlastnost

Definice

Získá klíč .IGrouping<TKey,TElement>

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

Hodnota vlastnosti

TKey

Klíč pro IGrouping<TKey,TElement>.

Příklady

Následující příklad ukazuje, jak použít Key vlastnost k označení každého IGrouping<TKey,TElement> objektu v posloupnosti IGrouping<TKey,TElement> objektů. Metoda GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) se používá k získání posloupnosti IGrouping<TKey,TElement> objektů. Smyčka foreach v jazyce Visual C# nebo For Each v jazyce Visual Basic pak iteruje každý IGrouping<TKey,TElement> objekt a vypíše jeho klíč a počet hodnot, které obsahuje.

// 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

Poznámky

Klíč objektu IGrouping<TKey,TElement> představuje atribut, který je společný pro každou hodnotu v objektu IGrouping<TKey,TElement>.

Platí pro