IGrouping<TKey,TElement>.Key プロパティ

定義

IGrouping<TKey,TElement> のキーを取得します。Gets the key of the 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> のキー。The key of the IGrouping<TKey,TElement>.

次の例では、Key プロパティを使用して、IGrouping<TKey,TElement> オブジェクトのシーケンス内の各 IGrouping<TKey,TElement> オブジェクトにラベルを付ける方法を示します。The following example demonstrates how to use the Key property to label each IGrouping<TKey,TElement> object in a sequence of IGrouping<TKey,TElement> objects. GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) メソッドは、IGrouping<TKey,TElement> オブジェクトのシーケンスを取得するために使用されます。The GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) method is used to obtain a sequence of IGrouping<TKey,TElement> objects. foreach Visual C# またはFor EachVisual Basic でループを反復処理して各IGrouping<TKey,TElement>オブジェクト、そのキーとが含まれている値の数を出力します。The foreach in Visual C# or For Each in Visual Basic loop then iterates through each IGrouping<TKey,TElement> object, outputting its key and the number of values it contains.

// 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>内の各値に共通する属性を表します。The key of an IGrouping<TKey,TElement> represents the attribute that is common to each value in the IGrouping<TKey,TElement>.

適用対象