Home  >  Article  >  Backend Development  >  How to convert list to string in C#?

How to convert list to string in C#?

王林
王林forward
2023-08-30 23:29:101082browse

如何在 C# 中将列表转换为字符串?

Declare a list.

List < string > l = new List < string > ();

Now, add the elements to the list.

// elements
l.Add("Accessories");
l.Add("Footwear");
l.Add("Watches");

Now convert it to string.

string str = string.Join(" ", l.ToArray());

Let’s see the final code in C# to convert list to string -

Example

using System;
using System.Collections.Generic;
class Demo {
   static void Main() {
      List < string > l = new List < string > ();
      // elements
      l.Add("Accessories");
      l.Add("Footwear");
      l.Add("Watches");
      string str = string.Join(" ", l.ToArray());
      Console.WriteLine(str);
   }
}

The above is the detailed content of How to convert list to string in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete