C# Syntax - Split String into Array by Comma, Convert To Generic List, and Reverse Order

后端 未结 6 591
Happy的楠姐
Happy的楠姐 2020-12-07 19:49

What is the correct syntax for this:

IList names = \"Tom,Scott,Bob\".Split(\',\').ToList().Reverse();

What am I

6条回答
  •  眼角桃花
    2020-12-07 20:19

    What your missing here is that .Reverse() is a void method. It's not possible to assign the result of .Reverse() to a variable. You can however alter the order to use Enumerable.Reverse() and get your result

    var x = "Tom,Scott,Bob".Split(',').Reverse().ToList()
    

    The difference is that Enumerable.Reverse() returns an IEnumerable instead of being void return

提交回复
热议问题