How To: Compile C# App in Visual Studio for Linux

For those who don’t know C# is a Microsoft language and Visual Studio is a Microsoft IDE or Integrated Development Environment.

Due to Microsoft’s history of being a very proprietary closed-source company there’s a bit of a perception that you wouldn’t be able to compile Applications written in C# and run them in the Open Source world of Linux but I’m going to show you just how easy this has become with a very basic hello world example.

What you’ll need:

Once the above prerequisites are satisfied we can begin. I’m using a very basic Hello World sample from here: https://msdn.microsoft.com/en-us/library/aa288463(v=vs.71).aspx

First open Visual Studio and navigate to File -> New -> Project

New Console Project

Then we’ll want to select the options for a new C# console application and give it a name at the bottom. Then click OK.

Code

Next you’ll want to copy and paste the Hello World Example 1 into the open file as pictured above.

Now debugging this application is difficult since the console window closes as soon as the application is finished. So to test this before we move over to Linux go ahead and build the application by clicking Build – > Build Solution in the menu at the top or Ctrl-Shift-B for keyboard fans.

Right-Click-Menu

Once your build is complete, right-click the project. in the solution explorer on the right-hand side, and choose Open Folder in File Explorer. This will open the folder with the project.

Right-Click-Menu.2

Then you want to go into the bin folder, then the Debug folder. Once in there do a shift & right click combination and choose Open command window here from the options.

Windows Debug

This will open a command prompt where you can run HelloWorld.exe and confirm that it does in fact output “Hello, World!”

Lastly copy HelloWorld.exe to Linux and run “mono HelloWorld.exe” and you should see something like the following:

Screenshot-from-2016-05-03-22-31-51

I hope this helps others to see a small portion of what’s possible out there now (for free!).

I’m a die hard fan of open source myself and I got my start with languages like HTML, PHP, & Javascript using IDE’s like Eclipse, Aptana, & Notepad++.

However that said since I’ve become a C# developer for work I’ve gained a lot of respect for the language and especially for Microsoft’s IDE. Visual Studio really makes coding a lot more fun and the “Intellisense” feature it offers keeps me from having to have documentation pages open about available methods and arguments like I did with PHP development. You simply start typing and Microsoft gives you a list of options you have in the method you’re using but don’t take my word for it. Download the Free Visual Studio Community edition and try it for yourself. There’s really nothing to loose (Except maybe a few Gigabytes of hard drive space).

Leave a Reply