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

c# foreach

作者:蛋蛋的憂傷   发布日期:2025-12-22   浏览:64

// C# foreach 示例代码

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // 定义一个整数列表
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

        // 使用 foreach 遍历列表中的每个元素
        foreach (int number in numbers)
        {
            Console.WriteLine(number);
        }
    }
}

解释说明:

  • List<int>:定义了一个存储整数的列表。
  • foreach (int number in numbers)foreach 循环用于遍历集合(如数组、列表等)中的每个元素。这里的 number 是每次迭代时从 numbers 列表中取出的一个元素。
  • Console.WriteLine(number):在控制台输出当前迭代的元素值。

这段代码会依次输出列表中的每个数字:1, 2, 3, 4, 5。

上一篇:c# substring

下一篇:c# datatable

大家都在看

c# 二进制

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

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

Laravel 中文站