In the following interview, software engineer Greg Shackles (@gshackles) aims to expand the potential of this platform. Shackles is the author of “Mobile Development with C#” and maintains a blog focusing mainly on .NET and its related technologies.
We hear a lot about using C++ to build mobile apps, so why use C# — and the associated .NET?
Greg Shackles: There are various ways to share code across different platforms. Unfortunately, many approaches will abstract away the user interface from the developer in order to achieve a “write once, run anywhere” solution, making it easier to release an application quickly on many platforms. This sounds great, but often it will lead to a degraded user experience since the app won’t look and feel native to that platform. The user experience is the most important thing to consider when designing an application.
Using C# and the Mono Tools allows the developer to share a large subset of an application’s code across multiple platforms while still building a completely native user interface on top of it for each platform. Applications created with this approach will look and feel native because they’re using the exact same APIs and toolkits exposed by the platform. In some cases, the Mono tools even help to clean up the platform APIs to make them easier to work with than those exposed by the native languages.
This approach allows developers to concentrate on solving business problems rather than having to manage multiple languages and reinvent the wheel every time they want to expand to a new platform. Going even further, the code that is shared across platforms isn’t limited to mobile applications. It can go pretty much anywhere that C# and .NET are supported, such as ASP.NET, Silverlight, or WPF. Developers already familiar with these technologies can easily hit the ground running and start targeting these new platforms while reusing the skills they already have.
What else makes the .NET Framework well suited for mobile development?
Greg Shackles: C# and .NET are both very mature and powerful technologies. They have evolved over the years to provide support for things like asynchronous programming and memory management, and features like LINQ help make them great to work with as a developer.
For example, there is no garbage collector when writing iOS apps with Objective-C. That’s a feature .NET developers are used to having. MonoTouch actually brings a garbage collector along with it, making it much easier to work with, without having to worry about manual memory management.
What are a few of the technical weaknesses of C# or .NET?
Greg Shackles: There aren’t too many technical limitations, but whenever you place another layer between you and the native platform, some problems are unavoidable.
One example is that on iOS, you are not allowed to dynamically execute code at runtime, meaning that the standard .NET style of just-in-time compilation is not permitted and that aspects of .NET that rely on runtime code compilation are not possible, such as Reflection.Emit and the Dynamic Language Runtime. To get around this, MonoTouch compiles the application down to static code ahead of time. This particular limitation does not apply on Android, which does allow for just-in-time compilation.
For those who are already developing native apps for Android or iOS, what benefits would they gain from using C#?
Greg Shackles: For developers who have already built their apps in Java in Objective-C, the case for switching to a new set of tools definitely becomes more difficult to make. The benefits they would get from making such a move would largely be in the ability to share code across all of the platforms rather than have to rewrite it in a different language every time. Both MonoTouch and Mono for Android offer the ability to interact with code written in Objective-C and Java, so code already written in those languages could still be leveraged.
What kind of cross-platform mobile apps are easy or best to make under C#?
Greg Shackles: I don’t think there’s any particular category of app that’s obviously more difficult to write in C#. For extremely simple applications that don’t have much logic, it becomes more of a decision of preference for the developer rather than a strategic advantage. In reality, not many applications fall into this category. A majority of applications will need to perform tasks like accessing the Internet or saving to a database, and that is where it becomes beneficial to be able to write that code once and share it across all platforms. Personally, I find C# to be a much nicer language to work with than Objective-C and Java, so that alone becomes an advantage of using it.
.NET is native on Windows Phone 7, but it’s not on Android or iOS without the use of MonoTouch or Mono. What are the performance issues or differences across these mobile platforms when you’re developing for all three at once using C# through .NET and its unofficial variants?
Greg Shackles: The addition of another layer between you and the platform will have its consequences, but by and large, it’s not something you’ll notice or need to worry about as a developer. Since MonoTouch applications are run through its ahead-of-time compiler, their performance is already highly optimized. Mono for Android applications include their own instance of the Mono runtime that .NET code is run against and includes an intelligent garbage collector that is optimized for managing objects across the different runtimes. In general, you won’t be able to see any difference in performance between an app written in C# and one that is not.
One other common concern is the size of the application, since the .NET Framework is not known for being minimal. Both Mono for Android and MonoTouch ship with a tool called a linker that is included as part of the build process. The linker is a static analysis tool that scans the compiled assemblies in the application and actually strips out any pieces of the framework that are not referenced. As a result, your application will only ship with precisely the pieces of the .NET Framework that you actually use, which drastically cuts down the size of the application. With each release, the Mono team seems to find new ways to optimize the linking process, so this size overhead continues to dwindle down further, even though it is already rather minimal.
This interview was edited and condensed.
- Greg Shackles interviewed by O’Reilly editor Rachel Roumeliotis
- 10 ways to botch a mobile app
- The evolution of iOS development: Better tools and a lot more to think about
- What to watch for in mobile web apps