10

I have a C# code which I need to compile, execute and decompile using the terminal.

How can I do so?

1

4 Answers 4

16

You need to install mono-complete if you want to run software for Mono or Microsoft .NET which you are not installing from a Debian package.


  1. Install mono-complete. In all currently supported versions of Ubuntu open the terminal and type:

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

    using System;
    
    namespace Project_1 {
        class MainClass {
            public static void Main (string[] args) {
                Console.WriteLine ("Hello World!");
                Console.ReadKey ();
            }
        }
    }
    
  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.

  9. Decompile the executable file.

    monodis --output=decompiled-hello.txt hello.exe
    
4
  • I don't think you have to perform Step 8
    – Sonevol
    Dec 13, 2018 at 6:17
  • 1
    But I tested too and I did not need the Step 8, Oh ok I am sorry I did not put Console.ReadKey();
    – Sonevol
    Dec 13, 2018 at 6:20
  • My bad for not including the sample hello.cs code that I used in Step 2 which I have edited my answer and included
    – karel
    Dec 13, 2018 at 19:31
  • why use Mono in 2018 when the official .NET Core has been available for Linux since 2016?
    – phuclv
    Sep 29, 2021 at 3:42
9

You can use mono which is C# implementation, having cross-platform support and is open source.

Open terminal and install mono:

For Ubuntu 20.04 (Stable)

sudo apt install gnupg ca-certificates
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb https://download.mono-project.com/repo/ubuntu stable-focal main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
sudo apt update

For Ubuntu 18.04

sudo apt install apt-transport-https dirmngr
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb https://download.mono-project.com/repo/ubuntu vs-bionic main" | sudo tee /etc/apt/sources.list.d/mono-official-vs.list
sudo apt update

For Ubuntu 16.04

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
sudo apt install apt-transport-https
echo "deb https://download.mono-project.com/repo/ubuntu vs-xenial main" | sudo tee /etc/apt/sources.list.d/mono-official-vs.list
sudo apt update

For Ubuntu 14.04

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
sudo apt install apt-transport-https
echo "deb https://download.mono-project.com/repo/ubuntu vs-trusty main" | sudo tee /etc/apt/sources.list.d/mono-official-vs.list
sudo apt update

Then type

sudo apt install mono-complete

Create a sample C# file in the current directory

For example you can use the following code:

class GoodDay
{
    public static void Main()
    {
        System.Console.WriteLine("Good Day!");
    }
}

Use any text editor like gedit, type the following code and save the file as GoodDay.cs

The command to compile the code -

mcs -out:GoodDay.exe GoodDay.cs

An executable file GoodDay.exe is generated.

The command to execute the .exe file -

mono GoodDay.exe

The output will be -

Good Day!

The command to decompile the executable file -

monodis --output=GoodDay.txt GoodDay.exe

The decompiled code information is saved in the newly generated file GoodDay.txt

2

The official .NET Core from Microsoft has been around since 2016. It's open source software and supports many platforms. Even Mono now shares some code with .NET Core so there's almost no reason to use Mono in 2018

Just go to the official website to download and install. If you want to install from command line then visit Install .NET on Linux, specifically Install the .NET SDK or the .NET Runtime on Ubuntu. To install the SDK and runtime with apt use

sudo apt-get update; \
  sudo apt-get install -y apt-transport-https && \
  sudo apt-get update && \
  sudo apt-get install -y dotnet-sdk-5.0

You can also install with snap

sudo snap install dotnet-sdk --classic --channel=5.0
0

True open source solution as of 2021, is still Mono, available at their site https://www.mono-project.com/ Microsoft's implementation unsurprisingly doesnt work well (slow, unresponsive) on Linux. This is to be expected as Microsoft is a long time nemesis of Linux and its free, open source nature. Stick with Mono it is still developed and easily installable. Download Instructions on included link

1
  • Because I posted the top upvoted answer on this page, I've tried various iterations of .NET Core as an alternative to Mono, but I still haven't gotten it to work well in Ubuntu.
    – karel
    Oct 25, 2021 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.