Appendix C. C# for Java Developers

At first glance, Java developers might not get particularly excited about C# code, because of the syntactical similarity between it and Java. However, look more closely and you will see subtle yet significant differences: features such as operator overloading, indexers, delegates, properties, and type-safe enumerations in C#.

This appendix focuses on applying much-loved Java programming tricks to C# code, highlighting features that C# adds to the picture, and pointing out tricks that C# cannot do (although you won't find many of those). Of course, it is assumed that as a reader of this appendix, you are a professional Java developer, so this appendix does not go into too much detail when describing the Java language.

Starting Out

Take a look at the infamous "Hello World!" example in Java:

public class Hello {
  public static void main(String args []) {
  System.out.println("Hello world! This is Java Code!");
 }
}

The corresponding C# code for this is as follows:

using System;
public class Hello
{
  public static void Main(string [] args)
  {
    System.Console.WriteLine("Hello world! This is C# code!");
  }
}

The first thing that you'll notice is that the two appear to be very similar syntactically and both languages are case-sensitive. C# is object-oriented like Java, and all functionality must be placed inside a class (declared by the keyword class). These classes can contain methods, constructors, and fields just as Java classes can, and a C# class can inherit methods ...

Get Professional C# 2005 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.