C# 中数组类的 Array.Length 属性有什么作用?
csharpprogrammingserver side programming更新于 2025/10/15 5:22:17
C# 中的 Array.Lenth 属性用于获取数组的长度。
我们先设置数组类的 Array.Length 值 −
Array arr = Array.CreateInstance(typeof(String), 3);
arr.SetValue("Electronics", 0);
arr.SetValue("Clothing", 1);
arr.SetValue("Appliances", 2);
由于数组的长度为 3,因此 Length 属性的结果将是 3 -
arr.Length
以下是实现数组类的 Array.Length 属性的代码 −
示例
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();
}
}
}
输出
Length: 3

