Run C# code on linux terminal

Of course it can be done and the process is extremely simple.

Here I am explaining the steps for Ubuntu Linux.

Open terminal:

Ctrl + Alt + T

Type

gedit hello.cs

In the gedit window that opens paste the following example code:

using System;
class HelloWorld {
  static void Main() {
    Console.WriteLine("Hello World!");
  }
}

Save and close gedit.

Back in terminal type:

sudo apt update
sudo apt install mono-complete
mcs -out:hello.exe hello.cs
mono hello.exe

Output:

Hello World!

NOTE: @adabru's answer below makes my solution obsolete unless you are using an older mono platform.

C# scripts can be run from the bash command line just like Python and Perl scripts, but it takes a small bit of bash magic to make it work. As Corey mentioned above, you must first install Mono on your machine. Then, save the following code in an executable bash script on your Linux machine:

if [ ! -f "$1" ]; then
dmcs_args=$1
shift
else
dmcs_args=""
fi
script=$1
shift
input_cs="$(mktemp)"
output_exe="$(mktemp)"
tail -n +2 $script > $input_cs
dmcs $dmcs_args $input_cs -out:${output_exe} && mono $output_exe $@                                                                          
rm -f $input_cs $output_exe

Assuming you saved the above script as /usr/bin/csexec, an example C# "script" follows:

#!/usr/bin/csexec -r:System.Windows.Forms.dll -r:System.Drawing.dll                                                                   
using System;                                                                                                                                
using System.Drawing;                                                                                                                        
using System.Windows.Forms;                                                                                                                  
public class Program                                                                                                                         
{                                                                                                                                            
    public static void Main(string[] args)                                                                                                     
    {                                                                                                                                          
        Console.WriteLine("Hello Console");                                                                                                      
        Console.WriteLine("Arguments: " + string.Join(", ", args));                                                                              
        MessageBox.Show("Hello GUI");                                                                                                            
    }                                                                                                                                          
}                                                                                                                                            

Save the above code to a file such as "hello.cs", make it executable, change the first line to point to the previously saved bash script, and then execute it, you should see the following output along with a dialog saying "Hello GUI":

bash-4.2$ ./hello.cs foo bar baz
Hello Console
Arguments: foo, bar, baz

Note that the GUI requires that you be at run level 5. Here is a simpler C# script that runs at a pure text console:

#!/usr/bin/csexec                                                                                                                     
using System;                                                                                                                                
public class Program                                                                                                                         
{                                                                                                                                            
    public static void Main(string[] args)                                                                                                     
    {                                                                                                                                          
        Console.WriteLine("Hello Console");                                                                                                      
        Console.WriteLine("Arguments: " + string.Join(", ", args));                                                                              
    }                                                                                                                                          
}                                                                                                                                            

Notice that the command line arguments are passed to the C# script, but the shebang arguments (in the first C# script above "-r:System.Windows.Forms.dll -r:System.Drawing.dll") are passed to the C# compiler. Using the latter functionality, you can specify any compiler arguments you require on the first line of your C# script.

If you are interested in the details of how the bash script works, shebang (#!) lumps together all arguments passed to it on the first line of the C# script, followed by the script name, followed by command line arguments passed to the script itself. In the first C# example above, the following 5 arguments would be passed into the bash script (delineated by quotes):

"-r:System.Windows.Forms.dll -r:System.Drawing.dll" "hello.cs" "foo" "bar" "baz"

The script determines that the first argument is not a filename and assumes it contains arguments for the C# compiler. It then strips off the first line of the C# script using 'tail' and saves the result to a temporary file (since the C# compiler does not read from stdin). Finally, the output of the compiler is saved to another temporary file and executed in mono with the original arguments passed to the script. The 'shift' operator is used to eliminate the compiler arguments and the script name, leaving behind only the script arguments.

Compilation errors will be dumped to the command line when the C# script is executed.


The #! (hashbang) tag is used to tell the shell which interpreter to use so that your perl, php, bash, sh, etc. scripts will run right.

But C# is not a scripting language, it is intended to be compiled into an executable format. You need to install at least a compiler and runtime if you want to use C#, and preferably an IDE (Integrated Development Environment) to help you develop and debug your applications.

Install Mono for the compiler and runtime, then MonoDevelop for the IDE.

Tags:

Linux

C#

Shell