如何在 C# 中使用陣列類的 GetUpperBound 方法?


C# 中的陣列類的 GetUpperBound() 方法獲取 Array 中指定維度上的上限。

首先,設定陣列並獲取上限,如下所示 -

arr.GetUpperBound(0).ToString()

下面是一個介紹在 C# 中使用 GetUpperBound() 方法的示例。

示例

 即時演示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         Array arr = Array.CreateInstance(typeof(String), 6);
         arr.SetValue("One", 0);
         arr.SetValue("Two", 1);
         arr.SetValue("Three", 3);
         arr.SetValue("Four", 4);
         arr.SetValue("Five", 5);
         Console.WriteLine("Upper Bound: {0}",arr.GetUpperBound(0).ToString());
         Console.ReadLine();
      }
   }
}

輸出

Upper Bound: 5

更新於: 2020-06-23

941 次瀏覽

開啟您的 職業生涯

透過完成課程來獲得認證

立即開始
廣告
© . All rights reserved.