The C# programming language deserves more respect. Here's why.
I used to be a Java developer, but I made the switch to C# a few years ago. I've been thrilled, ever since, with C#'s productivity conveniences like Linq and a philosophy that can be summed up as "strongly typed where possible, dynamic where necessary."
I've noticed, however, that while there's a good deal of high-performance server software and other open-source projects written in Java, little of this kind of code gets written in C#. Notably, Cassandra, Solr, and Hadoop are written in Java. I've also heard tell of several big-data startups choosing Java instead of C++.
But why not choose C#?
In fact, C# is a better fit than Java for high-performance requirements. I'm not referring to VM stats that change every year. C# has been designed from the ground up with efficiency in mind.
Value types
At the core, C# has a struct keyword that enables you to define objects that are always copied instead of being passed by reference. While Java has primitives like int, and byte, custom value types aren't supported. While reference types are usually more efficient, you can get more fine-grained control over memory with value types, which is critical in high-throughput scenarios.
Value types also have implications in garbage collection (GC). Both C# and Java have generational mark and sweep collectors. A little known trait of generational GC is that allocations on the heap are about as fast as stack allocations, in contrast to C/C++, where heap allocations are typically much slower. However, the longer an object lives, the more time is spent in GC cycles processing its references. Since you can't store references to value types, the garbage collector never has to process them, which means they are immune to the expensive faults of mark and sweep GC.
Generics
In C++, generic classes were completely reprocessed for every template instantiation. While this was powerful, it also had disastrous effects on compile times, among many other problems. Java overreacted by defining generics as reusable classes that must convert value types to and from their equivalent reference types -- an expensive operation called boxing that requires extra memory and execution time.
When a C# generic class is instantiated, the generic template is used to create a new class. So typeof (Person) is completely different from typeof (Manager). No boxing is required since each template class is specialized for its purpose.
Politics
As far as I can see, C# is not only a safer and more productive language than Java, it's also a more efficient platform due to some fundamental design decisions.
So why is Java frequently chosen over C#? In short, politics.
Java has always been about open process. There is a lot of open-source history that's become a core to Java, such as the Apache project. In contrast, Microsoft designed C# as an entire product ecosystem that it sought to monopolize. Many C# shops stick to a Microsoft-only mentality, despite the growing floods of open-source software becoming available.
Java was designed from the beginning to run equally well on Windows, Mac, and Linux. While C# was also designed to run cross-platform, in practice it never did until the mono project took off in recent years.
These trends seems to be changing. Microsoft is starting to open-source some core .NET infrastructure. Mono is gaining strength, especially in the mobile space. And so I wonder: When will the next serious open-source database contender get written in C#?
Your thoughts? Leave a comment below.
Related posts: