在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#中數組的一些常見用法,還有其他更高級的用法可以根據實際需求進行學習和掌握。