C# for Java Programmers

Links to other online resources

My personal experience

Quite recently, I had to implement several programs in C#, using the Microsoft .NET platform. Although I had no prior experience of programming in C#, I knew all about Java - so I thought that there would be few problems, since, as everyone says - C# is almost exactly the same as Java.

Here are a list of the interesting (and annoying) differences that I have discovered so far...

The main thing ...

In Java, the program entry point is the main method, which should have the signature:
public static void main(String[] args)
In C#, capitals are slightly different, and the signature looks like:
static void Main(string[] args) 
Note that string is shorthand for the System.String class in C#. Another interesting point is that in C#, your Main method can actually be declared to be parameterless i.e.
static void Main()
but then you can't pass any command line arguments to your program.

printing to stdout

In Java, output text to the standard output stream like this:
System.out.println("Hello world!");
In C#, do this instead:
System.Console.WriteLine("Hello world!");
Note that there is a major difference here. Java's System is a class, whereas C#'s System is a namespace, and thus is often left out, so the following suffices:
Console.WriteLine("Hello again!");
C#'s WriteLine() is a little like C's printf() So in C you could say things like
printf("Value of local variable i is %d\n", i);
In C# this is equivalent to:
System.Console.WriteLine("Value of local variable i is {0}", i);
Of course, in Java, we can do this trick by string concatenation:
System.out.println("Value of local variable i is " + i);

A constant problem ...

In Java, compile-time constant values are declared inside a class as
static final int K = 100;
In C#, compile-time constant values are declared inside a class as
const int K = 100;

True or false?

The boolean true/false primitive type in Java is called boolean. In C# this is shortened to bool.

Sign here

In Java, all integer primitive types (byte, short, int, long) are signed by default. There is no way to change this either. In C# there are both signed and unsigned varieties of these types.
UnsignedSignedSize
bytesbyte8 bits
ushortshort16 bits
uintint32 bits
ulonglong64 bits

Shifty behaviour

In Java, arithmetic right shift (replicate top bit) is the >> operator and logical right shift (shift in 0's) is the >>> operator. In C#, there is only one right shift operator, >>. This behaves as arithmetic right shift for signed integers, and as logical right shift for unsigned integers. (You can always cast types if you want the other kind of behaviour.)

Now then children

A subclass in Java is declared as
class B extends A
In C# the extends keyword is replaced by a colon, same as in C++
class B : A

A method in our madness

Methods are virtual by default in Java. That is to say, any method declared in class A can be overridden by a subclass of class A. Methods must be explicitly declared as final if they are not to be overridden. In C#, the default is for non-virtual methods. That is to say, subclasses are not allowed to override methods of their parent class. To allow overriding, the parent class must explicitly declare its methods to be virtual which means that a subclass may provide an overriding definition of this method, although it doesn't have to do so. Note that if a subclass does define an overriding method, then that method must be declared using the override modifier.

switch fall throughs considered harmful

In C#, non-empty cases within switch statements must end with a statement that causes a control-flow transfer. Falling through to the body of the next case clause is not allowed (unlike Java/C/C++). Usually cases end with a break; although they can end with goto case X; where X is another case in the switch statement.