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#.

apaderno
  • 289
  • 3
  • 14
Jacob
  • 1,120
  • 3
  • 12
  • 25
  • 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: https://github.com/MuMech/MechJeb2/blob/master/Makefile (requires KSP game dlls to link). – lamont Apr 05 '18 at 20:18
  • 3
    C# applications don't usually run *natively* anywhere. – OrangeDog Apr 06 '18 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 06 '18 at 13:08
  • 2
    What relevance does any of this have to Ubuntu? – underscore_d Apr 06 '18 at 15:58
  • @underscore_d Well, Ubuntu is linux system and i'm going to install it. I was worried my knowledge of C# become useless so i asked here. – Jacob Apr 06 '18 at 17:19
  • I'd like to answer somewhat tangentially that your C# experience will probably help you learn more *nix-specific tools like shell scripting. From experience, learning to code in one language helps you to learn others. – Nic Apr 07 '18 at 06:31
  • @NicHartley Yeah, i know it pretty well because i learned C# on top of GML, a language useful only in tool GameMaker. But that is enough for me. If i go for more languages at once I will learn less of them all. That is why i want to focus on one language and master it to perfection. I choosed C# 4 years ago because i felt limited by GML, and GML itself is C-based language so switch to C# was not so difficult. – Jacob Apr 07 '18 at 14:10
  • I mean direct relevance, not vague/tangential relevance. This SE site exists for questions that are specifically about Ubuntu or at least influenced enough by its particular properties that it's relevant. I don't think Ubuntu has any special properties wrt C# that make this an on-topic question. – underscore_d Apr 07 '18 at 14:49
  • @underscore_d Oh, i see. I thought some Ubuntu - specific steps must be done in order to create or run C# applications. Looks like here are no tasks specific to Ubuntu, yet. Let it be here, maybe someone come with something specific to Ubuntu. – Jacob Apr 07 '18 at 18:06
  • 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?](https://meta.askubuntu.com/questions/14523/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.) – Eliah Kagan Apr 07 '18 at 18:17

5 Answers5

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.

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
  • 11
    Maybe also mention Rider, the intellij idea-based IDE for C# that contains all the functionality of ReSharper for Visual Studio. – Wilbert Apr 05 '18 at 07: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. – user2066657 Apr 06 '18 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. – Eliah Kagan Apr 06 '18 at 15:36
  • If the OP had wanted to ask about non-Ubuntu OSes, they would probably have asked in [Unix & Linux](https://unix.stackexchange.com/questions). – Mr Lister Apr 06 '18 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. – Eliah Kagan Apr 06 '18 at 15:48
  • The Avalonia project also allows you to do XAML based front end development on all desktop and mobile platforms, too. On Linux it targets Gtk for now, though they want to change that. – Chris Bordeman May 10 '18 at 04:19
  • Just FYI, Unity uses .Net Core these days, NOT Mono (which is deprecated). Its new DOTS platform also uses a subset of C# features that removes the garbage collector and classes in favor of a Data Oriented programming paradigm that gives such extreme performance you have to see it to believe. – Chris Bordeman Nov 09 '19 at 06:30
  • You can even use Avalonia, an open source, cross platform reimplementation of WPF, to write Android/iOS/Linux/Mac/Windows apps these days. It's a much better option than Xamarin IMO. It's much more performant than WPF and has some nice improvements to it like CSS selectors in the styling system. – Chris Bordeman Nov 09 '19 at 06:32
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.

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
Finn
  • 1,426
  • 12
  • 8
  • 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 04 '18 at 18:12
  • @Garrom It is also part of Unity3d; that game engine [embeds Mono](https://askubuntu.com/questions/954173/what-are-dlls-doing-in-linux-programs/954371#954371). But you can write standalone programs that run with Mono, too. (You can also write your own native code programs that embed Mono, as Unity3d does, if you should ever want to do that.) – Eliah Kagan Apr 04 '18 at 18:18
  • But is Mono up to date with Microsoft's latest implementation of .NET? – Actually a physicist Apr 04 '18 at 19:51
  • @Actuallyaphysicist It depends what you need but [pretty much, yes](http://www.mono-project.com/docs/about-mono/compatibility/). There's a usually brief lag between new framework and C# and F# versions, and Mono support. IMHO the main missing thing in Mono is WPF. But it also doesn't have some libraries that are long-deprecated or highly Windows-specific. Note, however, that the version of Mono packaged downstream for your Ubuntu release may not have the latest functionality. So you may want to use [the Mono Project repository](http://www.mono-project.com/download/stable/#download-lin-ubuntu). – Eliah Kagan Apr 04 '18 at 20:00
  • 43
    The latest version of Microsoft's implementation (.Net Core) also supports Linux. – BlueRaja - Danny Pflughoeft Apr 04 '18 at 22:14
  • 6
    Mono implements .net standard 2.0 (iirc) not just core. – casey Apr 05 '18 at 03:27
  • 15
    This would have been a nice answer a few years ago. Now it’s pretty much obsolete. – Konrad Rudolph Apr 05 '18 at 15:13
  • 1
    @KonradRudolph obsolete? but doesn't .NET core basically require NuGet dependencies for basically any production-level program? or are there other alternatives – somebody Apr 06 '18 at 07:58
  • @somebody Afaik it creates dependencies for the packages you actually use in the program. Contrary to the usual "include everything" principle of Mono/.NET Framework. – Farhan Anam Apr 06 '18 at 18:34
  • @FarhanAnam it also doesn't include a _lot_ of things – somebody Apr 07 '18 at 00:12
  • 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 07 '18 at 22:50
  • @somebody "but doesn't .NET core basically require NuGet dependencies for basically any production-level program? " Yes, but that's got nothing to do with its readiness as a platform. It's more of a feature in that it's a far more modular framework... though time will tell. – NPSF3000 Apr 07 '18 at 23:12
  • @NPSF3000 by that I mean thirt-party NuGet dependencies for things Microsoft should have provided (I think) – somebody Apr 08 '18 at 01:12
  • @somebody I haven't noticed it being any worse than normal C#... or you know, open source software... – NPSF3000 Apr 08 '18 at 01:27
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.

Peter Mortensen
  • 933
  • 2
  • 11
  • 17
gorlok
  • 322
  • 1
  • 8
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
Sasan
  • 141
  • 4