Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


 
Printable Version

Jump To C#
By Vyas Bharghava

When I started learning C# half the time I was trying to find Java equivalents so that I can learn effectively, leveraging my existing Java skills. At times I was so frustrated by my knowledge or lack there of.  I'm penning this piece in the hope that my experience might help Java programmers to learn Java's new cousin (or villain according to some view points!). I'll only deal with those functionalities which are present in Java but are modified / changed completely in C#. You don't have to worry about another article detailing Delegates & Events. :)

1. final vs. sealed 

final Keyword is used in three contexts in Java.
1. A final class is not available for inheritance
2. A final method cannot be overridden
3. A final instance variable is a constant

People will tell you that there's a equivalent new 'sealed' keyword.  Take it with a grain of salt. For there are pitfalls.

1. A sealed class cannot be inherited from.
2. A sealed method cannot be overridden
(pitfall: This functionality is only available in Beta 2)
3. Two news keywords are provided for the third Java functionality.

const int a = 5; // Compile time
readonly Singleton s = new Singleton(); // Runtime

2. throws

In Java, in method signatures, you wrote,

public void method() throws InvalidStateException {
}
If the above method is overridden in a child class that method was constrained from throwing any other exception except the one specified and it's subclasses.

By contrast, in C#, there is no functionality for mentioning what exception a method throws! ALL EXCEPTION IN C# are UNCHECKED EXCEPTIONS except those you catch with try... catch. The COMPILER DOESN'T FORCE YOU TO CATCH A POSSIBLE EXCEPTION.

3. Inner classes

In Java,

1. Private members of an outer class are accessible from the inner class.
2. There's a concept of enclosing instance
3. Static inner classes were actually akin to top level classes

In C#,

1. Inner classes are called Nested Types
2. Private members of outer class are accessible from the nested class but 
     the enclosing instance must be passed to the nested class.
3. In fact, C#'s Nested classes are akin to Static inner classes of Java
   (but even then you have to access members of the outer classes thru either an instance
   or class name in case of a static member)

4. Anonymous Classes

There are no anonymous classes in C#

5. Classes inside Methods

Not available in C#

6. Java differentiates between primitives and object references. 
   In contrast, C# has a unified type   system that boxes & unboxes on demand. 
   Hence you are free to treat all of them as though they were descendents of object.

7. Package vs. Namespaces

In Java, packages are physical directories where you were forced to organize your files.  
C# takes a more practical approach. Namespaces represent a logical hierarchy. When attach 
a URI to a namespace it becomes globally unique.

8. Assertions

With JDK 1.4, there's an assert statment has been added to Java.

In C#, you write Debug.Assert(a == 3, "Failsafe");

9. Switch Statement

In Java, fall-throughs were allowed. If case 2: did not have a break statement, case 3 will get executed.

In C#, no fall-throughs are allowed. One of the hottest debate among developers is whether switch should have been butchered this way.

Here are the differences:
1. Fall-throughs are allowed if the case statement contains one or more statements. 
    If it's a empty statement fall-throughs are allowed.
2. A goto can be used along with the case


int i = 2;

switch(i) {
    case 1:
        goto default;// allowed!
    case 2:
        Console.WriteLine(\"Yes, I'm Two\");
        goto case 4;  // new 
    case 3:
        Console.WriteLine(\"Hi, I'm three\");
        break; // must
    case 4:
        Console.WriteLine(\"Or Am I Four... I'm confused!\");
        break;
    default:
        Console.WriteLine(\"No.. is no one to 4\");
        break;  // must
}
10. super vs. base

In Java, you accessed parent class members in the child class by using super.method(); format. 

In C#, just replace the super with base, i.e.
base.method(), and viola! It works.

But (it's a big but), you also invoked super class' constructors using the
notation super() as the first line in the constructor:

Constructor() {
   


using System;

class SomeUserDefinedError: Exception
{
 // THIS IS HOW YOU CALL SUPER CLASS' CONSTRUCTOR
 public SomeUserDefinedError(string Message): base(Message)  
  {
  }
}

public class ThrowException {

 public static void Main(string [] args) {
  throw new SomeUserDefinedError("Great! I'm throwing errors");
 }

}
I can go on and on (I've at least another 15 points) but I've to wind up this article. 
Catch you in my next one. Play C#.