这里以一维整型数组为例
using System;
namespace debug
{
class Program
{
static void Main(string[] args)
{
// 方法一
int[] numbers = { 2, 12, 5, 15 };
Console.WriteLine(numbers.GetType());
foreach(int i in numbers)
{
Console.WriteLine($"{i}");
}
// 方法二
int[] numbers1 = new int[] { 2, 12, 5, 15 };
Console.WriteLine(numbers1.GetType());
foreach (int i in numbers)
{
Console.WriteLine($"{i}");
}
}
}
}
//Result:
//System.Int32[]
//2
//12
//5
//15
//System.Int32[]
//2
//12
//5
//15
我们可以看到,在定义数组的时候我们可以直接省略后半段的new int[]
,直接输入想要定义的数组的值即可。
如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~