Unable to print new list created from oldlist.GetRange()

Hi there,

I am currently doing the RPA Developer Foundation course. My question is based on the data manipulation module, practice 1 - Lists. Please find my question at the bottom of the page, below is information on where my question originates from.

This is the task:

Sort the List and Print 3 Values

Given an input list of countries please sort the list and print the first 3 values in descending order.

Note: Please use this value for the List initialization: new List(of String) from {“Germany”, “Spain”, “Japan”, “Brazil”, “India”, “China”}.

This is the solution:

  1. Start the project as a sequence and define a L ist of Strings variable (“InputData”) with the following default value: new List(of String) from {“Germany”, “Spain”, “Japan”, “Brazil”, “India”, “China”}.
  2. Add an ’ Invoke Method ’ activity. Write ’ Sort ’ under MethodName and “InputData” under TargetObject. Leave the TargetType unchanged (null).
  3. Add one more ’ Invoke Method ’ activity. Write ’ Reverse ’ under MethodName and have the other 2 fields filled in just like in the previous ‘Invoke Method’
  4. Create a new List of String variable.
  5. Add an ‘Assign’ activity to the Designer panel.
  6. In the To field, enter the variable created at step 4.
  7. In the Value field, enter the name of the original variable, followed by .GetRange(0,3). This variable stores the first three values in the sorted list.
  8. Add a ‘Write Line’ activity. This will print the values.
  9. Use the ‘String.Join’ method: String.Join (", ", secondvariablename .ToArray) in the text of the ‘Write Line’ activity.

My question:
For the last step (step 9.), I don’t understand why we have to convert the secondvariablename which is the new list we created into an array in order to print the 3 items. When we used the assign activity and do secondvariablename = firstvariablename.GetRange(0,3), I thought we already populated secondvariablename with the three items and stored them as a list. Why do we have to use the .ToArray method, why doesn’t the String.Join method work on the secondvariablename which is already a list?

Thanks in advance for answering my question.

In older version of .NET Framework, String.Join only worked with a string array. In version 4.0, Microsoft introduced a new overloaded function which could also handle other collection types (including list). So it’s not necessary to convert to an array anymore.

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.