C# with Visual Studio on macOS

image
I have been coding with Microsoft .NET in C# since the day it came out as BETA1. On Windows it is my favorite language to deal with.
Once I learned that the new CEO of Microsoft had pushed his ranks to think more cloudly therefore more multi-platform oriented I checked out the new Visual Studio IDE, which is free and multi-platform.
I absolutely love Visual Studio for Windows, there’s nothing that beats it. I wanted to give a try the version for Mac OS X. My default platform for inventing.
The experience is not remotely close to the richness of what you get on Windows but man they are on an awesome path to nail it.
.NET comes to OS X (and Linux) via mono and other esoteric open source + corporate touches coding efforts. So documentation is a bit sparse but the value is coming out with regular updates.

Foundational and Terminology

There is plenty of new terminology to be understood and memorized, in a nutshell, the keywords that you want your brain to process right away are: DNVMMCSDNX, DNU and all the extra details are explained here. They all confusingly run under the term aspnet, if you don’t mean to work with dynamic web pages.
DNVM is a framework version manager.
DNX is the actual execution .NET runtime.
DNU provides various functionalities and mostly is package/libraries facilitator.
MCS is the actual compiler but hold one there are FOUR compilers available. Yeah! That is a big source of confusion for newcomers. Short answer:
mcs: compiler to target 1.1 runtime (to be deprecated with Mono 2.8).
gmcs: compiler to target the 2.0 runtime.
smcs: compiler to target the 2.1 runtime, to build Moonlight applications.
dmcs: Starting with Mono 2.6 this command is the C# 4.0 compiler, and references the 4.0 runtime.
To get your system up and running here is what you need to do:
  1. Install DNX following those instructions. Don’t fret and skip steps because fixing will be a major pain as the product is under development and some kinks just make it a lot harder if you don’t master the terminal world at God level.
  2. Download Visual Studio and move the app to the application folder (don’t leave it lay in the download folder)
  3. Create a folder somewhere that you usually store your source code
  4. using home-brew, npm or whatever floats your boat, install Yeoman aka YO. It’s a necessary scaffolding tool that will help you a lot.
  5. From terminal type yo aspnet
  6. some ascii art will display a menu that you can traverse using the arrow keys. Select “Console Application”.
image
  1. A few simple questions are asked before creating the scaffolding, hit enter on all to accept the defaults.
  2. Follow the instructions at the end of the scaffolding generation (dnu restore/build…)
  3. Add the following to your .bash_profile, it will save you time in the future: 
  4. #   —————————————
    #   VISUAL STUDIO
    #   —————————————
    code () {
       if [[ $# = 0 ]]
       then
           open -a “Visual Studio Code”
       else
           [[ $1 = /* ]] && F=“$1” || F=“$PWD/${1#./}”
           open -a “Visual Studio Code” –args “$F”
       fi
    }
  5. then type source ~/.bash_profile to force the reload without exiting the terminal (it’s just convenient)
  6. cd to the directory where you scaffolding was created
  7. type code . 
  8. Off the bat Visual Studio is setup for Nodejs projects. Which is awesome but annoying if you come with C# background on Windows. Click on the debug icon (last on the left), hit the play button on the toolbar. This will force the creation of a file, called launch.json
  9. The default content of that file is useless to you for C#, replace that with what you see in the following screenshot.
image
  1. That will allow you to build that can be down with the easy to not remember a combination of shortcuts CMD + L + SHIFT + R + Enter. Yah… You can do that in other ways however since the tool still in beta, I found this sequence to hit the right timing and less mouse pain.
  2. This change will allow you to make your application run, which is a big yeah! To enable debugging requires some extra sorcery but it is worth it.
  3. Hit the combo key CMD + SHIFT + B to build. You will get an error because the task build is not configured yet. On top of the IDE hit the blue button that says configure task runner.
  4. Replace what is created for you with the following
At this point is a very good idea to display the output window to diagnose any error that is not bluntly shown in the UI. Hit SHIT + CMD + U. If you hit the little icon on the top right you can even use split windows to have everything in one master view.
image
At the time that I am writing this, YO generates the improper code for a traditional console app. As you can see from the screenshot above. The screenshot below instead shows the fixes. Which is just adding the static keyword where necessary.
image
At this point you can place a breakpoint clicking on the left side of the line numbers, hit F5 or the wonder combo keys and enjoy your first debugging ever of C# on OS X. The world is changin’ !!
I made you suffer during the post is not providing the text to copy and paste in your attempt project because I wanted you to learn and not just emulate. However in case you are super lazy or you want a short cut for a second try you can use my GIT gists down below, they contain the screenshotted code in this post.
Enjoy the new toy on your cool mac ;-)

References

  1. Compilers
  2. Aspen overview
  3. Install aspnet on Mac
  4. Install aspnet on Windows

Code

It’s on GITHUB

Comments

Popular posts from this blog

Postgres on Synology

The Making of Basculo

Build an independent watch app - Part II