C# List to string with delimiter

Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

C# List to string with delimiter

Message par Clippy »

C# List to string with delimiter
Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Re: C# List to string with delimiter

Message par Clippy »

You can use [`String.Join`](http://msdn.microsoft.com/en-us/library/57a79xd0.aspx). If you have a `List` then you can call [`ToArray`](http://msdn.microsoft.com/en-us/library/x303t819.aspx) first:

```
List names = new List() { "John", "Anna", "Monica" };
var result = String.Join(", ", names.ToArray());

```

In .NET 4 you don't need the `ToArray` anymore, since there is an overload of `String.Join` that takes an `IEnumerable`.

In newer versions of .NET different `String.Join` overloads use different approaches to produce the result. And this might affect the performance of your code.

For example, those that accept `IEnumerable` use `StringBuilder` under the hood. And the one that accepts an array uses a heavily optimized implementation with arrays and pointers.

**Results:**

John, Anna, Monica
Répondre

Revenir à « .NET & C# »