using System; public class ForEachTest { public static void Main() { int odd=0; int eve=0; int length=100; int[] arr=new int[length]; Random r = new Random(); for(int i=0;i<length;i++) //随机数赋值 { arr[i]= r.Next(); } foreach(int tem in arr) //foreach 举例 { if(tem%2==0) eve++; else odd++; } Console.WriteLine("奇数"+odd+"个"); Console.WriteLine("偶数"+eve+"个"); } }
foreach (DataGridViewRow row in kDataGridView1.Rows) { switch(row.Cells["state"].Value.ToString()) { case "0": row.Cells["state"].Value = "未用"; break; case "1": row.Cells["state"].Value = "正常"; break; case "2": row.Cells["state"].Value = "停用"; break; case "3": row.Cells["state"].Value = "废弃"; break; case "4": row.Cells["state"].Value = "挂失"; break; default: row.Cells["state"].Value = " "; break; } }