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...
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.
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);
static final int K = 100;In C#, compile-time constant values are declared inside a class as
const int K = 100;
boolean.
In C# this is shortened to bool.
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.
| Unsigned | Signed | Size |
|---|---|---|
| byte | sbyte | 8 bits |
| ushort | short | 16 bits |
| uint | int | 32 bits |
| ulong | long | 64 bits |
>> 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.)
class B extends AIn C# the
extends keyword is replaced by a colon, same as in C++
class B : A
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 harmfulswitch 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.