How to use the join method in a list in C#?

In C#, the List class does not have a built-in Join method. However, you can achieve similar functionality using LINQ. Here is an example of using LINQ:

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<string> list = new List<string> { "Hello", "World", "C#" };

        // 使用LINQ的Join方法将List中的元素连接成一个字符串
        string result = string.Join(" ", list);

        Console.WriteLine(result); // 输出: Hello World C#
    }
}

In the example above, we utilized the Join method in LINQ, which takes two parameters: a separator and an enumerable collection. Here, we concatenated the elements in the List into a string separated by spaces.

广告
Closing in 10 seconds
bannerAds