57

I know C# and I like to switch between systems and use Linux. Can I use C# to build applications that will natively work on Linux? What should I do to make use of my knowledge of C# in a Linux system?

Note that I'm specialized in Unity3D, but I still want to create Linux applications using C#.

10
  • 1
    KerbalSpaceProgram is written in Unity and runs on Win/Mac/Linux. Plugins/Mods to KSP are written in C# and can be developed on Win, Mac or Linux. I use primarily Mac now instead of Linux for $REASONS, but I do KSP mod development on that platform in C# and not windows. I'm likely going to write some stand-alone C# code with xUnit tests outside of KSP soon and it'll be just like any other development, but I'll just have to run it under mono. MechJeb has an example Makefile that builds on Mac+Linux: github.com/MuMech/MechJeb2/blob/master/Makefile (requires KSP game dlls to link).
    – lamont
    Apr 5, 2018 at 20:18
  • 3
    C# applications don't usually run natively anywhere.
    – OrangeDog
    Apr 6, 2018 at 11:13
  • @OrangeDog I know, even on Windows you need to install .Net framework. If i said "natively" i mean without any emulation or compatibility layer. I basically mean if Linux support it no matter how many programs or packages you need to install.
    – Jacob
    Apr 6, 2018 at 13:08
  • 2
    What relevance does any of this have to Ubuntu? Apr 6, 2018 at 15:58
  • 2
    @underscore_d I don't know where you heard that, but it's not true. See Are “not only Ubuntu-specific” questions on-topic? Many, many questions on Ask Ubuntu apply equally to the vast majority of GNU/Linux systems. Maybe someone will find a plausible reason to consider this question off-topic, but if so, it won't be that. (Btw, this does have answers with Ubuntu-specific information. The second paragraph of my own post is about Ubuntu packages; it carries over to some, but not all, other distros.) Apr 7, 2018 at 18:17

5 Answers 5

82

Yes, you can develop software on Ubuntu, that itself will run on Ubuntu, in C#. Both Mono and .NET Core support GNU/Linux systems like Ubuntu. (You can use them on other distros, too, like Debian, Raspbian, Fedora, CentOS, Arch, Gentoo, and so forth.) The Unity3D game engine also supports Ubuntu, as you probably know. Unity3D embeds Mono.

A number of packages for Ubuntu, installable with Ubuntu's package manager from officially community-supported software sources, are written in C# and use Mono. This includes the music player Banshee, the notetaking app Tomboy, the raster graphics editor Pinta, and the password manager KeePass (since version 2).

Most text editors, such as Gedit, Vim, and Emacs, have syntax highlighting for C#. MonoDevelop and Visual Studio Code are two popular integrated development environments that run on Ubuntu and support C# development.

8
  • 11
    Maybe also mention Rider, the intellij idea-based IDE for C# that contains all the functionality of ReSharper for Visual Studio.
    – Wilbert
    Apr 5, 2018 at 7:22
  • You may want to tune up this answer so it implies that other distributions of linux do exist. Since they will also provide or support mono, you may want to be a little more neutral and inclusive. Apr 6, 2018 at 14:46
  • @user2066657 Thanks for the suggestion; I've edited. I had meant to make clear how Mono and .NET Core were not limited to Ubuntu ("GNU/Linux systems like Ubuntu"). But on further consideration, I realize this may not have been clear to readers who didn't already know what I was trying to tell them. So I've expanded that part. I do realize this edit may not satisfy you: I've made it more inclusive, but not more neutral. It's reasonable for posts on this site to emphasize Ubuntu. The middle part, giving examples of C# programs with downstream Ubuntu packages, is deliberately focused on Ubuntu. Apr 6, 2018 at 15:36
  • If the OP had wanted to ask about non-Ubuntu OSes, they would probably have asked in Unix & Linux.
    – Mr Lister
    Apr 6, 2018 at 15:42
  • @MrLister Yes, agreed. But I think something is gained, and nothing lost, by the clarification I have added. I don't plan to retool the post as a whole, though. For example, I considered saying something about which distros MonoDevelop and Visual Studio Code are reasonably easy to install and use on, but decided against doing so, on the grounds that it's somewhat complicated and would end up changing the tone and focus (and length!) of the post. Apr 6, 2018 at 15:48
60

Most likely you will stumble upon http://www.mono-project.com/

As the About Mono page says:

Mono, the open source development platform based on the .NET Framework, allows developers to build cross-platform applications with improved developer productivity. Mono’s .NET implementation is based on the ECMA standards for C# and the Common Language Infrastructure.

The supported platforms include Linux.

