9
string FirstName = Console.ReadLine();
            if (FirstName.Length > 12)
            {
                Console.WriteLine(".......................................");
            }
            if(FirstName.Length<3)
            {
                Console.WriteLine("....................");
            }
            Console.WriteLine("...................");
            string SecondName = Console.ReadLine();
            if (SecondName.Length > 12)
            {
                Console.WriteLine(".............................");
            }
            if(SecondName.Length<3)
            {

I want to stop the program if they press enter without putting a value,how to do it??/?

Praveen
  • 55,303
  • 33
  • 133
  • 164
Shalya
  • 93
  • 1
  • 1
  • 6
  • possible duplicate of [How to close a formless C# application](http://stackoverflow.com/questions/181018/how-to-close-a-formless-c-sharp-application) – Harsh Baid Nov 28 '13 at 05:20
  • 1
    This question and your other question indicates you are completely new to C#. Please read some tutorials first. – venerik Nov 28 '13 at 05:20
  • Read more answer here: http://stackoverflow.com/a/4898117/468718 – Harsh Baid Nov 28 '13 at 05:22

3 Answers3

14
string key = Console.ReadKey().ToString();  //Read what is being pressed
if(key == "") {
    Console.WriteLine("User pressed enter!");
    return; //stop further execution
}
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • I mean If "enter" is pressed without any value, what if they press enter after putting a value – Shalya Nov 28 '13 at 05:21
  • I mean to say only if "enter" is pressed without any value,what if they put a value and press enter???? – Shalya Nov 28 '13 at 05:24
  • @user2996625 If you add some value, the above if statement won't get executed. Do you want to stop even if you enter some values? – Praveen Nov 28 '13 at 05:27
8

I think you want to have a non empty string value as an input from console and if input is empty then want to terminate your application. Use following code:

Console.WriteLine("Enter a value: ");
string str = Console.ReadLine();
//If pressed enter here without a  value or data, how to stop the  program here without
//further execution??
if (string.IsNullOrWhiteSpace(str))
  return;
else
{
  Console.WriteLine(string.Format("you have entered: '{0}'", str));
  Console.Read();
}

If user will enter any kind of empty string or white spaces, the application will be terminated the moment he/she will press enter.

Nitin Joshi
  • 1,638
  • 1
  • 14
  • 17
5

Console.ReadLine() returns a string. If nothing is typed and the human just presses the enter key, we'll get an empty string.

There are a number of ways to test whether a string is "empty" for various definitions of empty. Some common definitions of empty and how to test for them:

  • non-null and containing no data: myString.Length == 0
  • null or containing no data: string.IsNullOrEmpty(myString)
  • null, empty, or just whitespace: string.IsNullOrWhiteSpace(myString)

Environment.Exit() will end the process with the exit code you specify.

Combine one of the tests above with Environment.Exit() (and probably an if) and you can stop the process when there's no "value or data".

Also note, that returning from Main is another way to exit the process.

chwarr
  • 6,777
  • 1
  • 30
  • 57