Convert `List` to comma-separated string

前端 未结 6 1731
梦毁少年i
梦毁少年i 2020-12-09 00:33

Is there a fast way to convert List to a comma-separated string in C#?

I do it like this but Maybe there is a faster or more

6条回答
  •  醉酒成梦
    2020-12-09 01:17

    In .NET 4 you don't need the ToArray() call - string.Join is overloaded to accept IEnumerable or just IEnumerable.

    There are potentially more efficient ways of doing it before .NET 4, but do you really need them? Is this actually a bottleneck in your code?

    You could iterate over the list, work out the final size, allocate a StringBuilder of exactly the right size, then do the join yourself. That would avoid the extra array being built for little reason - but it wouldn't save much time and it would be a lot more code.

提交回复
热议问题