7

I write a class in C# and I want to display in console. But i can't display it.

My program doesn't have any errors, which means the program runs it but i can't see a result :(

Please help me regarding this problem.

Tilak
  • 30,108
  • 19
  • 83
  • 131
Hamid
  • 89
  • 1
  • 1
  • 5

5 Answers5

21

You can use Console.WriteLine(Object) to print the output, and Console.Read() to wait for user input.

Console.WriteLine("Hello world");
Console.WriteLine("Press any key to exit.");
Console.Read();

Without Console.Read, sometimes output just comes, and program exits in a flash. So Output cannot be seen/verified easily.

Tilak
  • 30,108
  • 19
  • 83
  • 131
2

No Problem, I get what you are asking...

In regards to Microsoft Visual Studio 2010

  1. Write Console.WriteLine("Where are you console?"); somewhere in your code - make sure you will definitely see it..

  2. Press Debug button (or the play button)

  3. In the Microsoft Visual Studio, Go to Debug -> Windows -> Output

A small 'Output' Windows should pop up and you can see your console write statements! - Obviously you gotta run the code and it will appear.

Hope it helps!

jonprasetyo
  • 3,356
  • 3
  • 32
  • 48
1

Press CTRL+F5 to see your output. This will wait the console screen until you press any key.

Javed Akram
  • 15,024
  • 26
  • 81
  • 118
0

On MSDN you can find basic guide how to create console applications and how to output results.

kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
0

Your main structure must be in "Windows Form" architecture. So, try to attach parent (base) process, such as:

namespace MyWinFormsApp
{
    static class Program
    {
        [DllImport("kernel32.dll")]
        static extern bool AttachConsole(int dwProcessId);
        private const int ATTACH_PARENT_PROCESS = -1;

        [STAThread]
        static void Main(string[] args)
        {
            if (Environment.UserInteractive) // on Console..
            {
                // redirect console output to parent process;
                // must be before any calls to Console.WriteLine()
                AttachConsole(ATTACH_PARENT_PROCESS);

                // to demonstrate where the console output is going
                int argCount = args == null ? 0 : args.Length;
                Console.WriteLine("nYou specified {0} arguments:", argCount);
                for (int i = 0; i < argCount; i++)
                {
                    Console.WriteLine("  {0}", args[i]);
                }
            }
            else
            {
                // launch the WinForms application like normal
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
}

(or write a console app from scratch)

Emre Guldogan
  • 590
  • 9
  • 19