当前位置: 移动技术网 > IT编程>开发语言>c# > C#使用foreach循环遍历数组完整实例

C#使用foreach循环遍历数组完整实例

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#使用foreach循环遍历数组的方法。分享给大家供大家参考,具体如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace consoleapplication1
{
  class program
  {
    static void main(string[] args)
    {
      //声明数组. 第一种方法. 声明并分配元素大小.
      int[] myint = new int[30];
      myint[0] = 30;
      myint[1] = 50;
      // 以此类推, 起始下标为0
      //-------------------------------
      // 声明数组,第二种方法, 声明并直接赋值,没有指定元素大小.
      int[] myint1 = { 20,10,50,65,18,90}; 
      //------------------------------------------
      //声明数组,第三种方法, 声明并分配大小,且赋值.
      int[] i = new int[5] { 10, 20, 30, 40, 50 };
      // -----------------------------------------------
      // foreach循环遍历数组..
      int[] sum = new int[50];
      random rd = new random();
      // 先用for循环给数组取随机数.
      for (int s = 0; s <= sum.length - 1; s++) // sum.length是数组的一个属性,length代表数组的长度
      {
        sum[s] = rd.next(100);
      }
      // 遍历数组输出
      foreach (int t in sum)
      {
        console.writeline(t);
      }
    }
  }
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#遍历算法与技巧总结》、《c#程序设计之线程使用技巧总结》、《c#操作excel技巧总结》、《c#中xml文件操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程

希望本文所述对大家c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网