次の方法で共有


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> オブジェクトのシーケンスを取得するために使用されます。 Visual C# または Visual Basic ループのFor Eachforeachは、各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>内の各値に共通する属性を表します。

適用対象