Learning C#: Early Impressions

For the last couple of weeks, I have been spending time learning C# (the mongrel offspring of Java and C++, as I like to describe it). So far, I haven’t had much trouble, thanks to my strong background in Java, and I think I have done enough in a week to be able to compare the two languages and talk about the parts I like and the parts I don’t. Perhaps these will grow on me in the future, or it may not.

This is not a rant about ASP.NET, which I have also been learning at the same time. That will come in time.

Namespaces vs Packages

At the moment I am leaning towards Packages. Packages in Java are heavily enforced, by making sure the .java is placed in the right spot within the file structure of your project. Namespaces, however, allow the “assembly” to be placed pretty much anywhere in the project. Although it is possible to follow a strict convention like Packages, it is really up to the developer.

Strangely enough, C# does not use C/C++’s namespace system (where a namespace can be used anywhere in the code) by enforcing all namespace declarations to the top (a la Java Packages), having them apply to all classes in that particular assembly. In the end, however, minimal adjustments were needed for me to get used to the C# Namespace system.

Get/Set vs Accessors/Mutators

I have mixed feelings about C# Properties. On the one hand it provides straightforward access to properties of an object, while encapsulating object members properly. On the other hand, I feel it obfuscates the code to a small extent. Let us compare two snippets of code.

C#

1
Object.SomeBooleanValue = true;

Java

1
Object.setSomeBooleanValue(true);

In C#, the “Action” is not clear to me. Yes, we know it is assigning some boolean value to “true”. But if we look at the Java version, the method clearly states the intention. Furthermore, while C# Properties provides ways to implement read-only attributes, this is often unclear without the use of documentation. With Java, it is easy to see what operations are allowed with a particular class. This makes code easier to understand for me.

Another thing I have mixed feelings about is Interface Properties. While the intentions are sound, it breaks my concept of an Interface being a definition of functionality (as opposed to Superclasses, which should be a common set of properties). I suspect this is a “many tools for a single problem” syndrome that is often found in C++.

I may get used to Properties eventually, but it is not impossible to implement member access using methods instead of Properties.

C# Protected vs Java Protected

Here I have to say C# is the winner. Java’s Protected modifier was often used to provide member access to subclasses. However, it broke encapsulation by allowing access to other classes in the same package. C# follows C++’s version of Protected, by only allowing member access within the class, and derived classes only.

Subclass Declarations

Unfortunately, this aspect of the C# language is also inherited from C++ (pun intended). Java’s declarations are much clearer, and you can clearly see what are classes and what are interfaces, despite the more lengthly declaration. For example

C#

1
class A:B,C,D

Java

1
class A extends B implements C,D

Here we can see a problem. In C# there is no way to identify which is the superclass (or even if it does derive from one), and which are the interfaces, since there is no clear semantic. The “suggested” solution to this is to enforce a naming convention for Interfaces.

Again, a small problem, and I suspect C++ fans will enjoy their version alot more.

Conclusion

At this point in time, if I had to choose between the two, I would definitely go with Java. That’s not saying Java is better (the JVM is quite horrible, actually), but rather I prefer the language. As I like to say, “There is no way to go wrong in Java. If there is something wrong, its almost definitely your fault”. C# suffers the same “problems*” as C++; for a single problem it has many varied solutions. For example, I have never been a supporter of operator overloading. If someone overloads the + operator, how can you be sure that it adds two things together? You end up having to fight the language, by having to look through documentation all the time to figure out what it does.

Perhaps I will get used to it, perhaps not.

*I say “problems”, but they’re just my personal opinion.

Share this post: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Facebook
  • Google Bookmarks

3 Responses to “Learning C#: Early Impressions”

  1. I’m doing this from the other way around – learning Java after having a solid background in C#. I’d say its a more difficult that way, because C# gives you all kinds of nice constructs in the language, like delegates, event handlers, LINQ, lambda expressions, to make things easier (possibly at the expense of readability if you’re not used to it), which Java doesn’t have.

    About properties – I really missed these. If the lack of the ‘set’ keyword is confusing, declaring and setting variables must be confusing too :)

    As for knowing if something is read-only – I assume you’re using an IDE, in which case it will tell you if something is read-only by the icon and the popup hint. It takes time to get used to I suppose.

    The problem with properties is that it implies that it is just as simple as setting/getting a variable, when the get/set code might in fact be doing lengthy calculations, or has side-effects, neither which is conveyed in the syntax. Then again, that’s really abusing properties, and can be done in Java too.

    About subclasses – all .NET interfaces start with an I, e.g. ISerializable. This is by convention but is fairly strongly adhered to (and might even produce a warning in the IDE). I do like the Java syntax here though.

    And operator overloading – haven’t had the need to use it myself, but what about for matrix classes, or timespans? There’s a logical reason there. Someone would have to have a pretty good reason to operator overload though, and it should only replace functionality for specific cases (e.g. between two matrices, not anything else).

    Looking forward to hearing the rant on ASP.NET :)

  2. Welcome Sam to my humble home. I am glad to see your comment :)

    I suppose alot of these things are a matter of adjusting to the language and its “features” (something I reiterate alot in the post, as you might have noticed).

    As for the IDE… I could have a field day with that one! (Why doesn’t Eclipse have a .NET plugin!?!) Perhaps I will write about it when I finally use Visual Studio 2008, since it would be more relevant. From what I heard, not much has changed either.

    Again, thank you for your comment and I hope to see you around more :) Nice site btw.

    Cheers
    Daryl

  3. Java’s protected and default visibility are retarded.

    C++ and C# make more sense(not that I develop in either of those anyway),

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>