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 EachVisual 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>表します。

適用対象