为什么控制台窗口立即关闭不显示我的输出? [英] Why is the console window closing immediately without displaying my output?

查看:447
本文介绍了为什么控制台窗口立即关闭不显示我的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经决定学习一些基本的C#(虽然我的经验,当涉及到编程)的事情是我下面MSDN中的指南,但这里的问题是,他们的Hello World程序的显示出来,然后它会立即关闭。这是为什么?

 使用系统;

公共类Hello1
{
    公共静态INT主要()
    {
        Console.WriteLine(你好,世界!);
        返回0;
    }
}
 

解决方案
  

这里的问题是,他们的Hello World程序的显示出来,然后它会立即关闭。
  这是为什么?

因为它完成。:当控制台应用程序已经执行完毕,并从他们的方法返回时,相关的控制台窗口会自动关闭。这是正常现象。

如果你想保持它打开调试的目的,你需要指示计算机等待一个关键preSS结束应用程序,并关闭窗口前。

的<一个href="http://msdn.microsoft.com/en-us/library/system.console.readline.aspx"><$c$c>Console.ReadLine方法是这样做的一种方式。添加此行到您的code(在返回之前语句)最终会导致应用程序等待你preSS的主要退出之前。

另外,你可以启动应用程序,而不附加由pressing调试器<大骨节病>控制 + <大骨节病> F5 从Visual Studio环境中,但有明显的缺点$ P $使用的调试功能,你编写一个应用程序时,可能希望在您的处置pventing你。

最好的折衷办法可能是只将其包装到preprocessor指令调试应用程序时,调用到Console.ReadLine 方法。是这样的:

 #如果DEBUG
    Console.WriteLine(preSS进入关闭......);
    到Console.ReadLine();
#ENDIF
 

I've decided to study some basic C# (though I have experience when it comes to programming) the thing is I am following the guides in MSDN, but the issue here is that their Hello World Program is showing up then it would immediately close. why is that?

using System;

public class Hello1
{
    public static int Main()
    {
        Console.WriteLine("Hello, World!");
        return 0;
    }
}

解决方案

the issue here is that their Hello World Program is showing up then it would immediately close.
why is that?

Because it's finished. When console applications have completed executing and return from their main method, the associated console window automatically closes. This is expected behavior.

If you want to keep it open for debugging purposes, you'll need to instruct the computer to wait for a key press before ending the app and closing the window.

The Console.ReadLine method is one way of doing that. Adding this line to the end of your code (just before the return statement) will cause the application to wait for you to press a key before exiting.

Alternatively, you could start the application without the debugger attached by pressing Ctrl+F5 from within the Visual Studio environment, but this has the obvious disadvantage of preventing you from using the debugging features, which you probably want at your disposal when writing an application.

The best compromise is probably to call the Console.ReadLine method only when debugging the application by wrapping it in a preprocessor directive. Something like:

#if DEBUG
    Console.WriteLine("Press enter to close...");
    Console.ReadLine();
#endif

这篇关于为什么控制台窗口立即关闭不显示我的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