There are many times that you want to try a piece of logic or simply write some code that all of it would fit on one page. Well, if you are a C# developer like myself and you love the language, you can use Visual Studio Code and Scriptcs to make scripts in C# and run them conveniently on both Mac OSX and Windows.
To start on Mac OSX, I assume that you have already installed.Net Core on your system. If you don’t know how to do this, you can go through the steps here. Also, have the latest version of Mono installed on your Mac as well. Ideally, you have installed Visual Studio for Mac as well. Assuming that you have installed all of these tools and frameworks on your Mac, we can go to the next step to get Scriptcs installed and getting it to work.
Detail steps of getting Scriptcs for Mac and Linux are listed here. I personally went through some trouble to get the code built and installed on my Mac, so I will share with you what I did.
Building the source code can be tricky. Make sure that you have the latest version of Mono installed and you have set the correct permissions for the folders, so the build process can create the necessary folders such as bin and create the .dll files.
After building the code successfully, you need to add the path to your profile. To do so, in your terminal, go to the following directory in your scriptcs solution code:
~/scriptcs/src/ScriptCs/bin/Release
make sure that file scriptcs.exe
exists. Create an alias for the path to this file in your bash profile. I named the alias scriptcs.
alias scriptcs = "mono {path to scriptcs.exe}"
To make sure that everything is done right, we can execute a small script from the terminal window.
In your terminal command line type:
scriptcs
or whatever alias name that you chose to point to the scriptcs.exe
file. You must see the following response:
scriptcs
scriptcs (ctrl-c to exit or :help for help)
>
We are ready to type some C# command. Type:
> Console.WriteLine("Hello ScriptCS")
and you must see the following output:
Hello ScriptCS
Success! Scriptcs is working. We can press ctrl+c to exit the environment.
I use Visual Studio Code to write longer and more complicated C# scripts. In your desired destination create a folder. I have created such folder and named it Scripts under the Documents. Create a new file and name it project.json
inside the Scripts folder. Open up the file by whatever text editor tool that you like, and leave empty brackets in the file {}. That is it. You will appreciate this momentarily. Save and close the file. Create another file in the same Scripts folder and name it whatever you like, but save it as .csx
file.
Return to Visual Studio code and open the Scriptcs folder. It is very important to open the folder and not .csx file. Now that the folder is open, you have visibility to both project.json
file and your .csx
file. We will do our scripting code in the .csx
file. So, the reason that we created the project.json
file is that Omni Sharp is going to be triggered by this file and activate C# IntelliSense in Visual Studio Code. IntelliSense makes the coding for C# much more convenient.
Here I have written a simple code in Visual Studio Code:

As you can see, the whole process is quite simple and the combination of Visual Studio Code and ScriptCs gives you access to C# scripting like never before.