The summer of C#

14 Oct 2014

This summer, I spent most of my time at an internship, writing iOS apps in C# using the Xamarin.iOS toolchain. Initially, I thought that C# must be a terrible language, because people described it as “Microsoft Java” (bad thing + another bad thing). I’ve since come to like the language a lot — I’m writing this post so I have somewhere to send the people who give me strange looks after I tell them about writing iOS apps in C#. (It’s been happening a lot less since Swift came out.)

Balancing idealism and utilitarianism

There are some languages that let you do everything: for example, you can effectively create your own language with preprocessor macros and operator overloading in C++. There are other languages where the ideals are taken too far: Ruby’s principle of being as concise as possible also makes it difficult for beginners to read, and Java’s object orientation makes some people think that making factory factories is a good idea.

The genius of C#’s design is that it was first used internally when Microsoft ported large, nontrivial libraries to test their new language. Through this process, and by incorporating feedback from developers, they created a language that brings the best of both C++ and Java, while learning from their mistakes and adding modern features.

A language that grows with the developer

Superficially, C# looks a lot like Java, so it’s not hard to see why people dismiss it as a clone.

using System;

namespace MyProgram
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine ("Hello World!");
        }
    }
}

In fact, if you have written Java code before, just use the syntax you’re already familiar with to write mostly valid C# code. The IDE will correct you.

For advanced programmers, there are plenty of modern features to keep them happy. LINQ enables SQL-like queries on collections. Null coalescing. Operator overloading. Extension methods. Type inference. Unsafe blocks. Calling native libraries with P/Invoke. Stupid-simple asynchronous programming. C# is a managed language, but it doesn’t make you feel limited in the ways you can implement something.

Expressing intent

The result is a programming language conducive to writing code that expresses the programmer’s intent more than the mechanics of how to achieve that intent. There is a reduced need for boilerplate/idiomatic code, so more code represents the meaning of the program.

But Kevin, it’s a Microsoft language!

Nonsense. The Mono project (Xamarin) has open-source implementations of the runtime, language, and IDE. In some cases, Xamarin’s developer tools are even easier to use than their counterparts from Apple and Microsoft.

But Kevin, insert language is faster!

Probably, but by how much? Mono’s ahead-of-time compiler turns C# into native assembly, and can even optimize it with LLVM. If you use Rdio or MarketWatch, you’re already running C# code on your iOS device without knowing it.