Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# array

作者:迎着风飛行こ   发布日期:2026-06-20   浏览:73

using System;

class Program
{
    static void Main()
    {
        // 定义一个整数数组
        int[] numbers = new int[5] { 1, 2, 3, 4, 5 };

        // 遍历数组并打印每个元素
        foreach (int num in numbers)
        {
            Console.WriteLine(num);
        }

        // 获取数组的长度
        Console.WriteLine("数组的长度是: " + numbers.Length);

        // 修改数组中的元素
        numbers[0] = 10;
        Console.WriteLine("修改后的第一个元素是: " + numbers[0]);

        // 定义一个多维数组
        int[,] matrix = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };

        // 遍历多维数组
        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                Console.Write(matrix[i, j] + " ");
            }
            Console.WriteLine();
        }
    }
}

解释说明:

  1. 定义数组int[] numbers = new int[5] { 1, 2, 3, 4, 5 }; 创建了一个包含5个整数的数组。
  2. 遍历数组:使用 foreach 循环遍历数组中的每个元素并打印出来。
  3. 获取数组长度:通过 numbers.Length 获取数组的长度。
  4. 修改数组元素:通过索引修改数组中的元素,例如 numbers[0] = 10; 将第一个元素修改为10。
  5. 多维数组int[,] matrix = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } }; 创建了一个2行3列的二维数组。
  6. 遍历多维数组:使用嵌套的 for 循环遍历二维数组,并打印出每个元素。

上一篇:c# hashtable

下一篇:c# json解析

大家都在看

c# 二进制

c# 创建目录

c# socket服务端连接多个客户端

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

c# invoke方法

.net和c#

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站