array.Length / Array.Resize()
| Since: | C# 1.0(2002) |
|---|
The Length property gets the number of elements in an array, and the Array.Resize() method changes the size of an array.
Syntax
array.Length // Resizes the array to newSize elements. Updates the reference to the original array. Array.Resize(ref T[] array, int newSize) array.GetLength(int dimension)
Member List
| Member | Description |
|---|---|
| Length | Returns the total number of elements in the array. For multidimensional arrays, this is the product of the element counts across all dimensions. |
| Array.Resize(ref T[] array, int newSize) | Changes the length of the array to newSize. When expanding, new elements are initialized to their default value (0 for numbers, null for strings). When shrinking, elements at the end are removed. |
| GetLength(int dimension) | Returns the number of elements in the specified dimension (zero-based). For a one-dimensional array, GetLength(0) returns the same value as Length. |
Sample Code
Use the Length property to get the number of elements and process all elements in a for loop.
Program.cs
using System;
int[] scores = { 85, 92, 78, 95, 60 };
Console.WriteLine(scores.Length); // 5
for (int i = 0; i < scores.Length; i++)
{
Console.WriteLine($"[{i}] {scores[i]} points");
}
Run the following command:
dotnet run 5 [0] 85 points [1] 92 points [2] 78 points [3] 95 points [4] 60 points
Changing Array Size with Array.Resize()
Array.Resize() lets you increase or decrease the number of elements in an array. When expanding, new elements are initialized to their default value (null for strings). When shrinking, elements at the end are removed.
ResizeSample.cs
using System;
string[] team = { "Yagami Iori", "Kusanagi Kyo", "Terry Bogard" };
Console.WriteLine(team.Length); // 3
Array.Resize(ref team, 5);
team[3] = "Blue Mary";
team[4] = "Goenitz";
Console.WriteLine(team.Length); // 5
foreach (string name in team)
{
Console.Write(name + " ");
}
Console.WriteLine();
Array.Resize(ref team, 2);
Console.WriteLine(team.Length); // 2
Console.WriteLine(team[0]); // Yagami Iori
Run the following command:
dotnet run 3 5 Yagami Iori Kusanagi Kyo Terry Bogard Blue Mary Goenitz 2 Yagami Iori
Multidimensional Arrays and GetLength()
For a 2D array, Length returns the total element count (rows × columns). Use GetLength() to retrieve the element count for each individual dimension. The argument is the dimension index: 0 for the first dimension (rows), 1 for the next (columns).
MultiDimSample.cs
using System;
// 3 rows × 2 columns 2D array
int[,] grid = {
{ 85, 90 },
{ 72, 88 },
{ 95, 60 }
};
Console.WriteLine(grid.Length); // 6 (total elements)
Console.WriteLine(grid.GetLength(0)); // 3 (rows)
Console.WriteLine(grid.GetLength(1)); // 2 (columns)
for (int row = 0; row < grid.GetLength(0); row++)
{
for (int col = 0; col < grid.GetLength(1); col++)
{
Console.Write(grid[row, col] + " ");
}
Console.WriteLine();
}
Run the following command:
dotnet run 6 3 2 85 90 72 88 95 60
Common Mistakes
Common Mistake: Array.Resize() Changes the Reference to the Original Array
Array.Resize() internally creates a new array and copies the elements. If another variable holds a reference to the original array, that variable will not point to the new array after resizing.
using System;
// NG: backup still refers to the old array after Resize
string[] original = { "Yagami Iori", "Kusanagi Kyo" };
string[] backup = original; // both point to the same array
Array.Resize(ref original, 4);
original[2] = "Terry Bogard";
original[3] = "Mai Shiranui";
Console.WriteLine(original.Length); // 4
Console.WriteLine(backup.Length); // 2 (backup still points to the old array)
The following example demonstrates this:
dotnet run 4 2
Calling Array.Resize() causes original to point to a new array, but backup continues to reference the old array before resizing. When the element count changes dynamically, consider using List<T> instead.
using System;
using System.Collections.Generic;
// OK: List<T> avoids reference issues
List<string> team = new List<string> { "Yagami Iori", "Kusanagi Kyo" };
team.Add("Terry Bogard");
team.Add("Mai Shiranui");
Console.WriteLine(team.Count); // 4
Run the following command:
dotnet run 4
Notes
Arrays in C# have a fixed size once declared. Array.Resize() internally creates a new array and copies the elements, so calling it frequently can impact performance. If the number of elements changes dynamically, consider using List<T> from the start.
For sorting arrays, see Array.Sort() / Array.Reverse(). For searching elements, see Array.IndexOf() / Array.Copy().
If you find any errors or copyright issues, please contact us.