C# - Print Lower Bound and Upper Bound of an Array

Here, we are going to learn how to print the lower bound and upper bound of an array in C#?
Submitted by Nidhi, on August 22, 2020 [Last updated : March 19, 2023]

Here we create an integer array get the lower and upper bound of the array. The lower bound of array specifies the lowest index of the array and upper bound specifies the highest index of the array.

C# program to print the lower bound and upper bound of an array

The source code to print the lower bound and upper bound of an array in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to print the lower bound 
//and upper bound of an array in C#

using System;

class Program
{
    static void Main(string[] args)
    {
        Array intArray = Array.CreateInstance(typeof(int), 5);
        
        intArray.SetValue(10, 0);
        intArray.SetValue(20, 1);
        intArray.SetValue(30, 2);
        intArray.SetValue(40, 3);
        intArray.SetValue(50, 4);
        
        Console.WriteLine("Lower Bound : "+intArray.GetLowerBound(0));
        Console.WriteLine("Upper Bound : " + intArray.GetUpperBound(0));
    }
}

Output

Lower Bound : 0
Upper Bound : 4
Press any key to continue . . .

Explanation

In the above program, we created an array of integers using Array class and then set some values using SetValue() method and then get the lower and upper bound using GetLowerBound() and GetUpperBound() method. The lower bound of array specifies the lowest index of the array and upper bound specifies the highest index of the array.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.