
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# Array - GetLength() Method
The C# Array GetLength() method is used to return a 32-bit integer representing the number of elements available in an array of specified dimension.
This method throws an IndexOutOfRangeException if the dimension is zero or if it is greater than or equal to the rank.
Syntax
Following is the syntax of the C# Array GetLength() method −
public int GetLength(int dimension);
Parameters
This method accepts the following parameters −
- dimension: It represents a zero-based dimension of the array whose length needs to be determined.
Return value
This method returns a 32-bit integer that represents the number of elements in the specified array.
Example 1: Display Length of the Integer Array
This is the basic example of the GetLength() method to display the number of element present in the array −
using System; class Program { static void Main() { int[] Number = new int[] {1, 2, 3, 4, 5}; // Get the length int length = Number.GetLength(0); Console.WriteLine("Length of the Array: " + length); } }
Output
Following is the output −
Length of the Array: 5
Example 2: Find the Length of 2D Array
Let's create an example that displays the number of rows and columns in the two-dimensional array using theGetLength() method −
using System; class Program { static void Main() { int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Get the number of rows (dimension 0) int rows = matrix.GetLength(0); // Get the number of columns (dimension 1) int columns = matrix.GetLength(1); Console.WriteLine($"Rows: {rows}, Columns: {columns}"); } }
Output
Following is the output −
Rows: 3, Columns: 3
Example 3: Get the Dimension of 3D-Array
Let's see another example of theGetLength()method to display the number of elements of each dimension in the array −
using System; class Program { static void Main() { int[,,] cube = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }; // Get the length of the first dimension int dim0 = cube.GetLength(0); // Get the length of the second dimension int dim1 = cube.GetLength(1); // Get the length of the third dimension int dim2 = cube.GetLength(2); Console.WriteLine($"Dimension 0: {dim0}, Dimension 1: {dim1}, Dimension 2: {dim2}"); } }
Output
Following is the output −
Dimension 0: 2, Dimension 1: 2, Dimension 2: 2
Example 4: Dynamic Access to Array Dimensions
Let's see another version of the GetLength() method. In this version, we use the Rank property to determine the number of dimensions.
using System; class Program { static void Main(){ double[,] table = new double[4, 5]; for (int i = 0; i < table.Rank; i++){ Console.WriteLine($"Length of dimension {i}: {table.GetLength(i)}"); } } }
Output
Following is the output −
Length of dimension 0: 4 Length of dimension 1: 5