You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

继承BaseCollection类时,C#索引器的VB.NET等效代码是什么?

C#索引器转VB.NET的写法

你提到的C#代码中的public String this[int Index]是索引器,在VB.NET中需要用Default关键字来实现,完整的VB.NET代码如下:

Public Class StringCollection
    Inherits CollectionBase

    Public Default ReadOnly Property Item(ByVal index As Integer) As String
        Get
            Return DirectCast(MyBase.List(index), String)
        End Get
    End Property
End Class

关键说明:

  • VB.NET用Default关键字标记这是一个索引器,允许通过类实例的[索引]方式访问
  • 索引器的默认名称是Item,这是.NET框架的约定,也可以自定义名称,但使用Item更符合规范
  • 类型转换使用DirectCast,功能等价于C#中的(String)强制转换
  • 继承CollectionBase后,通过MyBase.List访问基类的内部列表

内容的提问来源于stack exchange,提问作者schlebe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 20:19:51