Printing lines of "*" of various lengths As a beginner, I tried a programming drill.  The method gets an Enumerable that contains numbers specifying how many "*" to print on each line.

public static void PrintStars(IEnumerable&lt;int&gt; neverEndingListOfInt)
{
    foreach (int item in neverEndingListOfInt)
    {
        Console.WriteLine(SetStars(item));
    }
}
private static string SetStars(int numberOfTimes)
{
    return string.Concat(Enumerable.Repeat("*", numberOfTimes));

}


I have another version here:

public static void PrintStars(IEnumerable&lt;int&gt; neverEndingListOfInt)
{

    foreach (int item in neverEndingListOfInt)
    {
        for (int i = 0; i &lt; item; i++)
        {
            Console.Write("*");
        }
        Console.WriteLine();

    }
}


What impact on the GC will creating so many strings have?
Which way is more efficient, and how would you prefer to do it?
I don't consider myself an expert when it comes to the internals of .NET's garbage collector, but as far as I understand:


Write("*") benefits from string interning, so there should be little to no GC overhead (other than what Write does internally, of course). However, Write has to be called for every single character, and that call overhead makes it (significantly) slower.
The WriteLine variant does allocate strings (among other things), so the GC will have to run more often. Whether that has any significant impact on performance is something you'll have to measure, but it's normally not something you need to worry about.
new string('*', numberOfTimes) is more efficient (both in terms of speed and in terms of allocations) than string.Concat(Enumerable.Repeat("*", numberOfTimes)).
You may run into different kinds of issues if you allocate a lot of very large objects (larger than 85.000 bytes), due to how the Large Object Heap works. But that's probably not relevant in this case.


Note that new .NET releases occasionally contain GC improvements, so while the general principles are probably fairly static, implementation details will change over time.