At Kalistick we are a Java shop from the beginning: the first version of our product analyzed only Java code. But we are not bigot, we are using and analyzing C# too since 2008. We notice that Java developers generally tend to look scornfully at C#, as a copycat created by Microsoft and used by dummies. In theses blog series, I am going to try to sweep this nonsense and show some of the C# goodness.
In the previous posts, I talked about the C# language: its differences with Java and its “unique” features. In this one, I am going to introduce the ecosystem. A language can be the best one in the world if its platform and tools are not as good, it will be useless. That is why I will focus, in this post, on the .NET platform in a whole: framework, libraries and tools. The time when knowing the language in and out only was enough is long time gone now.
- Part 1: Differences in similarities
- Part 2: A brand new world
- Part 3: The ecosystem
No good building without a good foundation.
Apart from the language itself, the standard framework is the part with which you interact the most. It is crucial that this framework provides facilities for basic day-to-day operations needed when writing an application, like reading/writing a file, parsing a text or sending a request over the network.
Long story short, the .Net Framework base classes offer the same features as Java, but in general they are more intuitive, easier to use and more concise compared to Java.
Let’s compare some basic operations.
String manipulation
String manipulation is a big chunk of the programming job.
Have you ever needed to check string references equality? String is more a Value Object than an Entity, we are only interested by its attribute not by its identity. So it is inconvenient that we cannot use the == operator in Java for checking equality. Thanks to operator overloading, in C# you can compare two strings either with the equals method or the equality operator. This seems insignificant but it is a frequent source of confusion in Java (we check this violation on our platform), it is even more error-prone because the equality operator can sometimes lead to the expected results due to string interning.
Regular expression constitutes a significant part of string manipulation; Regex manipulation is eased in C#, thanks to verbatim strings and named captures. Verbatim string helps prevent cluttering of the regular expression with escaping characters; named captures give names to group, making the regex easier to read.
Regex urlRegex =
new Regex(@"\\b(<protocol>https?|ftp)://(<domain>[-A-Z0-9.]+)(<file>/.*)?");
string domain = urlRegex.Match("https://cockpit.kalistick.fr").Groups["domain"].Value;
File IO
File IO is a fairly common operation and should be painless. In Java, without third-party library this is not true. Java beginners often ask what it is the best way to read of file, they do not know if they need to use a FileReader, a FileInputStream, a BufferedReader or a Scanner.
// In C#
string s = System.IO.File.ReadAllText(path);
// Equivalent in Java
String s = null;
FileInputStream stream = new FileInputStream(path);
try {
Reader reader = new BufferedReader(new InputStreamReader(stream)); // Use default encoding
StringBuilder builder = new StringBuilder();
char[] buffer = new char[8192];
int read;
while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
builder.append(buffer, 0, read);
}
s = builder.toString();
} finally {
stream.close(); // Potential issue if this throws an IOException,
}
Of course with a third-party library, this can be done in one line (Apache Commons IO for example).
Date manipulation
// In C# DateTime now = DateTime.Now; DateTime nextWeek = now.AddDays(7); // Equivalent in Java Date now = new Date(); Calendar c = Calendar.getInstance(); c.setTime(now); c.add(Calendar.DATE, 7);
(Use JODA Time for date manipulation in Java)
Concurrency
Before .NET 4, C# was behind Java concurrency-wise. But now it offers the same functionality than Java and even more.
// Parallel For
int[] numbers = { 2, 5, 8, 4, 7, 9, 13 };
int[] squaredNumbers = new int[numbers.Length];
Parallel.For(0, numbers.Length, i =>
squaredNumbers[i] = (int)Math.Pow(numbers[i], 2)
);
// Parallel LINQ
var fibonacciNumbers = numbers.AsParallel().Select(n => ComputeFibbonacci(n));
To sum up, I find the .Net framework base classes easier to use and more intuitive, in general, than their Java equivalent but all the third-party libraries available in Java alleviates this point. (Apache Commons, Guava, Joda time…)
Good Artists Borrow, Great Artists Steal
Many C# open source library are a direct port of successful Java library. They have since evolved to take advantage of the C# benefits, promoting DSL-like syntax instead of XML configuration file, but at the beginning they were direct “rip-off” of their Java equivalent.
- Hibernate => NHibernate
- Spring => Spring.NET
- Log4J => Log4Net
- JUnit => NUnit
- …
Example of a “fluent” NHibernate mapping:
public class CatMap : ClassMap
{
public CatMap()
{
Id(x => x.Id);
Map(x => x.Name).Length(16).Not.Nullable();
Map(x => x.Sex);
References(x => x.Mate);
HasMany(x => x.Kittens);
}
}
<!-- Equivalent XML mapping -->
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="QuickStart" assembly="QuickStart">
<class name="Cat" table="Cat">
<id name="Id">
<generator class="identity" />
</id>
<property name="Name">
<column name="Name" length="16" not-null="true" />
</property>
<property name="Sex" />
<many-to-one name="Mate" />
<bag name="Kittens">
<key column="mother_id" />
<one-to-many class="Cat" />
</bag>
</class>
</hibernate-mapping>
Frameworks: one vendor to rule them all
Desktop application
Once the platform of choice, the desktop is becoming less and less important with the rising of web application. Nevertheless, desktop applications are still significant and cannot be ignored.
For a cross-platform desktop application, you do not have much choice but to choose Java. But if your audience is Windows only you should consider using the .NET Framework. Two API are available: WinForm which is a wrapper around the old Win32 API and WPF which uses DirectX and provides vector graphics allowing elements to be scaled without loss in quality.
WPF employs XAML, a derivative of XML, to define the UI in a declarative way, allowing the developer to describe the interface and behavior without the use of procedural programming. The use of XAML provides a better separation of model and view and allows designers to contribute to the application development cycle more.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Xaml Example" Height="290" Width="300" Background="#eee">
<StackPanel Margin="10">
<ScrollViewer Height="200" Width="260" Margin="0 0 0 10"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto">
<TextBlock Text="{Binding TextContent}" Background="#fff"/>
</ScrollViewer>
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
<TextBox x:Name="TheLineTextBox"
Text="{Binding TheLine, UpdateSourceTrigger=PropertyChanged}"
Width="205"
Margin="0 0 5 0"
KeyUp="TheLineTextBox_KeyUp"/>
<Button Content="Enter"/>
</StackPanel>
</StackPanel>
</Window>
Desktop application development is an area where the .Net framework shines.
The Web
For a long time while the .NET community was dragging and dropping controls onto an ASP.net page, the Java community was building highly scalable web application using one of many web frameworks available. Here is the difficulty… information overload. If you take a look at the list of Java web frameworks, you can see there are many of them to choose from, and each one requires pretty extensive research to understand.
Until 2008, Web development in C# was analogous to ASP.Net and Web Forms. The main purpose of Web Forms was to ease the transition to the web development for people with a desktop development background. Same skills are transferable between the two, for example you can handle button clicks with events and delegates like you will do in a desktop application and all of the complexity is handled by ViewState and PostbackModel. The flaw of this is that it tries to inject state into what should be stateless: the web. The beauty of this model is its flaw too: by simplifying web development it abstracts and hides the inner bolts from the developer and produces developers who don’t understand the basics. Clean applications with separation of concern are feasible with Web Forms but require more discipline, everything is done to push you into using the RAD tools.
On the other hand, ASP.Net MVC, the new web framework of Microsoft, is a more lower-level component, closer to the standard method of Web Development. It looks like the other MVC frameworks that could be found in Java (Spring MVC) or in Ruby on Rails.
Rich Internet Application (RIA)
Both Java and .Net have competitors to Adobe Flex and Air. JavaFX is the RIA framework of Java, but it never took off.
Silverlight is the one for .NET, it is like a subset of WPF aiming rich internet application. It uses the same tool Visual Studio, Expression Blend, the same UI syntax XAML and a subset of the .NET Framework.
These last four years, Microsoft have brought Silverlight to the fore, releasing a new version at a rapid pace. That way it manages to reach a penetration rate of 73% (June 2011), which is quite good but still low compared to the 97% penetration rate of Flash.
The problem is that Microsoft seems to have a change of mind and they are now gone full in the HTML5 bandwagon, leaving the Silverlight developers doubtful. That’s the problem with languages and tools lead by a unique vendor, you are dependent of its internal politics.
Mobile
Mobile development is the new battleground, and I am afraid that Microsoft has already lost, killed by Apple and Google. The choice of language and platform is more driven by adoption than by convenience. It’s sad because the tools provided by Microsoft for Windows Phone 7 development are very nice: C#, Silverlight, XNA, Visual Studio, Expression Studio.
To sum up, nothing that can be done in Java that cannot be done in C# but you’ll have less choice in your framework choice in the .NET ecosystem than in Java. This is bad per se but avoids the information overload and the scattering that can be found in Java.
“A workman is only as good as his tools”
A comparison would not be complete without the comparison of the tools. As important as the framework, tools can make a hard job seems easy and inversely.
Visual Studio, the IDE from Microsoft, is the official choice for .Net development. It offers the same features that can be found in a Java IDE and some more like IntelliTrace, a historical debugger that records all events and allows the code execution to be rewound for debugging (an equivalent exist in Java: Eclipse Chronon), but also an architectural and modeling module allowing to draw a dependency graph of the application and check them (A bit like what we do in the Cockpit with the technical architecture) and more… Unfortunately, all these features have a cost and Visual Studio is neither free nor cheap.
Open source IDE exist too: SharpDevelop and MonoDevelop (develop by the Mono team), they have less features than Visual Studio but evolve quickly. The good point is that, unlike Java IDE, they all share a common project file, so you can switch back and forth between them. In Java, you will have to use a Maven POM file to have a similar feature.
Write once, run…somewhere
“Write once, run everywhere” is the slogan created by Sun to promote the cross-platform benefits of the Java language. The same slogan cannot be used for .Net but the situation is not as limited as people may think, C# and .Net in general are not for Windows only.
Silverlight
As I have explained earlier in this post, Silverlight is the Rich Internet Application framework of .Net, Silverlight application can run on Windows of course but also on Mac and Linux (the Mono team develops the linux port, under the name of MoonLight, with the cooperation of Microsoft). Silverlight is also used for Windows Phone 7 development.
XNA
XNA is a set of tools for game development and management in C#. Targeting indie game developers, it allows them to develop games in C# for Windows, Windows Phone 7 and the Xbox 360.
Mono
Mono is a free and open source project led by Xamarin (formerly by Novell) to create a .NET-compatible set of tools. Mono brings C# and the .Net framework not only on Linux but also on BSD, Solaris and even on the Wii.
The Mono team also develops MonoTouch that allows developers to create C# and .NET based applications that run on the iPhone, iPad, and iPod Touch devices, while taking advantage of the iOS APIs. As well as MonoDroid which is the equivalent of MonoDroid for Android devices and MonoMac for developing Mac OS application.
Netduino
Like for Java, the .NET framework comes in several edition:
- .NET Framework, equivalent to Java SE/EE
- The Compact Framework, equivalent to Java ME
- The Micro Framework, the smallest edition, targeting embedded device development. For example, a specific version of the Arduino (an open-source single-board microcontroller): the Netduino uses the Micro Framework.
It’s a wrap
In this post I have presented the .NET ecosystem which essentially offers the same features than Java but with a limited diversity/choice. Starting a project using .Net is more straightforward, you almost don’t have to search for a given solution. That can save you a considerable amount of time. But if the solution doesn’t fit your need, fewer alternatives are available.
In the last post of this series I am going to present the .Net community to see if it can match the very strong one of Java.
No related posts.

