Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีใช้เมธอด GetUpperBound() ของคลาส Array ใน C#


เมธอด GetUpperBound() ของคลาส Array ใน C# ดึงขอบเขตบน (upper bound) ของมิติที่ระบุในอาร์เรย์

ก่อนอื่น กำหนดอาร์เรย์และดึง upper bound ดังนี้

arr.GetUpperBound(0).ToString()

ตัวอย่างการใช้ GetUpperBound() ใน C#

ตัวอย่าง

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