• 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]);
    }
    }
    }
    }
    
  • 相关阅读:
    tab切换与表格展示
    ajax
    api
    slice() 方法
    iframe跳转
    快排序
    【问题排查】cpu占用过高排查
    LOJ6013 负载平衡 [最小费用最大流]
    随机序列 [思维题, 组合数]
    P1777 帮助 [状压dp]
  • 原文地址:https://www.cnblogs.com/gavinsp/p/2028712.html
Copyright © 2020-2023  润新知