Ubuntu – Ubuntu terminal not waiting for Console.ReadKey() input

14.04cmonodevelop

I'm using MonoDevelop to write C# (.NET) codes, on Ubuntu Desktop 14.0.5 LTS. While testing a simple Hello World code, I encountered a problem – earlier on windows, I used the Console.ReadKey() statement to make sure that the console doesn't disappear instantaneously after showing the output. However, this trick is not working on Ubuntu. Here's the code, in case you'd need to see it:

using System;

namespace Project_1 {
    class MainClass {
        public static void Main (string[] args) {
            Console.WriteLine ("Hello World!");
            Console.ReadKey ();
        }
    }
}

In spite of the last statement, the console shows up for about 1/100th of a second before disappearing. Precisely, the Console.ReadKey() statement is being ignored(??). I have no idea why this is happening. I'm new to using Ubuntu and installed MonoDevelop a few hours ago.

How do I fix this issue?

Best Answer

You need to install mono-complete instead of Mono development tools (mono-devel) package if you want to run software for Mono or Microsoft .NET which you are not installing from a Debian package.


  1. Install mono-complete

     sudo apt-get install mono-complete
    
  2. Save your C# code in a file called hello.cs.

  3. Make hello.cs executable. Right-click the hello.cs file -> select Properties -> Permissions tab -> put a check mark to the left of Allow executing file as program.

  4. Change directories using the cd command to the directory that contains the hello.cs file.

  5. Use the mcs compiler and create a Windows executable named hello.exe from the source hello.cs.

     mcs -out:hello.exe hello.cs
    
  6. Run the hello.exe program with mono.

     mono hello.exe
    
  7. The results of running your program in step 6. should be:

     Hello World!  
    
  8. Press Enter to exit back to a default terminal prompt.

Run C# program in MonoDevelop

  1. Install MonoDevelop

     sudo apt-get install monodevelop # 17.10 and earlier
    
  2. Open MonoDevelop application.

  3. Create a new project. Select File -> New -> Solution -> .NET -> Console Project -> check the dropdown menu next to Console Project to make sure that C# is selected -> click the Next button -> select a name and directory location for your project -> click the Create button.

  4. In the left pane select Program.cs. Copy your C# code into Program.cs.

  5. Select Build -> Build All.

  6. Click the Run arrow in the upper left corner of MonoDevelop to run the program.

Related Question