The Array.Lenth property in C# is used to find the length of the array.
Let’s set the array class first −
Array arr = Array.CreateInstance(typeof(String), 3);
arr.SetValue("Electronics", 0);
arr.SetValue("Clothing", 1);
arr.SetValue("Appliances", 2);Now since the array’s length is 3, the Length property will give the result 3 −
arr.Length
The following is the code to implement Array.Length property of array class −
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lower {
class Program {
static void Main(string[] args) {
Array arr = Array.CreateInstance(typeof(String), 3);
arr.SetValue("Electronics", 0);
arr.SetValue("Clothing", 1);
arr.SetValue("Appliances", 2);
Console.WriteLine("Length: {0}",arr.Length.ToString());
Console.ReadLine();
}
}
}Output
Length: 3