共用方式為


IGrouping<TKey,TElement>.Key 屬性

定義

取得 IGrouping<TKey,TElement> 的索引鍵。

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

屬性值

TKey

IGrouping<TKey,TElement> 的索引鍵。

範例

下列範例示範如何使用 Key 屬性來 IGrouping<TKey,TElement> 標記物件序列 IGrouping<TKey,TElement> 中的每個物件。 方法 GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) 可用來取得 物件的序列 IGrouping<TKey,TElement>foreach Visual C# 或 For Each Visual Basic 迴圈中的 ,然後逐一查看每個IGrouping<TKey,TElement>物件,並輸出其索引鍵及其包含的值數目。

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

備註

IGrouping<TKey,TElement> 索引鍵代表 中每個值通用的屬性 IGrouping<TKey,TElement>

適用於