IGrouping<TKey,TElement>.Key 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取 IGrouping<TKey,TElement> 的键。
public:
property TKey Key { TKey get(); };
public TKey Key { get; }
member this.Key : 'Key
Public ReadOnly Property Key As TKey
属性值
TKey
示例
以下示例演示如何使用 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>。