C# Array GetLongLength() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In C#, GetLongLength(dimension) is like GetLength, but it returns a long instead of an int.

Everyday arrays fit in int. Use GetLongLength when you must handle enormous dimensions safely as 64-bit values.

This is an instance member of System.Array. You call it on an array variable with a dot, such as arr.GetLongLength(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.

Syntax

Call GetLongLength with a 0-based dimension index.

C#
long rows = grid.GetLongLength(0);
CallWhat it does
grid.GetLongLength(0)Dimension 0 length as long.
grid.GetLongLength(1)Dimension 1 length as long.

For most beginner programs, prefer GetLength.

Library declarations
C#
public long GetLongLength(int dimension);

Parameters

ParameterDescription
dimensionRequired. 0-based dimension index.

Return Value

Return ValueDescription
longLength of that dimension.

C# Examples

Example 1 — Long Length of a Dimension

Print GetLongLength beside GetLength.

C#
using System;

class Program {
    static void Main() {
        int[,] grid = new int[2, 4];
        Console.WriteLine(grid.GetLongLength(0));
        Console.WriteLine(grid.GetLongLength(1));
        Console.WriteLine(grid.GetLength(1));
    }
}

How It Works

  1. Same counts. 2 and 4 match the int API.
  2. long type. Useful when assigning to long variables.
  3. GetLength agrees. Dimension 1 is 4 either way.
  4. Small grid. No overflow concerns here.

Example 2 — 1D Array GetLongLength

Compare with Length on a vector.

C#
using System;

class Program {
    static void Main() {
        string[] names = { "a", "b", "c" };
        long n = names.GetLongLength(0);
        Console.WriteLine(n);
        Console.WriteLine(n == names.Length);
    }
}

How It Works

  1. Dimension 0. Only dimension on a 1D array.
  2. Value 3. Three names.
  3. Equals Length. Comparison to int Length is true after numeric promotion.
  4. Beginner default. Still prefer Length for 1D.

Example 3 — Loop Using GetLongLength

Use long indexes carefully (shown with cast for indexer).

C#
using System;

class Program {
    static void Main() {
        int[] nums = { 10, 20, 30 };
        long len = nums.GetLongLength(0);
        for (long i = 0; i < len; i++) {
            Console.WriteLine(nums[(int)i]);
        }
    }
}

How It Works

  1. long loop var. Matches GetLongLength’s return type.
  2. Cast for indexer. Typical int[] indexer wants int.
  3. Same visit order. All three values print.
  4. Rare need. Shown for API completeness.

Frequently Asked Questions

When a dimension might be larger than int can store, or when you already work in long sizes.
The numeric value matches; only the return type differs (long vs int).
Yes. Pass the dimension index like GetLength.
An int total element count. Huge arrays also have LongLength.
GetLength or Length for normal apps.

Next: GetLowerBound()

Return the starting index of a dimension (usually 0) with System.Array.

GetLowerBound() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful