Split string, convert ToList() in one line

后端 未结 10 1923
借酒劲吻你
借酒劲吻你 2020-11-27 10:00

I have a string that has numbers

string sNumbers = \"1,2,3,4,5\";

I can split it then convert it to List

         


        
10条回答
  •  忘掉有多难
    2020-11-27 10:37

    You can use new C# 6.0 Language Features:

    • replace delegate (s) => { return Convert.ToInt32(s); } with corresponding method group Convert.ToInt32
    • replace redundant constructor call: new Converter(Convert.ToInt32) with: Convert.ToInt32

    The result will be:

    var intList = new List(Array.ConvertAll(sNumbers.Split(','), Convert.ToInt32));
    

提交回复
热议问题