Open In App

How to Compile, Decompile and Run C# Code in Linux?

Last Updated : 02 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

C# is a modern multi-paradigm programming language developed by Microsoft and released in the year 2000. By multi-paradigm we mean that it includes static typing, strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented, and component-oriented programming disciplines. The syntax of C# is highly inspired by the JAVA syntax, therefore, it is easier to understand for most developers who have some basic knowledge of C, C++, and JAVA. It was designed by  Anders Hejlsberg and developed by Mads Torgersen.

To compile, Decompile and Run C# code in Linux, follow the below-mentioned steps:

Firstly, we need to install mono-complete, to run software for Mono or Microsoft. NET.

Step 1: To Install mono-complete, open up your Linux terminal and type the following command, and hit enter.

Run the following command to set up the system before installing the mono.

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

Then run the following to install mono.

sudo apt install mono-complete

Step 2: Write a simple hello world program in C# and save the code in a file called geeks.cs.

C#




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


Output

Hello World!

Step 3: Now make this C# file an executable file. Navigate to the file and run the following command.

making executable

sudo chmod +x geeks.cs

Here, +x means executable.

Step 4: Now we will be using the MCS compiler and creating a Windows executable named geeks.exe from the source geeks.cs.

mcs -out:geeks.exe geeks.cs

Output:

compiling c# code

After this, an executable file, geeks.cs, will be generated.

Step 5: Now to run this geeks.exe executable file, run the following command.

mono geeks.exe

Output:

Running c# code

Step 5: Press Enter to exit back to a default terminal prompt.

Step 6: To decompile this executable file run the following command:

monodis –output=geeks.txt geeks.exe

Output:

Decompiled c# code

The decompiled code will be saved in the newly generated file, geeks.txt. To view the decompiled file in the terminal, run the following command:

cat geeks.txt

Output should look like this:

The output of decompiled code


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads