How to keep console window open

2019-01-06 21:41发布

When I run my program, the console window seems to run and close. How to keep it open so I can see the results?

class Program
{
    public class StringAddString       
    {                        
        public virtual void AddString()
        {
            var strings2 = new string[] { "1", "2", "3", "4", "5","6", "7", "8", "9"};

            Console.WriteLine(strings2);
            Console.ReadLine();
        }
    }

    static void Main(string[] args)
    {          
        StringAddString s = new StringAddString();            
    }
}

9条回答
Bombasti
2楼-- · 2019-01-06 22:37
Console.ReadKey(true);

This command is a bit nicer than readline which passes only when you hit enter, and the true parameter also hides the ugly flashing cursor while reading the result :) then any keystroke terminates

查看更多
闹够了就滚
3楼-- · 2019-01-06 22:38

Write Console.ReadKey(); in the last line of main() method. This line prevents finishing the console. I hope it would help you.

查看更多
相关推荐>>
4楼-- · 2019-01-06 22:43

You forgot calling your method:

static void Main(string[] args)
{          
    StringAddString s = new StringAddString();  
    s.AddString();
}

it should stop your console, but the result might not be what you expected, you should change your code a little bit:

Console.WriteLine(string.Join(",", strings2));
查看更多
登录 后发表回答