How to join ArrayList elements that convert it to a string representation?

I have an ArrayList and connect all its elements with a separator on the same line i m using ...

Dim s As String = String.Join(",", TryCast(myArrayList.ToArray(GetType(String)), String()))

however, I would know if there is a smarter / shorter method to get the same result, or the same code that looks better ...

Thanks in advance,

Max

+3
source share
3 answers

In Framework 4, this is very simple:

var s = string.Join(",", myArrayList);

In 3.5 using LINQ extension methods:

var s = string.Join(",", myArrayList.Cast<string>().ToArray());

They are shorter, but not smarter.

I have no idea how they should be written using VB.NET.

+4
source

, , , , VB.Net, :

Private Function MakeCsvList() As String
  Dim list As New List(Of String)
  list.Add("101")
  list.Add("102")

  Return Strings.Join(list.ToArray, ",")
End Function
+3

I would make it an extension method ArrayListfor example.

public static string ToCsv(this ArrayList array)
{
    return String.Join(",", TryCast(array.ToArray(GetType(String)), String()))
}

Using

string csv = myArrayList.ToCsv();
+2
source

Source: https://habr.com/ru/post/1734896/


All Articles