如何在 C# 中确定数组的大小

csharpprogrammingserver side programming更新于 2025/5/25 2:37:17

首先,设置一个数组 −

int[] arr = {6, 3, 8, 4};

现在,使用 Length 属性获取数组的大小 −

arr.Length

让我们看看完整的代码 −

示例

using System;
namespace Demo {
   public class Demo {
      public static void Main(string[] args) {
         int[] arr = {6, 3, 8, 4};
         Console.WriteLine("Array...");
         foreach (int i in arr) {
            Console.Write(i + " ");
         }
         Console.WriteLine("
Size of Array: "+arr.Length);       }    } }

输出

数组...
6 3 8 4
Size of Array: 4

相关文章