C# Versus Java by Marc Eaddy Example 1: (a) HelloWorld.java class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } (b) HelloWorld.cs class HelloWorld { public static void Main(string[] args) { System.Console.WriteLine("Hello, World!"); } } Example 2: Value types. (a) Java; (b) C#. (a) int i = 10; System.out.println((new Integer(i)).hashCode()); // Use wrapper class (b) int i = 10; System.Console.WriteLine(i.GetHashCode()); // Conversion is implicit Example 3: (a) String name; public String getName() { return name; } public void setName(String value) { name = value; } obj.setName("Marc"); if (obj.getName() != "Marc") throw new Exception("That ain't me!"); (b) string name; public string Name { get { return name; } set { name = value; } // 'value' is the new value } obj.Name = "Marc"; if (obj.Name != "Marc") throw new System.Exception("That ain't me!"); Example 4: (a) // Create an object array Object args[] = { "File not found.", new Integer(2) }; // Format the message System.out.println( java.text.MessageFormat.format("Error: {0} (Code {1})",args)); (b) System.Console.WriteLine("Error: {0} (Code {1})", "File not found.", 2); Listing One using System.NewXml; using System.Xml.Serialization; [XmlRoot("album", Namespace="music")] public class Album { [XmlElement("artist")] public string artist; [XmlElement("title")] public string title; [XmlArray("songs"), XmlArrayItem("song")] public string[] songs; } Listing Two using System.Xml.Serialization; using System.IO; class TestAlbum { public static void Main() { Album album = new Album(); album.artist = "Sasha"; album.title = "Xpander"; album.songs = new string[5]; album.songs[0] = "Xpander Edit"; album.songs[1] = "Xpander"; album.songs[2] = "Belfunk"; album.songs[3] = "Rabbitweed"; album.songs[4] = "Baja"; // Serialize the object to a file FileStream fs = new FileStream("Album.xml", FileMode.Create); XmlSerializer serializer = new XmlSerializer(typeof(Album)); serializer.Serialize(fs, album); } } Listing Three Sasha Xpander Xpander Edit Xpander Belfunk Rabbitweed Baja Listing Four using System.Diagnostics; class ConfigFile { bool isFileOpen; public void Open(string strFile) { // Pre-conditions Debug.Assert(!isFileOpen, "Config file already open.", "You can only call Open() once."); Debug.Assert(strFile.Length > 0); isFileOpen = true; // ... } public static void Main() { ConfigFile file = new ConfigFile(); file.Open("Joe.xml"); file.Open("Joe.xml"); // Causes an assertion! } } Listing Five using System; delegate void PriceDecreasedDelegate(string name, long newPrice); // Called when the price drops delegate void PriceIncreasedDelegate(string name, long newPrice); // Called when the price increases class Stock { // Holds the price of a stock public Stock(string stockName, long startPrice) { name = stockName; price = startPrice; } public long Price { get { return price; } set { if (value > price && Fire_OnPriceIncreased != null) { Fire_OnPriceIncreased(name, value); // Inform listeners } else if (value < price && Fire_OnPriceDecreased != null) { Fire_OnPriceDecreased(name, value); // Inform listeners } price = value; // Update the price } } // DATA string name; long price; public event PriceDecreasedDelegate Fire_OnPriceDecreased = null; public event PriceIncreasedDelegate Fire_OnPriceIncreased = null; } class StockTracker { // Outputs a message when the stock price changes public StockTracker(Stock stock) { // Connect the Stock events to our event handlers stock.Fire_OnPriceDecreased += new PriceDecreasedDelegate(OnPriceDecreased); stock.Fire_OnPriceIncreased += new PriceIncreasedDelegate(OnPriceIncreased); } // Signature of event handler must match the // PriceDecreasedDelegate delegate declaration private void OnPriceDecreased(string name, long val) { Console.WriteLine("Price of {0} dropped to {1}!", name, val); } private void OnPriceIncreased(string name, long val) { Console.WriteLine("Price of {0} rose to {1}!", name, val); } } class StockTester { // Test the events public static void Main() { Stock ibm = new Stock("IBM", 100); StockTracker tracker = new StockTracker(ibm); // Update the stock price ibm.Price = 125; // Outputs "Price of IBM rose to 125!" ibm.Price = 90; // Outputs "Price of IBM dropped to 90!" } } Listing Six import java.util.*; class Iterate { public static void main(String[] args) { // Enumerate command-line args array for(int i = 0; i < args.length; ++i) System.out.println(args[i]); // Create a linked list LinkedList list = new LinkedList(); list.add("Cube Farm"); list.add("Sasha & Digweed"); // Enumerate the list ListIterator it = list.listIterator(0); while (it.hasNext()) System.out.println(it.next()); } } Listing Seven using System.Collections; class Iterate { public static void Main(string[] args) { // Enumerate command-line args array foreach (string arg in args) System.Console.WriteLine(arg); // Create a linked list ObjectList list = new ObjectList(); list.Add("Cube Farm"); list.Add("Sasha & Digweed"); // Enumerate the list foreach (string str in list) System.Console.WriteLine(str); } } 4