我是靠谱客的博主 能干蜜粉,这篇文章主要介绍C# 用foreach遍历,现在分享给大家,希望可以做个参考。

 
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

namespace TestCollectionForeach
{
class Program
{
static void Main( string [] args)
{
StringCollection strColl
= new StringCollection();
strColl.Add(
" Gavin " );
strColl.Add(
" Jane " );
strColl.Add(
" Microsoft.com " );
foreach (String str in strColl)
{
strColl[strColl.IndexOf(str)]
= str + " _king " ;
Console.WriteLine(str);
}
// 当使用foreach来遍历strColl时,当改变集合的值的时候,遍历会抛出异常
// 使用foreach进行遍历要注意这一点
// 更深入的一点说,foreach是目标对象实现的,也就是说foreach本身是一种设计模式而不是
// 一种循环方法。
}
}
}
// 当知道集合的数量的时候,可用for循环取而代之。
namespace TestCollectionForeach
{
class Program
{
static void Main( string [] args)
{
StringCollection strColl
= new StringCollection();
strColl.Add(
" Gavin " );
strColl.Add(
" Jane " );
strColl.Add(
" Microsoft.com " );
for ( int i = 0 ; i < strColl.Count; ++ i)
{
strColl[i]
= strColl[i] + " _king " ;
Console.WriteLine(strColl[i]);
}
}
}
}


转载于:https://www.cnblogs.com/gavinsp/archive/2011/04/25/2028712.html

最后

以上就是能干蜜粉最近收集整理的关于C# 用foreach遍历的全部内容,更多相关C#内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(122)

评论列表共有 0 条评论

立即
投稿
返回
顶部