14
  • 2
    Mono... sound familiar... Oh yeah, this is what is unity based on ! I had no idea that i can use it separately. I thought it is part of Unity3d.
    – Jacob
    Apr 4, 2018 at 18:12
  • 43
    The latest version of Microsoft's implementation (.Net Core) also supports Linux. Apr 4, 2018 at 22:14
  • 6
    Mono implements .net standard 2.0 (iirc) not just core.
    – casey
    Apr 5, 2018 at 3:27
  • 15
    This would have been a nice answer a few years ago. Now it’s pretty much obsolete. Apr 5, 2018 at 15:13
  • 2
    If you are using a recent version of Ubuntu, please please please don't use mono, it has so many bugs and poor implementations, use .net core. Where I work, almost every issue we have when running C# applications on old linux distros can be explained with the statement: "because of mono". Exception filters losing stack traces, async implementations which are Task.Run wrappers, memory leaks all over the shot and lack of valgrind support so you can't even properly diagnose the leak.
    – Lukazoid
    Apr 7, 2018 at 22:50
22
  1. You can now develop server applications on Linux using C# (.NET Core framework), like you can use Java or Python. By server applications I mean web applications and web service (REST) applications mostly. This makes a perfect match with Linux containers (Docker/K8s) and clouds.
  2. You can develop desktop applications with C# using the Mono Framework (a .NET implementation) and GTK# (GtkSharp) toolkit (a wrapper of GTK). There are many projects developed on it. This framework have had some resistance by part of the Linux's community in the past. Some distributions and users didn't want Mono applications.
  3. You can develop games with the Unity Engine using C# on Linux. Unity Editor is experimental on Linux yet.

So, yes. Knowledge on C# can be very useful on Linux systems today.

2
  • 4
    You can also do desktop applications in .NET Core.
    – PmanAce
    Apr 5, 2018 at 15:50
  • You can also use Avalonia, a cross platform WPF re-implementation to do mobile+desktop apps on Mac+Linux+Windows. Finally a non-garbage UI toolkit! Nov 9, 2019 at 6:34
3

You can now even do XAML based front end cross platform (Linux, Mac, Windows, Android, iOS, UWP) apps using .Net Core and a new project called Avalonia. Avalonia is in Beta, but works pretty well now. It is similar to WPF, but with some CSS like enhancements to styling.

I believe on Linux Avalonia targets Gtk currently, but they're wanting to move to something else. IT's mentioned in this video, but I personally couldn't understand what he said: https://www.youtube.com/watch?v=WESJUJWBLJ0

3

Linux provides its native APIs in the C programming language. A native Linux program uses these APIs to access files, I/O devices, sockets (networking), inter-process communications, threading, etc. To create a native Linux app you would write your app in C and then compile it with (most likely) GCC to end up with an executable. You can even go further and use GUI libraries in your program to add GUI (GTK and Qt are two popular ones) or use a packaging system to package your app (like .deb and .rpm files).

C#, F# and VB bring their own compiler (which normally produces IL code instead of machine code) and instead of directly using the Windows or Linux native APIs (both in C), they have their own wrappers around them. This means that there needs to be another extra layer between the compiled code and the OS. This extra layer has to read the IL code and translate that to the native Windows or Linux or macOS APIs. This extra layer can be .NET Framework, Mono or .NET Core (currently just .NET).

Now to answer the question "Is C# usable for Linux system programming?", in most cases yes.

The .NET has libraries for file handling, networking, threading and some I/O devices. Now for example, say you need to access Bluetooth in you app. .NET does not have any APIs for Bluetooth, so in such cases you have two options:

  1. Find the native API in C and call it in C#
  2. Search in NuGet to see if someone has already done it

Other examples would be WiFi Direct, Gamepad, CPU temperature, Battery Info, Camera, GPS, Laptop Sensors, etc. So for low-level apps you're on your own (this is the case even on Windows unless you go with UWP). For such apps on Linux, C or Python would be a way better choice.

If you want to add GUI to your app, .NET has GUI libraries but only for Windows. Mono on the other hand has bindings for GTK called GTK# but naturally GTK# would always be behind the GTK development (which is not a problem unless you want the latest features).

.NET 6 has another solution for cross-platform GUI. In .NET 6 you can create a Blazor app with C#, HTML and CSS and use Electron to create a desktop app from it (as of writing this, it's not ready yet).

To sum up:

Pros of C# on Linux

  • You'd use your existing skills
  • If you stick to .NET libraries your app would be portable to Windows and mac
  • You're dealing with a modern clean language instead of a 50-year-old one (hardcore C programmers may disagree :D)

Cons of C# on Linux

  • Many native APIs are not available
  • On paper native C code would be faster than framework-dependent C# code. Although it's possible to directly compile for Linux but we'd loose portability and with today's hardware users wouldn't feel the difference anyway.
  • Your hands are tighter for a GUI app
3
  • How does this answer the question in any way? I don't think anyone asked about P/Invoke May 11, 2021 at 17:01
  • 1
    I'd say not supporting every conceivable OS API is SOP for any language, so don't put that in 'cons.' Otherwise great synopsis! And thanks for mentioning P/Invoke, since it is important to usefulness. Aug 22, 2021 at 13:33
  • What does, "your hands are tighter" mean?
    – localhost
    Nov 1, 2022 at 14:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .