Unless you've been living on a planet far, far, away from the galactic trade routes, you know there are only two practical choices of programming languages -- Java and Objective-C -- for developing native mobile apps. If you are an experienced C# codesmith who wanted to try your hand at Android or iOS app development, you were out of luck. No longer: Xamarin 2.0, an SDK produced by Xamarin, allows you to write Android and iOS apps using C# code. Equally important, you can do this with Microsoft's Visual Studio.

The Xamarin SDK is based on an open source project known as Mono that allows Microsoft .Net applications to run on other platforms. Xamarin currently consists of class libraries, a C# compiler, and a virtual machine. The class libraries implement the core APIs of Microsoft's .Net 4.0 framework. The compiler complies with the C# 5.0 standard, and it has support for namespaces and assemblies. The virtual machine executes the Common Language Infrastructure bytecode generated by the C# compiler.

The Xamarin SDK also provides platform-specific frameworks that work with Google's Android APIs and Apple's iOS APIs. However, these frameworks are not abstraction layer APIs that only access a lowest common denominator of platform services. Instead, they provide "bindings" -- mechanisms that enable C# method calls to properly invoke native methods designed for use with a different programming language -- to the native APIs for each platform. The Android API binding frameworks are called Mono for Android, while those for iOS are called MonoTouch. The result: Xamarin lets you write a native mobile app entirely in C# code.

To use the Xamarin SDK to its fullest, the app should be factored so that its UI code is uncoupled from its core algorithmic code. That is, you write the core algorithms with the .Net framework, then implement the native UI for each target platform using the Mono for Android and MonoTouch frameworks. The factored design allows the app to be built and deployed on both Android and iOS devices, where the app's core code uses the native API of the target platform. This scheme lets you repurpose field-tested C# code and libraries. It also provides some flexibility in choosing how many of the platform's native APIs to use. For example, your app might draw on the .Net networking stack for network services or rely on the platform's own networking stack frameworks.

To develop your Android and iOS apps, you can use Xamarin's own IDE, Xamarin Studio, or stick with Visual Studio. The Xamarin IDE allows you to organise projects into solutions so that one build command will make both an Android app and an iOS app. The Xamarin IDE also lets you execute and debug code in simulators for each platform or on the devices themselves. There's no compromise if you prefer to develop in Visual Studio, as Xamarin lets you build, download, and debug app code on the target devices with Microsoft's IDE.

iOS app development: It's complicated

Although Xamarin lets you use your Windows PC for iOS development, that doesn't mean you don't need a Mac. For a number of reasons, Xamarin still requires Apple's iOS SDK tools to complete the code generation. For example, the Xcode simulator is required for code debugging, and the app's interface must be designed using Xcode's layout and storyboard editor, Interface Builder (IB). Finally, to distribute apps in the App Store you must use Xcode's code-signing mechanism.

How is it possible to use Visual Studio to write and debug iOS apps? Xamarin performs a clever sleight of hand that sets up the Mac as a back-end build server to Visual Studio. Installing Xamarin on the Mac places a remote build server on the computer. This build server uses Apple's Bonjour service discovery protocol so that Windows systems can find it on the network and establish remote sessions with it. An inexpensive Mac Mini can be used as a shared network resource for multiple Windows developers.

When you execute a build with Visual Studio, the remote session has the build server issue the appropriate command lines to the Xcode build tools. (This works because the Xcode IDE itself is simply a GUI layered over command-line tools.) After the build completes, the iOS simulator on the Mac starts and remote commands from Visual Studio operate the simulator through the Xcode debugger. Or the Xcode debugger can download the code onto an iOS device, and you can debug it there through Visual Studio.

The other aspect to iOS app development is whether to IB or not to IB. The UI of an iOS app can be constructed two ways: either through program code or by using Xcode's visual editor. IB lets you lay out the app's interface by pointing, clicking, and dragging UI elements. These UI elements are actually Cocoa Touch framework objects in a serialised format. That is, you manipulate the UI objects themselves in IB. This ability to edit Cocoa Touch objects is not easily duplicated on the PC. Therefore, you either have to use IB on the Mac to design the app's UI or write the UI entirely in code to implement it. In the latter case, you can use a utility like PaintCode, which lets you design the UI visually; it then generates the appropriate Objective-C or C# code.

Constructing the UI for an Android app can be accomplished with Windows tools, because Android uses XML to store the layout. Xamarin Studio has a graphical layout editor similar to IB that lets you construct the Android app's UI visually. Xamarin also provides an add-in that implements this graphical editor in Visual Studio.

Using Xamarin Studio to run a C# app in the iOS simulator. A breakpoint has been set in a platform-specific UI file that manages the native implementation of a log-in screen.