Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีแปลง List เป็น String ใน C#

ประกาศ List และเพิ่มข้อมูล:

List<string> l = new List<string>();
l.Add("Accessories");
l.Add("Footwear");
l.Add("Watches");

แปลงเป็น String ด้วย string.Join:

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

ตัวอย่าง

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