1
我有一個Visual Studio 2008 C#.NET 3.5應用程序,其中有一個由分號分隔的數字列表。將字符串轉換爲整數列表
string num_list = "1;2;3;4;201;2099;84"
我想將其轉換爲List<int>
。有沒有比這更簡單的方法?
List<int> foo = new List<int>();
foreach (string num in num_list.Split(';'))
foo.Add(Convert.ToInt32(num));
感謝, PaulH
你們能不能簡化 「選擇」'選擇(Convert.ToInt32)'? – Crisfole
@Cpfohl我不確定那是否會編譯。 –
@Cpfohl - 當我嘗試的時候,我得到一個錯誤,說'方法的類型參數...不能從用法推斷。嘗試明確指定類型參數。' – PaulH