c# - Write numbers from 1 to 10, set cursor position to the beginning and restart the writing -


i came strange behavior can't explain. have console application writes numbers 1 9 , sets cursor beginning of console window again. after that, loop goes on , restarts writing numbers 1 10. problem numbers written before overwritten instead of moved forward.

my code better understanding:

int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; (int counter = 0; counter < 10; counter++) {     foreach (int number in numbers)     {         console.write(number + " ");         thread.sleep(5);     }     console.setcursorposition(0, 0); } 

when add line console.write("test"); below console.setcursorposition(0, 0);, following output:

test1 2 3 4 5 6 7 8 9

what didn't expect: when adding console.write(1); below, get

1 2 3 4 5 6 7 8 9

with code wanted achieve following:

1 2 3 4 5 6 7 8 9 [now setting cursor position beginning , moving consisting numbers forward] 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 [and on]

why isn't program behaving this?

obviously when set cursor of console 0,0 start overwriting previous values, because of happen. can strings concatenation, advice use stringbuilder because muttable.

    public static void main(string[] args)     {         stringbuilder sb = new stringbuilder();          int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };         (int counter = 0; counter < 10; counter++)         {             stringbuilder numberbuilder = new stringbuilder();             foreach (int number in numbers)             {                 numberbuilder.append(number + " ");             }              sb.insert(0, numberbuilder.tostring());         }          console.writeline(sb.tostring());     } 

here dotnet fiddle example


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -