Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Executing C# code in Linux
The .NET ecosystem was traditionally limited to Windows, but Microsoft's introduction of Mono changed this landscape. Mono enables the execution of .NET applications on Linux systems, making them run as if they were native Linux packages rather than Windows executable files.
What is Mono?
Mono is an open-source, cross-platform implementation of Microsoft's .NET Framework. It allows developers to run .NET applications on various platforms including Linux and macOS. Mono provides a complete development stack that supports Windows Forms, LINQ, XML web services, ADO.NET, and ASP.NET using the same CLR namespaces.
Installing Mono on Linux
To install Mono on a Linux system, you can use the package manager. For Ubuntu/Debian-based systems −
sudo apt update sudo apt install mono-complete
Alternatively, you can install just the compiler using −
sudo apt-get install mono-mcs
Verify the installation by checking the version −
mono --version mcs --version
Writing and Compiling C# Code
The structure of a Mono-based console application is identical to a traditional .NET console application. Create a simple C# program to test the setup −
Example
using System;
namespace HelloLinux {
class Program {
public static void Main(string[] args) {
Console.WriteLine("Hello from C# running on Linux!");
Console.WriteLine("Current Platform: " + Environment.OSVersion);
Console.WriteLine("Runtime Version: " + Environment.Version);
}
}
}
Save this code as Program.cs. To compile and run the program −
# Compile the C# code mcs Program.cs # Run the compiled executable mono Program.exe
The output of the above code is −
Hello from C# running on Linux! Current Platform: Unix 5.4.0.74 Runtime Version: 4.0.30319.42000
Working with .NET Libraries
Mono supports most .NET libraries and namespaces. Here's an example using common .NET features −
Example
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinuxDemo {
class Program {
public static void Main(string[] args) {
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
Console.WriteLine("Even numbers:");
foreach (int num in evenNumbers) {
Console.Write(num + " ");
}
Console.WriteLine();
Console.WriteLine("Sum of even numbers: " + evenNumbers.Sum());
}
}
}
Compile and run using the same commands −
mcs LinuxDemo.cs mono LinuxDemo.exe
The output of the above code is −
Even numbers: 2 4 6 8 10 Sum of even numbers: 30
Compilation Options
The Mono compiler (mcs) supports various compilation options −
| Option | Description |
|---|---|
-out:filename |
Specify the output executable name |
-target:exe |
Create console application (default) |
-target:library |
Create a .dll library |
-reference:assembly |
Reference external assemblies |
Example with Custom Output Name
mcs -out:MyApp.exe Program.cs mono MyApp.exe
Conclusion
Mono enables seamless execution of C# applications on Linux systems, providing full compatibility with .NET libraries and frameworks. With simple installation and familiar compilation commands, developers can easily deploy .NET applications across multiple platforms without modifying their existing codebase.
