O'Reilly logo

Professional C# 2005 by Allen Jones, Morgan Skinner, Karli Watson, Jay Glynn, Bill Evjen, Christian Nagel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required