Java vs. C# Code Comparison

C# is Microsoft's new object-oriented language that has many similarities to Java and C++.  C# is one of the major building blocks of the Microsoft .NET initiative.  More information on .NET and C# can be found at:

Example 1: Shape Hierarchy

The .NET Software Development Kit (SDK) or Visual Studio.NET is needed to compile these examples.
 
Java Files C# Files
Shape.java Circle.cs
Shape2D.java circle.exe
Circle.java

We need to create a hierarchy of Shape classes.  The abstract Shape class is at the top, and it contains two methods: getUnitOfMeasurement() and setUnitOfMeasurement()Shape2D is an abstract class that extends Shape, and it defines two abstract methods: computePerimeter() and computeArea()Circle is a concrete subclass of Shape2D that takes a radius in it's constructor that's used for computing the two formulas.  The Circle class takes a radius and unit of measurement as program arguments, and prints out the perimeter and area of the instantiated Circle.

The C# compiler, csc.exe, is used to compile the Circle.cs file.  By default, csc will create an executable file that has the same name as the C# file.  Here's the full command:

C:\IndyJUG>csc Circle.cs
Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0.2914]
Copyright (C) Microsoft Corp 2000-2001. All rights reserved.

Example 2: Graphical User Interface

This example also requires the Java and C# source files from the first example.
 
Java Files C# Files
ShapeGUI.java ShapeGUI.cs
circle.netmodule
shapegui.exe

We need to create a GUI that takes in the radius of a circle as a parameter and outputs the perimeter and area of that circle when a button is pushed.

In order to use the C# Circle class from above, we need to link that source code in with the ShapeGUI.  Here's the full command:

C:\IndyJUG>csc /out:shapegui.exe ShapeGUI.cs /out:circle.netmodule Circle.cs
Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0.2914]
Copyright (C) Microsoft Corp 2000-2001. All rights reserved.

ShapeGUI.cs(169,7): warning CS0168: The variable 'ex' is declared but never used

We can safely ignore the warning that csc generated.  The /:out parameter specifies the output file name for a source file.  The Circle class and it's ancestors are stored in a netmodule named circle.netmodule.  The shapegui.exe file references this netmodule at runtime.