Professional C# 2005
by Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner, Allen Jones
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access