Dùng property Length:
int[] arr = { 6, 3, 8, 4 };
int size = arr.Length; // 4
Ví dụ hoàn chỉnh
using System;
namespace Demo
{
public class Demo
{
public static void Main(string[] args)
{
int[] arr = { 6, 3, 8, 4 };
Console.Write("Mảng: ");
foreach (int i in arr) Console.Write(i + " ");
Console.WriteLine("\nKích thước mảng: " + arr.Length);
}
}
}
Kết quả
Mảng: 6 3 8 4 Kích thước mảng: 4
Lưu ý:
Lengthlà property (không phải method), O(1)- Mảng đa chiều:
arr.GetLength(0)cho dimension 0,arr.Rankcho số chiều List<T>dùngCountpropertyArray.Lengthtổng số phần tử tất cả chiều