Java to C# Comparison

by Brett on November 22, 2006

The first high-level language that I learned was Java. Of course I shortly found that there was a need to know C# too. So, as I started  to learn C# I kept a list of the differences between C# and Java (as can m be seen below). 
    Java: class GMTTime extends Time implements ICloneable{...}
    C#: class GMTTime: Time, ICloneable{...}
    Java: GMTTime(){
       GMTTime(long millis): base(millis){...}//if base not specified default constructor

Java            =       C#
    Finalizer     =       destructors //spedifies code that will execute once an object is no
                                    //longer usable
    Package       =       namespace
    import        =       using
    StringBuffer  =       StringBuilder
    indexer: allows a class to be accessed as an array
    struct: lightweight class, value type instead of reference type (like class)

Method Inheritance
    class BaseClass{
        //virtual is required  to allow this method to be overridden by a sub class
        virtual long GetTime(){...}

    class childClass: BaseClass {
        //override allows an object to be a base class (BaseClass) but will still use
        //this method
        override long GetTime(){...}

    class childClass2: BaseClass {
        //new overwrites the method but uses the method of the class that the calling
        //object is.
        new long GetTme(){...}

Java Final
    class = sealed //can not be inherited
    method = no virtual
    variable = const or readonly

    public = visible to all
    protected = only derived classes
    private = only withen given class
    internal = visible only to project/assembly/jar file
    protected internal = visible only to project/assembly/jar file and derived classes
    default = private

    //new C# way...old way used getter and setter methods
    class Circle {
         private int radius;
         public int Radius {
              get {return radius;}
              set {radius = value;}

         public int Area {
              get { return 3.14 * radius * radius; }

    class otherClass{
        Circle c = new Circle();
        c.Radius = 3;//Note: no need to call a setter method.
        int area = c.Area;//Note no need to call a getter method

synchronous = responds immediately
asynchronous = responds when ready to.

How to do events in C#
1. Manually Trigger Event
    //namespace level, also specifies signature for event/handling methods
    public delegate void MyEventHandler();
2. Create an Event (requires a delegate)
    public event MyEventHandler TriggerIt;//class level
3. Register the event handlers with the event
    obj.TriggerIt += new MyEventHandler(obj.method);
4. Manually Trigger Event

// Declare the delegate handler for the event:
public delegate void MyEventHandler();//determines signitures of handling methods.

class TestEvent{
    // Declare the event implemented by MyEventHandler.
    public event MyEventHandler TriggerIt;

    public void MyMethod1(){
    public void MyMethod2(){
        System.Console.WriteLine("Hello again!");
    public void MyMethod3(){

    static void Main(){
        TestEvent myEvent = new TestEvent();

        // Subscribe to the event by associating the handlers with the events:
        myEvent.TriggerIt += new MyEventHandler(myEvent.MyMethod1);
        myEvent.TriggerIt += new MyEventHandler(myEvent.MyMethod2);
        myEvent.TriggerIt += new MyEventHandler(myEvent.MyMethod3);
        // Trigger the event:

        // Unsuscribe from the the event by removing the handler from the event:
        myEvent.TriggerIt -= new MyEventHandler(myEvent.MyMethod2);
        System.Console.WriteLine("\"Hello again!\" unsubscribed from the event.");
        // Trigger the new event:

typeof, GetType, is
    Type s = typeof(String);
    Type s = "my String".GetType();
    "my string" is String //returns true

    int[] arr = new int[]{1,2,3);
    int[] arr = {1,2,3};
    foreach(int i in arr){
        Console.WriteLine("Value is {0}",i);

Previous post:

Next post: