-1

How can print number only 6-digit start from 000000-999999 by C#

string st = "";
for (int i = 0; i <= 999999; i++)
{
    st += i.ToString("000000") + "\n";                
}
MessageBox.Show(st);
maccettura
  • 10,514
  • 3
  • 28
  • 35

1 Answers1

3

I'm not sure what issue you're facing with your code, but you might want to consider using a StringBuilder instead of a string. Every time you add to a string you're actually getting a new instance of the string and that many new instances can eat up some memory. Whereas a StringBuilder will change itself, instead of creating new instances. See Difference between string and StringBuilder

using System;
using System.Text;

public class Program
{
    public static void Main()
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i <= 999999; i++)
        {
            sb.AppendLine(i.ToString("000000"));
        }

        Console.WriteLine(sb);
    }
}
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
  • this one its work , but take time to print one-by-one – islam kabaha Jul 21 '18 at 07:02
  • @Fabio It's clearly explained in my answer and in the link https://stackoverflow.com/questions/3069416/difference-between-string-and-stringbuilder-in-c-sharp There will be less memory consumption with a performance increase. – Shar1er80 Jul 21 '18 at 12:18
  • @Fabio how do you even know what OP wants, it’s not clear at all. – maccettura Jul 21 '18 at 14:04
  • @maccettura, I don't, but I know that OP didn't ask about how to improve performance. And output of OP's code and output of this answer will be exactly same. – Fabio Jul 21 '18 at 20:24