Explain Length and GetLength() in C#

Learn: What is the difference between Length and GetLength() in C#, when and where they are used in C# program? By IncludeHelp Last updated : April 11, 2023

In C#.Net, Length and GetLength() are used with the arrays, and most of the time these two things are confusing for the developers. In this tutorial, we are going to learn what is the difference between Length and GetLength() in C#. When and where they should be used?

What is the difference between Array.Length and Array.GetLength()?

Length is a property that specifies the total number of elements in an array. Whereas, GetLength() is a pre-defined method of the Array class. It has an argument that specifies the dimension. If we pass 0 to GetLenth() method then it returns the size of the first dimension. And if we pass 1 to GetLegnth() method then it returns the size of the second dimension.

Example of Length Property

using System;

namespace arrayEx {
  class Program {
    static void Main(string[] args) {
      int i = 0;
      int[] X;

      X = new int[5];

      Console.Write("Enter Elements : \n");
      for (i = 0; i < X.Length; i++) {
        Console.Write("\tElement[" + i + "]: ");
        X[i] = Convert.ToInt32(Console.ReadLine());
      }

      Console.Write("\n\nElements are: \n");
      for (i = 0; i < X.Length; i++) {
        Console.WriteLine("\tElement[" + i + "]: " + X[i]);
      }
    }
  }
}

Output

Enter Elements :      
        Element[0]: 10
        Element[1]: 20
        Element[2]: 30
        Element[3]: 40
        Element[4]: 50
		
Elements are:         
        Element[0]: 10
        Element[1]: 20
        Element[2]: 30
        Element[3]: 40
        Element[4]: 50 
Press any key to continue . . .

In the statement for (i = 0; i < X.Length; i++), we used X.Length property which is returning length of the array that is 5.

Example of GetLength() Method

using System;

namespace arrayEx {
  class Program {
    static void Main(string[] args) {
      int i = 0;
      int j = 0;
      int[, ] X;

      X = new int[2, 3];

      Console.Write("Enter Elements : \n");
      for (i = 0; i < X.GetLength(0); i++) {
        for (j = 0; j < X.GetLength(1); j++) {
          Console.Write("\tElement[" + i + "," + j + "]: ");
          X[i, j] = Convert.ToInt32(Console.ReadLine());
        }
      }

      Console.Write("\n\nElements are: \n");
      for (i = 0; i < X.GetLength(0); i++) {
        for (j = 0; j < X.GetLength(1); j++) {
          Console.Write(X[i, j] + " ");
        }
        Console.WriteLine();
      }

    }
  }
}

Output

Enter Elements :        
        Element[0,0]: 11
        Element[0,1]: 22
        Element[0,2]: 33
        Element[1,0]: 44
        Element[1,1]: 55
        Element[1,2]: 66
                        
                        
Elements are:           
11 22 33                
44 55 66
Press any key to continue . . .

In above example GetLength(0) specify number of rows and GetLength(1) specify number of columns.




Comments and Discussions!

Load comments ↻





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