C# for和 foreach 的数组遍历 比较
2024-08-26 20:42:32
刚学习程序,感觉写代码 很有意思,所以把自己的感悟写下来啦,第一次写博客,可能是菜鸟中的菜鸟 时间久了,相信就会写的很好哦!
for和 foreach 的数组遍历 比较
很简单的程序,不解释啦!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{ int[] nums = { 3, 5, 99, 23, 53, 88 }; //定义数组
int max = 0; for (int i = 0; i < nums.Length; i++) //for遍历数组
{
if (nums[i] > max)
{
max = nums[i];
}
}
Console.WriteLine("for遍历数组demo:" + max);
Console.WriteLine("====================================================");
foreach (var p in nums) //使用foreach 遍历
{
if (p > max)
{
max = p;
} }
Console.WriteLine("foreach遍历数组demo:"+max); } }
}
结果为:
最新文章
- RS-232, RS-422, RS-485 Serial Communication General Concepts(转载)
- Scala:没有continue,break怎么办?
- 模拟java.util.Collection一些简单的用法
- Brackets(bestcoder)
- JavaScript中in操作符(for..in)、Object.keys()和Object.getOwnPropertyNames()的区别
- Android IOS WebRTC 音视频开发总结(十)-- webrtc入门002
- 两款较好的Web前端性能测试工具
- Objective-C 学习笔记(1)
- 防止跨域(jsonp详解)
- 关于URL 解码, 编码
- wxPython学习笔记(二)
- 微软推荐的Get a code signing certificate流程和链接
- 表设计与SQL优化
- table 中的td 字段超长,超过部分用....表示
- 主席树初步 HDU2665的区间第k小
- day001-html知识点总结(二)不常见但很重要的元素汇总
- NameError: name &#39;messagebox&#39; is not defined 错误处理
- 安装supervisord
- 【安卓开发】Layout Inflation不能这么用
- Angular 中的数据交互(get jsonp post)