C#中foreach用法

foreach 语句为数组或对象集合中的每个元素重复一个嵌入语句组。

foreach 语句用于循环访问集合以获取所需信息,但不应用于更改集合内容以避免产生不可预知的副作用。

嵌入语句为数组或集合中的每个元素继续执行。当为集合中的所有元素完成迭代后,控制传递给 foreach 块之后的下一个语句。

可以在 foreach 块的任何点使用 break 关键字跳出循环,或使用 continue 关键字直接进入循环的下一轮迭代。

foreach 循环还可以通过 gotoreturn  throw 语句退出。

 示例

在此示例中,使用 foreach 显示整数数组的内容。

[csharp]  view plain  copy
  1. class ForEachTest  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };  
  6.         foreach (int i in fibarray)  
  7.         {  
  8.             System.Console.Write(i);  
  9.         }  
  10.     }  
  11. }  

输出

01235813

例1、   计算1到100的和,用foreach语句实现

            //用foreach循环实现1到100的和;

[csharp]  view plain  copy
  1. int[] Array=new int[100];  
  2. for(int i=0; i<100;i++)  
  3. {  
  4.     Array[i]=i+1;  
  5. }  
  6. int sum=0;             
  7. foreach (int j in Array)  
  8. {  
  9.     sum=sum+j;  
  10. }  

例2、计算文本框中的最高成绩及对应学生姓名:文本框中格式:姓名=成绩,按Button控件显示结果

[csharp]  view plain  copy
  1. <span style="margin: 0px; padding: 0px; border: 0px; background: transparent;">   </span>private void button1_Click(object sender, EventArgs e)  
  2.         {  
  3.             //方法一  
  4.             //string str = txtAllScore.Text;  
  5.             //string[] sp = str.Split(new char[] { '=', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);  
  6.   
  7.             //int Max = 0;  
  8.             //int each = 0;  
  9.             //int j = 0;  
  10.             //for (int i = 1; i < sp.Length; i = i + 2)  
  11.             //{  
  12.             //    each = Convert.ToInt32(sp[i]);  
  13.             //    if (each > Max)  
  14.             //    {  
  15.             //        Max = each;  
  16.             //        j = i;  
  17.             //    }  
  18.             //}  
  19.             //txtName.Text = sp[j - 1];  
  20.             //txtScore.Text = sp[j];  
  21.   
  22.             //方法二  
  23.             //string s = txtAllScore.Text;//按照\r\n进行split  
  24.             string[] lines = txtAllScore.Lines;  
  25.             string maxName = "";  
  26.             int maxScore = -1;  
  27.             foreach (string line in lines)  
  28.             {  
  29.                 if (line == "")  
  30.                 {  
  31.                     break;  
  32.                 }  
  33.                 string[] strs = line.Split('=');  
  34.                 string name=strs[0];  
  35.                 string strScore=strs[1];  
  36.                 int score=Convert.ToInt32(strScore);  
  37.                 if(score>maxScore)  
  38.                 {  
  39.                     maxName=name;  
  40.                     maxScore=score;  
  41.                 }  
  42.             }  
  43.             MessageBox.Show(string.Format("{0}是第一名,成绩{1}",maxName,maxScore));  
  44.   
  45.         }  
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值