c#动态修改列表

时间:2016-11-22 09:03:59

标签: c# list foreach

我不明白如何用foreach做到这一点...... 目标是每次更改Num时修改列表。 使用Canvas List和工作列表的方式是否适合编码?

class Program
{
    static void Main(string[] args)
    {
        int i_Num = 0;
        string Str_Num = "";
        string[] linkToPLC = {"toto[{0}].test{1}", "tata[{0}].test{1}", "titi[{0}].test{1}"};
        List<string> genlnkPLCCanvas = new List<string>(linkToPLC);
        List<string> genlnkPLCworkingwith = new List<string>(linkToPLC);

        Console.WriteLine("Insert Num: ");
        Str_Num = Console.ReadLine();
        i_Num = Convert.ToInt32(Str_Num);
        for (int item = 0; item < genlnkPLCCanvas.Count; item++)
        {
            genlnkPLCworkingwith[item] = String.Format(genlnkPLCworkingwith[item], i_Num, 200);
            Console.WriteLine("with List: result= " + genlnkPLCworkingwith[item]);
        }
        //foreach (string item in genlnkPLCCanvas) genlnkPLCworkingwith[item] = String.Format(item, i_Num, 200);
        Console.ReadKey();
    }
}

7 个答案:

答案 0 :(得分:0)

无需重复foreach(第一个,对您的项目没有任何作用)。试试这个:

foreach (var item in genlnkPLC) 
    Console.WriteLine("with List: result= "+ String.Format(item, i_StationNum, 200));

答案 1 :(得分:0)

String.Format()会返回一个字符串,它不会改变您正在格式化的内容。因此,您的第一个foreach (var item in genlnkPLC)会创建立即销毁的临时字符串。

foreach (var item in genlnkPLC) 
{
    Console.WriteLine("with List = " + String.Format(item, i_StationNum, 200));
}

答案 2 :(得分:0)

声明

foreach (var item in genlnkPLC)
    Console.WriteLine("with List: result= "+item);

您没有使用String.Format将参数插入genlnkPLC的成员中,这些成员显然是作为格式字符串。你可以使用

foreach (var item in genlnkPLC)
    Console.WriteLine("without List result = " + String.Format(item, i_StationNum, 200));

代替。

答案 3 :(得分:0)

问题在于您无法更改foreach循环中枚举的元素的引用。 string是一个不可变对象,因此更改它会将旧引用替换为新引用。如果您想更改列表中的元素,您需要在for循环中执行此操作,如下所示:

for (int item = 0; item < genlnkPLC.Count; item++)
            genlnkPLC[item]= String.Format(genlnkPLC[item], i_StationNum, 200);

答案 4 :(得分:0)

如果要修改现有列表,则必须使用for循环而不是foreach一个:

 foreach (var item in list) ...

应改为

 for (int i = 0; i < list.Count; ++i) {
   var item = list[i]; // not necessary, but often convenient
   ...
   list[i] = ... // modification
   ... 
 }

例如

   for (int i = 0; i < genlnkPLCCanvas.Count; ++i) {
     var item = genlnkPLCCanvas[i];

     genlnkPLCCanvas[i] = string.Format(item, i_StationNum, 200);
   } 

在测试时尝试创建报告(将所有逻辑放入单个可读查询中),然后在一次打印出来

   ...

   var withListReport = genlnkPLC
     .Select(item => "with List: result = " + string.Format(item, i_StationNum, 200)); 

   var withoutListReport = genlnkPLC
     .Select(item => "without List: result = " + string.Format(item, i_StationNum, 200));

   // now you can do whatever you want with the reports: 
   //  - print them to console
   //      Console.WriteLine(string.Join(Envrironment.NewLine, withListReport));
   //  - save to file: 
   //      File.WriteAllLines(@"C:\MyFile.txt", withListReport);
   //  - print to, say, WinForm UI: 
   //      MyTextBox.Text = string.Join(Envrironment.NewLine, withListReport);

   Console.WriteLine(string.Join(Envrironment.NewLine, withListReport));
   Console.WriteLine(string.Join(Envrironment.NewLine, withoutListReport));

   Console.ReadKey();

答案 5 :(得分:0)

正如M.Bychenko所说:“如果你想修改现有的列表,你必须使用for循环而不是foreach:”

感谢报告tipp!

class Program
{
    static void Main(string[] args)
    {
        int i_Num = 0;
        string Str_Num = "";
        string[] linkToPLC = {"toto[{0}].test{1}", "tata[{0}].test{1}", "titi[{0}].test{1}"};
        List<string> genlnkPLCCanvas = new List<string>(linkToPLC);
        List<string> genlnkPLCworkingwith = new List<string>(linkToPLC);

        Console.WriteLine("Insert Num: ");
        Str_Num = Console.ReadLine();
        i_Num = Convert.ToInt32(Str_Num);
        for (int item = 0; item < genlnkPLCCanvas.Count; item++)
        {
            genlnkPLCworkingwith[item] = String.Format(genlnkPLCCanvas[item], i_Num, 200);
        }
        var CanvasListReport = genlnkPLCCanvas.Select(item =>  "Canvas List = " + item);
        var WorkingListReport = genlnkPLCworkingwith.Select(item => "Working list = " + item);//string.Format(item, i_Num, 200));
        // now you can do whatever you want with the reports: 
        //  - print them to console
        //      Console.WriteLine(string.Join(Envrironment.NewLine, withListReport));
        //  - save to file: File.WriteAllLines(@"C:\MyFile.txt", withListReport);
        //  - print to, say, WinForm UI: 
        //      MyTextBox.Text = string.Join(Envrironment.NewLine, withListReport)
        Console.WriteLine(string.Join(Environment.NewLine, CanvasListReport));
        Console.WriteLine(string.Join(Environment.NewLine, WorkingListReport));
        Console.ReadKey();
    }
}

答案 6 :(得分:-1)

这是因为

String.Format(item, i_StationNum, 200)

不会更改列表中的字符串。

您必须将String.Format结果分配给您的商品。