Introducing C# To Go: a C# Compiler for Android

If you follow me on Twitter (or the #monodroid hashtag), you might have seen some posts from me lately about progress on my new Android app, C# To Go. I’ve been cranking away on it in hopes of getting it to market in time for Monospace, and I’m excited to say that last night I was able to finally publish it! I just wanted to do a quick write-up about the app, what it can do and how it was made.

The App

First off, C# To Go is exactly what it looks like: a C# Read/Eval/Print Loop for Android devices. I intentionally kept things pretty simple for this version, so it’s a single line text input. This may change in the future, but having full editing brings in expectations of proper code completion, formatting, syntax highlighting, etc…all things I wanted to avoid for this launch. That said, there is in fact basic code completion. Whenever you type the “.” key, the compiler will try to find completion options for you. Typing code on a phone can be kind of annoying, so this was a necessary feature. Some other other features include:

  • Namespaces can be quickly brought in using the built-in list of all available namespaces
  • Declared variables are available throughout the lifetime of the current session
  • Errors/warnings encountered are displayed after the code is executed
  • Console.Out is captured and displayed as well
  • Tapping on any executed code copies it back to the input box
  • Pressing and holding on an error or warning allows you to copy the text or search the web for help

Come On Man, Let’s Get Technical

If you’re reading this blog, you might be more interested in how it was made than what it actually does. I do hope to open source it all sometime soon, but I want to clean some things up a bit before I do that.

First things first: as you might expect, the app is written entirely with Mono For Android. The compiler used in the app is based on the Silverlight port of Mono’s C# compiler service done by Frank Krueger. I’m using my own fork of it at the moment, in which I added a Mono For Android build, as well as a little hack to get things working properly for me. I ran into an issue where certain code completion requests would go off indefinitely, which would tie up a lock and prevent anything else from being executed. I added a quick hack that allowed me to terminate that call if I needed to. It’s not pretty but it works for now – that’s what forks are for right? Seriously though, I want to give major props to Frank on this. His work in the original port saved me a ton of time here.

In my project structure I was able to separate out a lot of the core compiler logic into a separate class library that doesn’t reference any Android code, which should hopefully allow me to create a Windows Phone 7 port at some point, time allowing. To help decouple things further, I’m using Steven Robbins’ TinyMessenger event aggregator/messenger library. This allows me to keep more of the app logic in the class library without it having to know any specifics of the environment. I’m not using TinyIoC in this project (yet), but it’s also a great little library that works well with Mono For Android.

I think that just about sums up the core parts of the system. The rest of the work mainly involved the Android side of things, where I learned some hard lessons on how much of a pain UI work is on Android. The code input box is a custom view I made that extends the standard AutoCompleteTextView to do what it needed to.

The app is also pretty large, by necessity. The APK is about 17MB, which installs to about 21MB (on my phone, at least).

Current Limitations

So I know all that sounds pretty damn awesome, but I should probably mention some of the current limitations and problems. There were a couple problems I ran into with the Mono C# compiler service. The main one was an issue where if you tried to bring in a namespace and try to run some code again after getting an error the first time that the type could not be found, it still can’t find the type. I filed a bug report with them about this. A little annoying, but definitely not a showstopper.

I also had to prevent you from declaring classes within the app. The Mono compiler does actually support this, but for some reason when using it inside of Mono For Android, newing up an instance of that class will cause a crash deep in Mono. I’m hoping to be able to get this feature into the app in a future version.

Aside from those, the main issue is one of stability. From time to time, trying ot run some code will cause the app to just terminate unexpectedly. It’s not the input itself that is the problem. I think it may be related to garbage collection but haven’t been able to nail it down yet. This one sucks, so I’m definitely trying to get to the bottom of it as soon as I can.

Have Android, Will Compile

So there you have it. I’m definitely curious to see what everyone thinks, and also hear about how it behaves on various devices. In my brief beta period I had it tested on a few devices but I know that mileage can vary in the Android ecosystem, so definitely let me know if you see some issues. If you’re going to be at Monospace next week I’d be happy to show you the current source for the app if you’re interested. I may show some of it in my Android session on Saturday, time permitting. Happy compiling!

Tags: ,

22 Responses to “Introducing C# To Go: a C# Compiler for Android”

  1. Introducing C# To Go: a C# Compiler for Android…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  2. [...] it to market in time for Monospace, and I’m excited to say that last night I was able to… Read more… Categories: C#  .NET     Share | Related [...]

  3. [...] Introducing C# To Go: a C# Compiler for Android – Greg [...]

  4. Sounds really promising. Good luck with the project

  5. Thanks for an excellent idea and a really usefull utility!
    How to make this even better:
    - allow something like ‘#include “filename.cs”‘ – run /sdcard/csharptogo/filename.cs as if it were typed line by line
    - automatically run ‘#include “_autostart.cs”‘ on startup

    Thanks again – this is really usefull!


  6. Daniel Oulian

    I am a huge fan of c# but i just can’t figure it out how to use your app at a usual power. If you have the time we would appreciate some tutorials with some ex: generics, linq over objects, and so on. Thank you very much for your fantastic work. So please post some videos on how to really use your app.

  7. Thanks for checking out the app! Truthfully, in its current form it’s really not meant to be something you do any real work in. It’s modeled to be a typical REPL in which you can enter lines of code, get their output, and do little things like that. Taking things to the level of providing things like multi-line editing, code formatting, syntax highlighting, etc was something I wanted to avoid for at least this initial version. I will certainly be looking to make improvements to the app over time, possibly in those areas, but recognize that it’s a very slippery slope between the fun/novelty version that exists now and being expected to essentially provide a full IDE.

    I’m always open to feedback and feature requests/suggestions though, so don’t hesitate to get in touch with anything!

  8. Please how to use. var=Console.Read(); do not imput when is runnig

  9. Please how to use. var=Console.Read(); do not imput when
    is runnig

  10. Console.Read() is not currently supported by C# To Go, since it’s not really needed at all, at least in this version.

    Thanks for checking out the app!

  11. Very great work so far, congrats!

    My recommendations:

    * multiline editing would be most preferrable even if you don’t have proper highlighting or code completion yet; it’s just a nuisance to write longer statements in such a small space.

    * the Namespace bug is annoying and results in much typing but I see it is already known/reported

    * code completion/suggestions feel still a bit off at times for example when you’re starting out the “System.IO.” road. There are not really meaningful suggestions until you hit a real class.

    Just my 2c, I know you already accomplished some complex and difficult work. I understand that it will take time to tackle the points I listed.

    Thank you!

  12. Thanks for the feedback! I’m definitely looking into how to work multiline editing into a future update. It’s high on the priority list!

    Also agree on the namespace bug being annoying, hopefully the Mono team gets that one squared away sometime soon and I can bring the fix into the app.

    Regarding code completion, I would agree that it’s a little rough. The C# compiler service doesn’t seem to always provide all the completions that you would expect from an IDE, and since that’s what I’m using to drive things, I get the same limitations. I’m planning on exploring some options for tweaking things to see if I can smooth it out a bit, not sure what the possibilities are there yet without writing my own code completion system from scratch.

    Thanks again for your input, and for trying out the app!

  13. Great app!
    Blew our C# dev team away with my mobi-compiler and new-found uber-geekiness!
    P.S. I’ve overcome the single line issue by keeping a line-break copied on my clipboard. when I need to move to the next line I just paste it :p

  14. Awesome, thanks! I hope to work in multi-line editing as a feature sometime in the near future so you won’t have to paste line breaks :)

  15. Greg this app is a lot of fun.
    wish there was some intellisense. :)

  16. OIC there is intellisense

  17. Yep, it does basic code completion whenever you enter a dot. Glad you’re enjoying it! :)

  18. This app its great!
    But I have a question.
    How can I write files ?
    (access to the path is denied)
    Thank you! :-)

  19. File access isn’t something I’m really supporting at the moment. The app (and all apps on Android for that matter) can only write to their own files so in theory you should be able to do that given the correct file path (Environment.SpecialFolder.Personal might help you out there), but this is untested :)

  20. Great little app, good work. I have picked up heaps of hot women by showing them my Linq skills in bars with it :p

    Can you make it so you can load/save .cs files? I know it’s only for playing on your phone/tablet but i find i write a bit of code (because i’m traveling with my mobile device) I need to turn off the tablet and comeback to it later.

  21. Thanks, glad I could help :)

    Saving/loading files is definitely high on the list of features I want to add to the app. I’ve been swamped with some other things so havent been able to make the time for it, but I’m hoping that will change soon. Thanks again for checking out the app!

  22. Why don’t you just write a C# compiler that runs under Android? A lot of folks would buy it.

Leave a Reply