foreach遍历数组元素

本文通过C#代码示例展示了如何定义并遍历不同类型的数组,包括整型一维数组、二维数组及字符串数组,并提供了完整的输出结果展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArrayOperation
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
int[] a = { 123,12,245,1,2,45,67};//一维数组,类型为int
            int[,] b = { {12,34,1,7},{23,345,12,45}};//二维数组,类型为int
            string[] c = { "hello","World","你好"};//一维数组,类型为string
            foreach (int i in a) {//输出a数组
                System.Console.Write("{0}  ",i);
            }
            System.Console.WriteLine();
//换行
            foreach (int j in b){//输出b数组
                System.Console.Write("{0}  ", j);
            }
            System.Console.WriteLine();
//换行
            foreach (string j in c){//输出c数组
                System.Console.Write("{0}  ", j);
            }
            System.Console.ReadLine();
//方便看结果,免得结果一闪而过
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值