Interactive Mono C#

I've  been teaching my son C# programming lately.  This is a great choice, because it's the best language to use with Unity, and with Unity, he can make games for virtually every platform under the sun. With some luck, he'll be able to make enough money in the various app stores to pay his way through college (or at least pay his own car insurance).  And as a bonus, when you know C#, you can also develop native apps for Mac, Windows, iOS, and Android, using various toolkits (such as MonoTouch for example).

However, the code-run-test-debug cycle is slower in Unity than I would like for his purposes.  Learning a language is easier when you can play around with it interactively.  This is especially true when you're working through small programming problems like those at Project Euler.

 

So I today I rolled up my sleeves and found the following little gem: CsharpRepl, the interactive Read-Eval-Print-Loop (REPL) for Mono C#.  Installing this on my Mac was super-easy:

1. Visit the Mono downloads page, and get the installer (I grabbed the 3.0.3 beta).

2. Run the installer.

3. In Terminal, run the new "csharp" command (which has been installed as /Library/Frameworks/Mono.framework/Commands/sharp, with a symbolic link in /usr/bin/).

That's it!  It drops you into a friendly little interactive C# session.  You can define variables and methods, evaluate expressions just by typing them, print things with the print() function, and so on, just as you would expect.

You can even go nuts and use csharp in a shell-scripting style, though you have to use /usr/bin/env to resolve the path to csharp.  Here's an example:

 

#!/usr/bin/env csharp
String whom = "world"
print("Hello " + whom + "!");
print("It works!")


Save this in a text file, make it executable (with chmod u+x), and then you can execute this from the command line just by typing its path.

There are some limitations to CsharpRepl; most critically, it sounds like you can't define classes this way, but only statements and methods.  So for any non-trivial program, you'd want to either use MonoDevelop, or (preferably, in my view) use a good text editor to write it and the 'mono' command to run it.

But for quick hacks and experimenting, CsharpRepl looks like a valuable tool.