Friday, November 4, 2011

Difference between C# and Java

Following are some important and key difference in the language C# and Java!!!

#10 – Give me my standard output!

This may not seem like a big deal, but when I’m first getting my head in a language, I want to be able to debug. With everything so new and shiny, I don’t want to hear about the debugger yet, I don’t care about the fancy message boxes, just tell me how to get something on standard output!
In C#, the code looks like this:
Console.WriteLine("your string here");

#9 – Namespaces == Freedom

In Java, the package hierarchical structure mirrored the directory hierarchical structure. This made sense to a degree, but then why didn’t Sun just auto-derive the package structure? Anyway, Microsoft has freed us from this constraint. The C# package structure is defined using namespaces (just like Java), but the namespaces do NOT have to reflect the directory structure.

#8 – What happened to super?

Slight renaming of keywords drives me bonkers! Substitute Java’s super keyword with base.

#7 – Chaining constructors to a base constructor

In Java, I often created a set of constructors with differing parameters that all reference a common all-powerful constructor. In other words, I created a set of overloaded constructors that basically just called a constructor that did the bulk of the work. This is called constructor chaining and normally leads to easier to maintain code. In Java, the this(…) statement is used to call the constructor. In C#, this is done right up in the method signature.
public MyConstructor(int i) : this(i, -1) {     ... }  public MyConstructor(int i, int j) {     ... } 

#6 – Dagnabit, how do I inherit?

In Java, the keyword extends is used to inherit from a parent class, and the keyword implements is used to inherit an interface. In C#, the distinction does not exist and it’s all done in the method signature.
public class MyClass : MyParentClass {     ... }

#5 – Why don’t constants remain constant?

Well of course they do! Just not among different languages. In Java, I normally defined global constants using public static final. In C#, the static keyword does exist (final does not), but you can define constants like this:
public const MyConstant = 101;
When the constant needs to be initialized at run-time (for example in a constructor), use the readonly keyword instead of const.

#4 – Where is ArrayList, Vector or Hashtable?

In Java, these guys were my staples. Pre-built dynamic arrays and look-up tables decrease development time and makes my life a whole lot easier, but where are they in C#? C# has an ArrayList, but it doesn’t make use of generics. Instead, I like to use:
Unfortunately, List<> is not thread-safe (C#’s ArrayList and Java’s Vector are thread-safe). C# also has a Hashtable; the generic version is:

#3 – Of Accessors and Mutators (Getters and Setters)

As long as methods exist in classes, accessors and mutators will exist. However, C# largely replaces them with class properties. Instead of the traditional getSomeInteger() and setSomeInteger(), we use properties!
public class SomeClass {     int someInteger = 42;      public int SomeInteger     {         get { return this.someInteger; }         set { this.someInteger = value; }     }      public void Main(string[] args)     {         SomeClass c = new SomeClass();         Console.WriteLine(c.SomeInteger);         c.SomeInteger = 420;     } }
this.someInteger is a class variable, and value is the new integer.

#2 – I can’t override!?

At this point, we’ve transcended the syntax differences and we’ve entered into more philosophical choices that the Microsoft team made. In Java, all methods are by default virtual and you can override them. In C#, all methods are non-virtual. To override a method in the parent class, make sure the method of the parent class is defined as virtual using the virtual keyword.
class MyParentClass {     public virtual void MyMethod() { ... } }
In the child class, the method must use the override keyword.
class MyChildClass : MyParentClass {     public override void MyMethod() { ... } }

#1 …

I remember when I first learned Java, and everyone was saying how Java made everything an object. How Java forced you to make classes for everything. This turns out to be partially true. It is necessary to make classes. However, primitives are not objects. In C#, even what were considered primitives in Java are objects in C#. Absolutely everything descends from System.Object. This choice by the C# team allows for greater consistency and for such things as a cleaner implementation of automatic boxing/unboxing.
Post a Comment