-2

in my program execute is print a Unknown character what meaning? Where did it come from unknown character? why print it in console. please help... my code is here

static void Main(string[] args)
    {
        char[] array = new char[10];

        for (int i = 0; i < 10; i++){

            array[i] =(char) i;
        }
        for(int i = 0; i < 10; i++)
        {
            Console.Write(array[i] + "\n");

        }
        Console.Read();
    }

it is a simple code but c# not recognize my code after update windows 10 my program become it. maybe is problem from update windows 10 ?

execute my code is

http://uupload.ir/files/5tdi_problem_print_unknow_character.png

but most It should be 1 2 3 4 5 6 7 8 9

Please help me.. tanks. What can I do to not write the irrelevant square? enter image description here

picture from my execute http://s9.picofile.com/file/8312295492/problem_print_unknow_character.png

OR http://uupload.ir/files/5tdi_problem_print_unknow_character.png

muhammad
  • 1
  • 1
  • 2
  • 2
    You are printing unprintable characters, what do you expect to see? – Alex K. Nov 20 '17 at 19:12
  • 2
    It would really help if you explained what you think that code should do, because you are almost certainly wrong. – Dour High Arch Nov 20 '17 at 19:12
  • You are printing items 0-9 in this table. Try looping through some printable characters (like 48-57). http://www.asciitable.com/ – zzxyz Nov 20 '17 at 19:13
  • 1
    Are you trying to print the numbers? If so, change your array to contain an `int` type instead of a `char` type: `int[] array = new int[10]`, and remove the `(char)` cast. This way your array is an array of numbers, not ascii characters. Otherwise, please explain what you're trying to do, because your code is doing exactly what you've asked it to do. – Rufus L Nov 20 '17 at 19:18

2 Answers2

0

by

(char) i;

you are converting an int to char using the ascii. The corresponding ascii for 0 - 10 are unprintable characters.

You could make the array int array instead of char array or you can call i.ToString()[0] (for 0-9 only) to get the desire result

Steve
  • 11,696
  • 7
  • 43
  • 81
0

If you really want it to print 1 2 3 4 5 6 7 8 9 then you need to ensure

  1. your loop starts at 1 not 0,
  2. you use " " not "\n" after each Write and
  3. as others have commented above, you ensure your array is of type int not char (or add '0' to each integer to get the correct ASCII value).

There's no way this program could ever have printed what you expect under any version of Windows (or other O/S).

Dylan Nicholson
  • 1,301
  • 9
  • 23