遍历数组:foreach(type objName in collection/Array)
string test = "Hello,world!";
foreach (char i in test)
{
Console.WriteLine(i);
}
借助foreach,只能一一取得数组中的元素,并不能利用这种语句改变数组所存储的元素。
生成随机数: random
const int N = 50; //const声明常量,不能再次赋值
long[] a = new long[N]; //数组
var r = new Random();
for (int i = 0; i < N; i++)
{
a[i] = r.Next(0 ,10); //一般包括左边的下限,不包括右边的上限
}
foreach (long x in a)
{
Console.WriteLine(x);
}
break会跳出最近的一层循环.
c#预处理指令
#region 提示文字
...
...代码区域
...
#endregion
List
List<int> nums = new List<int>(); //对于集合类,习惯用s结尾
nums.Add(3);
nums.Add(1);
nums.Add(2);
foreach (int num in nums)
{
Console.WriteLine(num);
}
Console.ReadLine();
原文:https://www.cnblogs.com/jiutianzhiyu/p/12853212.html