Command to close an application of console?

后端 未结 6 464
天涯浪人
天涯浪人 2020-11-28 08:26

I need to close the console when the user selects a menu option.

I tried using close() but it did not work..

how can I do this?

6条回答
  •  情书的邮戳
    2020-11-28 08:46

    return; will exit a method in C#.

    See code snippet below

    using System;
    
    namespace Exercise_strings
    {
        class Program
        {
            static void Main(string[] args)
            {
               Console.WriteLine("Input string separated by -");
    
                var stringInput = Console.ReadLine();
    
                if (string.IsNullOrWhiteSpace(stringInput))
                {
                    Console.WriteLine("Nothing entered");
                    return;
                }
    }
    

    So in this case if a user enters a null string or whitespace, the use of the return method terminates the Main method elegantly.

提交回复
热议问题