C# Oddity article (for Java people) would be a good idea

July 21, 2008

A lot Java programmers believe C# is just a bastard version of Java. On first appearances, especially looking at C# 1.0, it could look that way. Regardless, the language has evolved through C# 3.5, and it has a number of language features that would be odd to a heads down Java programmer., but may actually be useful. Maybe an article highlighting these would be a good idea (maybe there is a good one, I haven’t searched for it yet).

One article that explains the ?? operator in C# (what the ?? to a Java programmer):

Since C# 2.0, there is new operator called null coalescing operator (??) is available to provide inherent null-checking capabilities. It returns the left-hand operand if it is not null, or else it returns the right operand. For example, the equivelent statement using null coalescing operator (??) for the previous code is:

string employeeName = “Dave Gate”;

Console.WriteLine(employeeName ?? “Employee name not specified”);

The result of the expression is based on whether the first operand is null. If the first operand is not null, it is the result of the expression, otherwise, the result falls to the second operand. In other words, return thefirst value if not null, otherwise the second.

Granted in Java you could code something like this:

String it = “hello”;
String x = it == null ? “not hello” : it;

But the ?? gives you cleaner syntax. It’s up to a programmer if in this case this is valuable or not.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: