正確!謝謝! 但我不明白如何使用foreach ... 目標是每次修改Num時修改列表。 用Canvas List和工作列表的方式是好的編碼好嗎?c#動態修改列表
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();
}
}
感謝
同樣的String.Format不打印,所以你必須做'Console.WriteLine(的String.Format(項目,i_StationNum,200); )' – Sid
哇,4個答案後,仍然沒有答案告訴OP如何更改列表中的字符串。他們都只是告訴如何打印到控制檯。 –