如何在 C# 中使用数组类的 GetUpperBound 方法?

csharpprogrammingserver side programming

C# 中数组类的 GetUpperBound() 方法用于获取数组中指定维度的上界。

首先,设置数组并获取上界,如下所示 −

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

相关文章