在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#中数组的一些常见用法,还有其他更高级的用法可以根据实际需求进行学习和掌握。