码迷,mamicode.com
首页 > 编程语言 > 详细

C#遍历数组

时间:2016-06-28 23:47:07      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

Eg:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             //声明数组. 第一种方法. 声明并分配元素大小.
13             int[] Myint = new int[30];
14             Myint[0] = 30;
15             Myint[1] = 50;
16             // 以此类推, 起始下标为0
17             //-------------------------------
18             // 声明数组,第二种方法, 声明并直接赋值,没有指定元素大小.
19             int[] Myint1 = { 20,10,50,65,18,90}; 
20             //------------------------------------------
21             //声明数组,第三种方法, 声明并分配大小,且赋值.
22             int[] i = new int[5] { 10, 20, 30, 40, 50 };
23 
24             // -----------------------------------------------
25             // foreach循环遍历数组..
26             int[] Sum = new int[50];
27             Random rd = new Random();
28            // 先用for循环给数组取随机数.
29             for (int s = 0; s <= Sum.Length - 1; s++)  // Sum.Length是数组的一个属性,Length代表数组的长度
30             {
31                 Sum[s] = rd.Next(100);
32             }
33            // 遍历数组输出
34             foreach (int t in Sum)
35             {
36                 Console.WriteLine(t);
37             }
38         }
39     }
40 }

 

C#遍历数组

标签:

原文地址:http://www.cnblogs.com/AdaLoong/p/5625159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!