Csharp/CSharp Tutorial/Data Structure/Array Bound

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Array: GetLowerBound, GetUpperBound

<source lang="csharp">using System; public class ArrayTest {

   public static void Main( ) {
      int[] array_1 = new int[5];
     
      for( int i = array_1.GetLowerBound(0); i <= array_1.GetUpperBound(0); i++)
         array_1[i] = i+1;
      for( int j = array_1.GetLowerBound(0); j <= array_1.GetUpperBound(0); j++)
         Console.WriteLine("array_1[{0}] = {1}", j, array_1[j]);
   }

}</source>

array_1[0] = 1
array_1[1] = 2
array_1[2] = 3
array_1[3] = 4
array_1[4] = 5

Get lowerbound and upperbound

<source lang="csharp">using System; public class MainClass {

   static void Main() {
       int[,] twoDim = { {1, 2, 3},
                         {4, 5, 6},
                         {7, 8, 9} };
       for( int i = twoDim.GetLowerBound(0);
            i <= twoDim.GetUpperBound(0);
            ++i ) {
           for( int j = twoDim.GetLowerBound(1);
                j <= twoDim.GetUpperBound(1);
                ++j ) {
               Console.WriteLine( twoDim[i,j] );
           }
       }
   }

}</source>

1
2
3
4
5
6
7
8
9