-1

I have created a small project . and here is it:

class Program
{
   public static void Main()
    {
        Console.WriteLine("Welcome");
        Console.WriteLine("1 to go to Data Files ");
        Console.WriteLine("type quit to exit");
        string input = Console.ReadLine();
        if (input == "1")
        {
            Data go = new Data();
        }
        else if (input == "quit")
        {

        }
    }
}

When user type quit. I want my program to exit. anyone help me, please

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Jhon
  • 17
  • 1
    Possible duplicate of [How do I specify the exit code of a console application in .NET?](http://stackoverflow.com/questions/155610/how-do-i-specify-the-exit-code-of-a-console-application-in-net) – AntiTcb Apr 26 '16 at 16:11
  • program will exist automatically when the main method ends. you don't need to do anything. – Zohar Peled Apr 26 '16 at 16:11
  • Just return from `Main` – Sean Apr 26 '16 at 16:12
  • but when i typed any words. it's also quit from my application. – Jhon Apr 26 '16 at 16:19
  • if you want the application to not quite after typing then you need to issue a `Console.Read()` or a Console.ReadLine()` then put logic to either call the Main Method again or put your code into another method it's doing exactly what's expected based on what you have written.. – MethodMan Apr 26 '16 at 16:21
  • @AlexGravely This is not a duplicate. That linked question deals with exit codes. This question simply wants to exit the application. – Rob Apr 29 '16 at 03:05

2 Answers2

5

You just need something like this:

else if (input == "quit")
{
  return;
}

Update: based on your comments I think you're looking for something like this:

class Program
{
   public static void Main()
    {
        while(true)
        {
                Console.WriteLine("Welcome");
                Console.WriteLine("1 to go to Data Files ");
                Console.WriteLine("type quit to exit");
                string input = Console.ReadLine();
                if (input == "1")
                {
                    Data go = new Data();
                }
                else if (input == "quit")
                {
                   return;
                }
                else
                {
                  Console.WriteLine("invalid option");
                }
        }
    }
}
Sean
  • 60,939
  • 11
  • 97
  • 136
  • But when i typed my name , it's also quit my application. – Jhon Apr 26 '16 at 16:17
  • @Jhon - that because neither condition matched and you got to the end of the function and exited that way. If you want to keep getting input then you need to put the code in a `while` loop. – Sean Apr 26 '16 at 16:19
  • @Jhon - I've added some code which I think does what you want. – Sean Apr 26 '16 at 16:22
  • @Jhon: Try hand tracing your code. You are showing that you are completely unaware of all the possible code paths it can take. – AntiTcb Apr 26 '16 at 16:31
  • @Sean. Thanks. Sorry I'm still beginner. – Jhon Apr 26 '16 at 16:32
2

You just can use Environment.Exit(code). where code is integer representation of standard application-exit-codes like return 0 or return 1 in world of C++.

Iskander Raimbaev
  • 1,322
  • 2
  • 17
  • 35