Data Manipulation Practice 1 .ToArray

Hey Guys, I am a newbie with programming so apologies is this is a silly question. In practice 1 of the Data Manipulation foundation learning, it was asked to print out the first three elements from a list of strings in reverse alphabetical order. To output the required list, I had String.Join(“,”,countryListReverse) and it printed out the required list. However, in the solution it had String.Join(“,”,countryListReverse.ToArray). I’m trying to understand why the .ToArray function is either necessary or good practice?

2 Likes

The overloaded function you are using is String.Join(String, IEnumerable<String>) which was introduced in .NET Framework 4.0. If you want your code to work in older version, the string array version is preferred.

However, UiPath Studio requires at least version 4.6.1 of .NET Framework to run. So I don’t consider converting the list to an array necessary or good practice anymore. If the function is there, why not use it?

Join(String, IEnumerable) is a convenience method that lets you concatenate each element in an IEnumerable(Of String) collection without first converting the elements to a string array. It is particularly useful with Language-Integrated Query (LINQ) query expressions.

2 Likes

Thank you! That explains it perfectly :blush: