Home >Backend Development >C#.Net Tutorial >LongLength property of arrays in C#

LongLength property of arrays in C#

WBOY
WBOYforward
2023-09-17 16:49:151199browse

C# 中数组的 LongLength 属性

In C#, the Array class has a read-only property called LongLength. It returns a long integer value indicating how many elements the array can hold. Only arrays of rank one or higher, that is, non-single-dimensional arrays, can access the LongLength property.

Although the LongLength property provides long integer values, it is important to remember that the maximum size of an array in C# is still limited by the amount of memory supported by the system. If you try to build an array that is too large, an OutOfMemoryException may be thrown.

grammar

public long LongLength { get; }
  • Long − 64-bit integer value, indicating the number of elements in the array.

The number of elements in the entire array is returned as a long integer through the LongLength property of the array in C#. This property is useful when working with large arrays that may contain more than 2 billion elements (the maximum capacity of a 32-bit integer). In this case, the Length property returns a negative value, indicating an overflow problem. The LongLength property solves this problem by returning a long integer capable of representing higher values.

Example

In this program, we create an array containing 1 billion integers and assign a value to each element. We then use 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, for example int[] abc= new int[1000000000];

  • Step-2 - Assign values ​​to array elements, for example abc[0] = 1; abc[1] = 2; ... abc[999999999] = 1000000000;

  • Step-3 - Use the Length property to get the number of elements in an array. Due to integer overflow, this returns a negative number because 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 count the number of elements in a 2- or 3-dimensional array. This helps in accurately counting the number of elements in complex arrays. In this example, we will create a 2-dimensional array and calculate the number of elements of the 2-dimensional array using the LongLength property.

  • Step 1 - Declare a two-dimensional 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

in conclusion

The LongLength property of arrays in C# is a useful property when dealing with large arrays that exceed the integer limit. It allows us to process arrays of almost any size, the only limit is the amount of memory available on the system. It returns the number of elements that the array can hold as a long value.

The above is the detailed content of LongLength property of arrays in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete