使用for语句来实现:需要进行显式的强制转换
再来
- <span style="font-size:18px;"> int[] a=new int[3]{1,2,3}; ArrayList arrint = new ArrayList();
- arrint.AddRange(a); for (int i = 0; i < arrint.Count;i++ )
- { int n = (int)arrint[i];
- Console.WriteLine(n); }
- Console.ReadLine();</span>
两个程序输出的结果为:每一行一个元素,分别为1,2,3。
foreach语句对于string类更是简洁:
- <span style="font-size:18px;">using System; using System.Collections.Generic;
- using System.Linq; using System.Text;
- using System.Threading.Tasks;
- namespace @foreach {
- class Program {
- static void Main(string[] args) {
- string str = "This is an example of a foreach"; foreach (char i in str)
- { if (char.IsWhiteSpace(i))
- { Console.WriteLine(i);//当i为空格时输出并换行
- } else
- { Console.Write(i);//当i不为空格时只是输出
- } }
- Console.ReadLine(); }










