C#这个VB.NET Me Indexer转换的索引器

jp2*_*ode 0 .net c# vb.net

如何在VB.NET中编写?

public class Foos : ICollection<Foo> {

  private List<Foo> list;

  public Foos() {
    list = new List<Foo>();
  }

  public Foo this[int index] {
    get {
      return list[index];
    }
  }

}
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的:

Public Class Foos
  Implements ICollection(Of Foo)

  Private list as Generic.List(Of Foo)

  Public Sub New()
    list = New Generic.List(Of Foo)()
  End Sub

  Public ReadOnly Property Me(index As Integer) As Foo
    Get
      Return list(index)
    End Get
  End Property

End Class
Run Code Online (Sandbox Code Playgroud)

Visual Studio提供了一个编译错误Me(index As Integer)(指向Me关键字):

关键字无效作为标识符.

VB编码器在这里使用什么?

D S*_*ley 7

使用Default保留名称以外的内容Me:

  Default Public ReadOnly Property Item(index As Integer) As Foo
    Get
      Return list(index)
    End Get
  End Property
Run Code Online (Sandbox Code Playgroud)

Default表示该属性是该类的默认属性,这意味着如果您不指定属性名称,它将隐式使用默认属性.所以它看起来像一个数组索引器,但实际上只是一个方法调用.

您也可以将它命名为任何您想要的名称 - 这Item是框架类使用的相当常见的名称,但使用该名称并没有什么神奇之处Item.