C# for Java Developers, Part I

C# (pronounced see sharp) first appeared in the year 2000, 5 years after Java. It’s currently at version 7, which was released in March this year. The language is similar to Java in many ways, but it’s also very different. Both Java and C# are typed statically and strongly, object orientated, use curly braces to define scope, and semi colons to indicate line endings. While they have similar syntax, the C# syntax differs enough from Java that it might look a bit confusing to a long time Java developer at first sight.

The structure of this guide is as follows: Each hand picked topic consists of three sub sections. One code sample from each camp, and then some quick bullet points that cover what I consider the most important takeaway from the particular subject. All code you see is also available in the C# for Java Developers GitHub repository.

A word of warning: I’m writing this guide as I learn C# myself, so don’t except everything to be 100% accurate. Consider reading this as a way to kick start your own C# adventure.

Without further ado, let’s jump in the deep end of the pool.

Code structure

We’ll begin like we always do when we’re learning a new language: Hello, World! It’ll fill us in on the basics. First, your familiar piece of Java code, then the mystery that is C#:

package net.vegard.csharpforjavadevs.structure;

public class Example {

    public static void main(final String args[]) {
        System.out.println("Hello, World!");
    }

}
using System;

namespace VegardNet.CSharpForJavaDevs.Structure
{
    class Example
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

Classes

In it’s most old-fashioned form, C# classes look almost exactly like Java classes1.

package net.vegard.csharpforjavadevs.classes;

public class Student {

    private String name;

    public Student(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}
namespace VegardNet.CSharpForJavaDevelopers.Classes
{
    public class Student
    {
        private string name;

        public Student(string name)
        {
            this.name = name;
        }

        public void SetName(string name)
        {
            this.name = name;
        }

        public string GetName()
        {
            return name;
        }
    }
}

Interfaces

There are no big differences between an interface in Java, and an interface in C#.

package net.vegard.csharpforjavadevs.interfaces;

public interface Reportable {
    void generateReport();
}
namespace VegardNet.CSharpForJavaDevelopers.Interfaces
{
    interface IReportable
    {
        void GenerateReport();
    }
}

Wrap up

That’s it for now. It’s important that your first bite of the C# cake isn’t too big, or you’ll get a bellyache. So far, this looks like a solid case of Same Shit, Different Wrapping. But don’t despair. First, we need to cover the basics. Then, we’ll get to the juicy stuff.

If you have any corrections, information you think can be useful, or just general praise, please post a comment.

Part II of this guide will be published When It’s DoneTM.


  1. Yes, I know the C# example below can be written a lot more elegantly. I’ll get to that later, so please hold your angry comments and hate mail. ↩︎


Feedback

Do you have any thoughts you want to share? A question, maybe? Or is something in this post just plainly wrong? Then please send an e-mail to vegard at vegard dot net with your input. You can also use any of the other points of contact listed on the About page.