0

I would like to output a list (C# Scripting), which contains elements of objects' names. Since this list is generating dynamacally, I just get a output in every single line. This is my code:

public static List<string> myList= new List<string>();
//...
public static void myMethod (Renderer[] renderers) {
        foreach (var renderer in renderers) {
            if (IsVisible(renderer)) {
                //print (renderer.name + " is visible!");
                myList.Add (renderer.name);
            }
            print (myList);
        }
        myList.Clear ();
        print ("--");
       }

The output is like System.Collections.Generic.List'1[System.String]in every line. After a reseach I also tried

foreach (var item in myList) {
      print (item);
} 

This method prints every object in a single line. But I want all names of the objects to be together in one line until there will be "--" printed. How can I realize it? Thanks!

Example:

output now:

object0 
object1
object2
etc.

what I want to achive: object0, object1, object2, etc.

6
  • what's this print ? Console.Write ? What type of application is this winforms/console/web ? Commented Jul 13, 2017 at 9:59
  • Is print a function you have written? Commented Jul 13, 2017 at 10:00
  • From msdn.microsoft.com/fr-fr/library/57a79xd0(v=vs.110).aspx Console.Writeline(string.Join(",", myList.ToArray()); Commented Jul 13, 2017 at 10:02
  • @JuanFerrer print comes from using System.Collections.Generic; Commented Jul 13, 2017 at 10:02
  • @Krishna it's like Debug.Log(), I'm developing in MonoDevelop for the game engine Unity3D. Commented Jul 13, 2017 at 10:04

4 Answers 4

1

You can print all the names of visible renderers with the following code using String.Join and LINQ

public static void myMethod(Renderer[] renderers)
{
    print(string.Join(",", renderers.Where(IsVisible).Select(r => r.name)));
    print("--");
}

This should output:

object1,object2,object3
--
Sign up to request clarification or add additional context in comments.

2 Comments

A very smart solution, thnak you!
@Viktoria BTW your error was in calling print (myList); inside the foreach loop. You should have done it after the loop, when all items were already added, You could either put it after or change that line to Print(renderer.name);
1

You can use String.Join method. It recevies the separator and an array of strings as parameters. For example:

print(String.Join(", ", myList.ToArray());

2 Comments

This works nearly perfect. But now I get an output like object0 (newline) object0, object1 (newline) object0, object1, object2 ...
do not execute the print for each element. You need to execute the print at the end of the loop, once the list is filled
1

If your output coming as a string then u have to take one string variable and concatenate all items with "--" into the variable.

string temp = string.Empty;
foreach(var item in items)
{
   temp += item + "--";
}

print(temp)

Comments

0

You can use string.Joinfor this.

string itemsInOneLine = string.Join(", ", myList);
print(itemsInOneLine);

For further information have a look at the MSDN string.Join page.

3 Comments

I'm using a List<string> myList, not an Array
and Join needs as a second parameter an array.
string.Joinsupports List<string ;) Have a look at the MSDN, there is an overload: Join<T>(String, IEnumerable<T>) and List<string> is an IEnumerable

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.