C# for the Java Programmer

I’ve signed up to do a “C# for the Java Programmer” presentation on campus the week
after PDC. I have some ideas for what I’m going to cover, but I’d like your input.

What topics are the most important in this talk? What was the most non-obvious thing
that surprised you about C#?

Comments (15)

  1. Eric,

    Indexers, properties, events (although I am not a Java programmer, so I can’t say, but those are the two obvious differences).

    – Nick

  2. Mike Iles says:

    I think C#’s use of delegates is a big improvement over what Java has to offer. Event handling in Java is typically done by implementing a ‘listener’ interface. (What happens if the object wants to listen for the same events from two different sources?) The looser coupling of delegates is much more intuitive.

    (Anders Hejlsberg discusses this in http://www.artima.com/intv/simplexity.html.)

  3. Jason Goodwin says:

    Delegates are probably one of the biggest topics. They take the whole discussion of programming to an interface to the next level.

    Another unobvious thing, or one that is easy to overlook, is the difference between value types and reference types, particularly when it comes to boxing/unboxing, use as object ref parameters, or the source of a delegate. (hint, delegates coming from a structure has a really nasty surprise if you don’t know that the delegate keeps a COPY of the structure instead of a reference to it.)

  4. ALes Pour says:

    Explicit interface implementation, especially its consequences (C# spec, 20.4.1, paragraph 5):

    Because explicit interface member implementations are not accessible through class or struct instances, they allow interface implementations to be excluded from the public interface of a class or struct…


  5. Larry says:

    using vs. import, assembly binding, and versioning. Maybe not side-by-side in detail, but definitely assembly loading & GAC. I think this is the biggest difference for mental models.


    Indexers, +1.

    Of course, destructors & IDisposable. And you have to show Interop.

    Differences in initialization? Too much for a single lecture?

  6. RichB says:

    Double Checked Locking is not broken!
    Are inner classes (rather than scoped classes) vital for a good architecture?
    Static constructors don’t deadlock
    Does the enum keyword promote too much usage of the switch keyword, rather than good polymorphism?
    How the virtual keyword promotes IDE enhancements such as intellisense in 2003

  7. Zef Hemel says:

    Don’t forget about methods not being virtual by default and not having to catch any exceptions if you don’t want to (ie no throws BlablaException added to the method signature, just "RuntimeExceptions" as they’re called in Java).

  8. S N says:

    I feel that the following link may offer lots of leads on what would be best to say.


  9. I suggest to explain in some detail the role of the CLR in the Class Loading process, someday sooner or later these Java guys will want to implement a Custom Class Loader (yes, in some scenarios is very useful to load assemblies from other storages for example a database …), and they will find a big surprise because Java and the .NET are very differente about this.

  10. Victor Hadianto says:

    – Missing checked exception (which I still think after all this time is a bad thing 🙂
    – Indexer
    – Properties
    – Delegates
    – Namespaces != imports

    – CLR differences with Java Virtual machine (more into the platform rather than language)

  11. Michael Bertelsen says:

    Methoids is by default virtual in Java. I think kewords like override, new etc. is worth to look at for Java developers that switch to C#

  12. Not sure if this is just about the C# vs. the Java language syntax or also about the .NET class libraries vs. those of Java, but if you plan on addressing the latter as well, you will definitely need to talk about the fact that WinForms does not do MVC. Most Java programmers have gotten quite comfortable with the MVC approach to writing applications and will be surprised that .NET did not choose to take this approach – I know I was. (see also a post I did on that subject earlier this year at http://www.hutteman.com/weblog/2003/02/07-31.html )

    A short overview on why Microsoft decided against MVC as well as how you could potentially separate the Model from the View using WinForms would be very helpful – I wouldn’t mind it as a blog-entry either 😉

  13. Aman Ullah Mughal says:

    I feel that C# gives a good control for marshalling data by using pointers directly without any wrapper concept as in java.

  14. Richard Lowe says:

    I can’t believe only one person even mentioned attributes! Definately show a demo of build-in attributes (i dunno, even ‘little’ ones like ThreadStatic are cool!).

    Event handling vis-a-vis delegates, of course.

    Even address a few intricacies: like that fact that C#/.NET provide a memory barrier across locked calls so that threads will treat fields marked non-volatile as volatile, which is apparently not the case in Java. (this thread explained it to me: http://tinyurl.com/s0k0 so if I’ve misspoken: mea culpa).