【问题标题】:Starting Index of Arrays in C# and VB.NetC# 和 VB.Net 中数组的起始索引
【发布时间】:2013-04-08 09:28:35
【问题描述】:

看看下面的代码,

C#

 string[] testString = new string[jobs.Count];

等效的 VB.Net

Dim testString() As String = New String(jobs.Count - 1) {}

为什么在创建新数组时在 vb.net 中使用 'jobs.Count - 1' 而不是 'jobs.Count'?

【问题讨论】:

  • 请检查您的问题。这两个例子都没有意义。它们都没有定义为数组。

标签: c# arrays vb.net


【解决方案1】:

在 VB.NET 中,数组声明中的数字表示“最大索引”,但在 C# 中表示“元素数”

【讨论】:

    【解决方案2】:

    在 C# 中,数组具有您提供的元素数量:

    string[] array = new string[2]; // will have two element [0] and [1]
    

    在 VB.NET 中,数组包含您提供的元素数量加一(您指定最大索引值):

    Dim array(2) As String // will have three elements (0), (1) and (2)
    

    【讨论】:

      【解决方案3】:

      因为您的C# 代码示例,

      string testString = new string[jobs.Count];
      

      这是一个创建字符串数组的构造函数。

      在 VB.Net 示例中,

      Dim testString As String = New String(jobs.Count - 1) {}
      

      您指的是一个新的 String 对象,其长度为括号中声明的字符串。

      如果你想在 VB.Net 中创建一个String 的数组,它必须是这样的:

      Dim testString (jobs.Count) As String
      

      请参阅下面的支持链接: VB.Net C#

      【讨论】:

        猜你喜欢
        • 2010-09-10
        • 2017-09-08
        • 1970-01-01
        • 2010-12-19
        • 1970-01-01
        • 1970-01-01
        • 2014-04-06
        相关资源
        最近更新 更多