IGrouping<TKey,TElement>.Key Properti

Definisi

Mendapatkan kunci dari IGrouping<TKey,TElement>.

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

Nilai Properti

TKey

Kunci dari IGrouping<TKey,TElement>.

Contoh

Contoh berikut menunjukkan cara menggunakan Key properti untuk melabeli setiap IGrouping<TKey,TElement> objek dalam urutan IGrouping<TKey,TElement> objek. Metode GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) ini digunakan untuk mendapatkan urutan IGrouping<TKey,TElement> objek. foreach dalam Visual C# atau For Each dalam perulangan Visual Basic kemudian melakukan iterasi melalui setiap IGrouping<TKey,TElement> objek, menghasilkan kuncinya dan jumlah nilai yang dikandungnya.

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

Keterangan

Kunci dari mewakili IGrouping<TKey,TElement> atribut yang umum untuk setiap nilai dalam IGrouping<TKey,TElement>.

Berlaku untuk