【问题标题】:Accessing Item property using C#使用 C# 访问 Item 属性
【发布时间】:2012-07-20 06:47:54
【问题描述】:

我正在尝试使用http://msdn.microsoft.com/en-us/library/aa289500%28v=vs.71%29.aspx

它工作正常,但我经常在 VB 版本中看到 Item

但是,button.Item 在 C# 中不存在,但是当我打开一个新项目并在 VB.NET 中尝试时,我得到了Item。为什么会这样?如何在 C# 中使用 Item

Visual Basic

Default Public ReadOnly Property Item(ByVal Index As Integer) As _
   System.Windows.Forms.Button
   Get
      Return CType(Me.List.Item(Index), System.Windows.Forms.Button)
   End Get
End Property

C#

public System.Windows.Forms.Button this [int Index]
{
get
   {
      return (System.Windows.Forms.Button) this.List[Index];
   }
}

【问题讨论】:

    标签: c# vb.net visual-studio-2012


    【解决方案1】:

    在 C# 中调用“项目”可以用方括号来完成:

    var myButton = myButtonsArray[0];
    

    “[]”括号实际上是在调用索引器,在 VB 中称为“Item”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多