在C#中,数组有多种用法,包括以下几个方面:
var numbers = new int[5];
int[] numbers = new int[5];
int firstNumber = numbers[0];
numbers[1] = 10;
int length = numbers.Length;
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
foreach (int number in numbers)
{
Console.WriteLine(number);
}
var positiveNumbers = numbers.Where(n => n > 0);
int[,] matrix = new int[3, 3];
matrix[0, 0] = 1;
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[3];
jaggedArray[0][0] = 1;
void PrintArray(int[] array)
{
foreach (int number in array)
{
Console.WriteLine(number);
}
}
int[] numbers = new int[] { 1, 2, 3 };
PrintArray(numbers);
Array.Sort(numbers);
var positiveNumbers = numbers.Where(n => n > 0);
var sortedNumbers = numbers.OrderBy(n => n);
var sum = numbers.Sum();
以上是C#中数组的一些常见用法,还有其他更高级的用法可以根据实际需求进行学习和掌握。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: c# writeline怎样实现换行控制