1

When I selected folder Im printing this path to my TextBlock (WPF):

folderName = dialog.SelectedPath.ToString();
tbArea = "Selected Path: " + dialog.SelectedPath.ToString() + "\r\n";

As probably I will use this more then one time, I have created method:

 public void addToTextArea(string[] newString)
 {
  tbArea = tbArea + newString + "\r\n";
 }

Now Im doing like this:

 string[] arr = {"Selected Path", dialog.SelectedPath.ToString()};

 addToTextArea(arr);

But as result Im getting this: System.String[].

What is wrong or missing?

1
  • The default .ToString() behavior in .NET is to return the full name of the type. So technically nothing is wrong - you just need to tell .NET how to combine the elements of the array into a single string. Commented Jun 17, 2019 at 21:14

1 Answer 1

3

If you want to join items in an array with some common string in between them, you can use the string.Join method (note I'm using Environment.NewLine instead of \r\n because it's more platform-friendly):

public void AddToTextArea(string[] newStrings)
{
    tbArea += string.Join(Environment.NewLine, newStrings) + Environment.NewLine;
}

The string.Join method takes in a string to conacatenate the items with and then returns a string containing all the items joined with the specified string. A more common example is:

int[] items = {1,2,3,4,5};

Console.WriteLine(string.Join(", ", items));

// Output: "1, 2, 3, 4, 5"

Note that there is no leading or trailing connecting string (", ") added to the list, so in your case we add a newline character to the end.


An alternative would be to create an overload that takes in a single string, and then call that method for each item in the string array:

public void AddToTextArea(string[] newStrings)
{
    foreach (string newString in newStrings)
    {
        AddToTextArea(newString);
    }
}

public void AddToTextArea(string newString)
{
    tbArea = tbArea + newString + Environment.NewLine;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.