Why can I access an item in KeyCollection/ValueCollection by index even if it doesn't implement IList(Of Key)?

后端 未结 2 527
Happy的楠姐
Happy的楠姐 2020-12-04 02:00

I\'ve noticed a strange VB.NET thing. Coming from this question I\'ve provided a way to access keys and values of dictionaries\' KeysCollection and Values

2条回答
  •  [愿得一人]
    2020-12-04 02:01

    It invokes Enumerable.ElementAtOrDefault, not the indexer.

    // [10 13 - 10 31]
    IL_001f: ldloc.1      // keys
    IL_0020: ldc.i4.0     
    IL_0021: call         !!0/*valuetype [mscorlib]System.DateTime*/ [System.Core]System.Linq.Enumerable::ElementAtOrDefault(class [mscorlib]System.Collections.Generic.IEnumerable`1, int32)
    IL_0026: stloc.2      // firstKey
    

    This behavior is documented in the Visual Basic Language Specification, 11.21.3:

    Every queryable collection type whose element type is T and does not already have a default property is considered to have a default property of the following general form:

    Public ReadOnly Default Property Item(index As Integer) As T
        Get
            Return Me.ElementAtOrDefault(index)
        End Get
    End Property
    

    The default property can only be referred to using the default property access syntax; the default property cannot be referred to by name. For example:

    Dim customers As IEnumerable(Of Customer) = ...
    Dim customerThree = customers(2)
    
    ' Error, no such property
    Dim customerFour = customers.Item(4)
    

    If the collection type does not have an ElementAtOrDefault member, a compile-time error will occur.

提交回复
热议问题