Hello World using MonoTouch
By ESCOZ posted March 24th, 2010At the MSDN Blogs, Shawn Burke has recently decided to do a comparison of the iPhone SDK and the Windows Phone 7 Series SDK (what a horrible, horrible name!) by writing a simple Hello World application and showing the difference between the two.
While I don’t think it’s really fair to compare a C-based platform with a .NET based one, Shawn’s point that the it takes a lot more code to write the same application is real. It’s also real to say that the iPhone SDK could run extremely well in a 400MHz processor three years ago, while the Microsoft will need a 1+Ghz processor and still isn’t on the market, but that’s a different conversation.
Anyway, I work daily with MonoTouch, a port of the Mono platform to the iPhone, and thought it would be a good idea to show how concise iPhone development can be when you write code on top of the platform. Here are the steps:
- Open MonoDevelop, select File->New Solution->Type in name->click Forward->Click OK.
- Double-click MainWindow.xib
- In Interface Builder, drag and drop a UIViewController into the xib, and then a UIView inside the UIViewController
- Drag and drop a UITextField, a UILabel and a UIButton into the UIView, and change the text on the controls accordingly.
- Define three outlets in the AppDelegate class (for simplicity), and connect them to the UIViewController, UILabel and UITextField added previously.

- Add and action to the AppDelegate called “OnButtonClicked” and connect to the TouchUpInside event of the button.
- Control-S to save, Control-Q to close Interface Builder.
- Back in MonoDevelop, open up Main.cs and inside the FinishedLaunching() method, add this line of code:
window.AddSubview(ViewController.View); - Outside of the method, type “partial”, and select the OnButtonClicked option.
- Finally, write the code to set the text:
partial void OnButtonClicked (UIButton sender)
{
label.Text = String.Format("Hello {0}!", String.IsNullOrEmpty(textField.Text)?"World" : textField.Text);
} - You’re done! Press Command+Enter, and the application will run on the simulator.

You can take a look at the entire solution here. You won’t be able to open the solution in Visual Studio, but you can look at the files in any editor and see the code I wrote above. It even includes copy/paste! (yeah, low punch, I know).
So, at the end, how many lines of code have we written? 3 or 5, depending how you count. If I had written the code the same way as Shawn, it would be more like 7-8. So it’s more code than when using the Windows Phone SDK, right? Well, I would say wrong. Here’s why:
Although we used XML on the solution (the XIB file is XML afterall), we absolutely never touched the file at all. For all purposes, the code in that file doesn’t exist at all. I’ve been developing for 5 months using MonoTouch now, and I never had to open the XIB in anything other than Interface Builder.
So when Shawn mentions that he can write the entire thing in only 4 lines of code, (or even zero!) that’s not entirely true. You’ll be hand editing the XML directly daily, to the point of even writing untested, non-testable business logic in it (like Shawn did in his example). And that XML will have bugs in it (yes, it will). And if you’re going to do it, you better count those lines of code as well. In Shawn’s case that adds 15 lines and a totally different language developers will have to get used to.
Isn’t it funny how you never really hear objective-C users complain about their platform? The iPhone SDK is a really well developed API, and really simple to use to create applications. It’s different from Microsoft’s solution, but that doesn’t mean its bad (some would say its actually better just for that, I prefer to stay in the middle).
Before anybody says anything, yes, I used MonoTouch, and not Objective-C like the Shaw’s example. My opinion here is that the language doesn’t really matter: the framework does. What’s the problem if it takes 5 more lines of code in objective-C than C# to do something? I bet a lot of those obj-c developers can write those 5 lines a lot quicker than I do.
I love .NET, and I’ll likely be writing many apps in the WP7SSDK (worst acronym ever, I guess) in the future. From what I’ve seen so far, the platform sounds very interesting, and it’ll be great to reuse code from MonoTouch and just have to rewrite the view code for the new phone.
Filed under: Development | Tags: MonoTouch, Redpoint | 5 Comments »
ESCOZ » Blog Archive » Hello World using MonoTouch…
Thank you for submitting this entry – Trackback from MonoTouch.Info…
While it’s nice to see the MonoTouch side of the fence, your “hand editing XML comment” is quite wide of the mark.
While it’s true that Shawn shows, and works with, the RAW XAML, and Visual Studio and Blend provide decent intellisense for it, you could quite easily create the same thing using the Visual Studio drag drop designer, or Expression Blend.
I don’t see why you think the XAML contains business logic either? It’s a declarative markup language, much like a xib file, but you don’t actually put code in there (triggers withstanding, but they *should* be used for UI “reaction” to user input). In Shawn’s example he uses code behind, rather than MVVM, but code behind is no more “evil”, or less testable, than the broken MVC implementation in the iPhone/MonoTouch world.
The big winner in this story has to be the C# developer. The game I’m working on I have all the game logic under test in a class library and by changing UI/interaction layers I can “easily” target it at the iPhone with MonoTouch, the web with Silverlight, Windows Phone Series 7 and in the future possibly even Android if MonoDroid materialises.
I started with Windows Mobile (years ago) and now also build iPhone apps several months using MonoTouch.
One thing not written here is the fact that most of the code is “business logic” or “functionality”. Of course there is UI and animation – but for an example in our projecet Sticker http://iphone.pp-p.net/Products/Stickers.aspx far less than 20% of the code is UI.
For me the language counts. I love C# and it offers me a great way of code reuse.
And I tend to use Storyboards or other platform specific things as little as possible.
Stickers as well as TTMoney http://iphone.pp-p.net/Products/TTMoney.aspx use a framework for handling forms.
) Silverlight thing:
And since TTMoney should be “User designable” via a Silverlight interface we also used this framework to build this (very beta
http://iphone.pp-p.net/Products/Images/TTMoney/SLDesigner.png
Just my 2 cents
Manfred
“You’ll be hand editing the XML directly daily” that’s a totally misleading statement. All what Shawn did can be done using VS and Blend UI, just like when you did with MonoDevelop UI.
I don’t think Shawn’s article was just about “see what I can do”, it was also helping begginers to get started, and that won’t happen with showing how to drag and drop items around.
Nobody said iPhone API is bad, but let’s face it, developing with Phone 7 & SL is much easier and faster.
One more thing worth noting here; iPhone API is the officially supported platform by Apple and not MonoTouch, so I think it makes more sence to compare with.
This is a totally different story than whether iPhone is better or Phone 7. I know some people get sensitive when it comes to iPhone
Hi TheBlueSky
I don’t really think saying that the xaml code will be manually coded is misleading.. Even Microsoft recognizes that: most of their videos and documentation includes a lot of editing of xaml.
My point regarding that, is not that its bad per se (although I don’t like it at all), but that that code counts towards LoC counting. It’s another language (xml), but its still code.
The Apple SDK might be the official platform, but its not the only one; Shawn had already done that comparison, so it would be kind of pointless for me to do the same. Instead, I decided to write about a different platform that can be used. I don’t see the problem with that.
People get sensitive about the iPhone, but it seems there’s a lot of people who get sensitive about WMobile as well..