LongLength property of an Array in C#


In C#, the Array class has a read-only property called LongLength. It returns a long integer value that indicates how many elements the array can accommodate. Only arrays with a rank of one or more, i.e., those that are not single-dimensional arrays, can access the LongLength property.

Although the LongLength property provides a long integer value, it's crucial to remember that the maximum size of an array in C# is still constrained by the amount of memory that the system supports. If you try to build an array that is too big, an OutOfMemoryException can be raised.

Syntax

public long LongLength { get; }
  • Long − 64-bit integer value that represents the number of elements in the Array.

The entire number of elements in the array is returned as a long integer via the LongLength property of an array in C#. When dealing with big arrays that may include more than 2 billion elements—the maximum capacity for a 32-bit integer—this attribute is helpful. The Length property would in this situation yield a negative value, signifying an overflow problem. By returning a long integer, which has the ability to express higher values, the LongLength property gets around this issue.

Example

In this program, we create an array of 1 billion integers and assign values to each element. Then, we use both the Length and LongLength properties to get the number of elements in the array. The Length property returns a negative number due to integer overflow, while the LongLength property returns the correct number of elements as a long integer.

Algorithm

  • Step-1 − Create an array of any type, such as int[] abc= new int[1000000000];

  • Step-2 − Assign values to the array elements, such as abc[0] = 1; abc[1] = 2; ... abc[999999999] = 1000000000;

  • Step-3 − Use the Length property to get the number of elements in the array. This will return a negative number due to integer overflow, since the array has more than 2 billion elements.

  • Step-4 − Use the LongLength property to get the total number of elements in the array as a long integer. This will return the correct number, which is 1000000000.

using System;
class Program {
   static void Main(string[] args) {
      //initilize an array of 1000000000 elements
      int[] arr = new int[1000000000];       
      for (int i = 0; i < arr.Length; i++) 
      
      //loop to assign values to array you can do this without loop but its a large array so loop is needed {
         arr[i] = i + 1;
      }
      Console.WriteLine("Length: " + arr.Length);// length property        Console.WriteLine("LongLength: " + arr.LongLength);//longlength property
   }
}

Output

Length: 1000000000

Example

You can calculate the number of elements in a 2-D or 3-D array. This helps you get an accurate count of elements in complex arrays. In this example, we will create a 2-D array and count the elements of the 2-D array using LongLength property.

  • Step 1 − Declare a 2-D array of int data type and initialize it with some values.

  • Step 2 − Get the LongLength property of the array.

  • Step 3 − Print the value of the LongLength property to the console.

using System;
class Program {
   static void Main(string[] args) {
      // Declare and initialize a 2-D array of integers
      int [,] a = new int [3,4] {
         {0, 1, 2, 3} ,  
         {4, 5, 6, 7} ,  
         {8, 9, 10, 11}   
      };
      
      // Get the LongLength property of the array
      long length = a.LongLength;

      // Print the value of the LongLength property to the console
      Console.WriteLine("The length of the array is: " + length);
   }
}

Output

The length of the array is: 12

Conclusion

when working with huge arrays that go beyond the limit of an integer, the LongLength property of an array in C# is a helpful attribute to employ. It allows us to deal with arrays of nearly any size, with the only restriction being the amount of memory that the system has available. It returns the number of elements an array may carry as a long integer value.

Updated on: 25-Apr-2023

187 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements