Initial C# impressions

(This was originally meant as a comment for Janus’s post about C#, but it turned out to be longer than that…)

Mikkel and I have to learn C# for understanding the code from SCET, a project running before our project. Anyway, we’ve been looking at a tutorial, the one over at http://csharp-station.com/ (just a random one).

And so far my impression of C# is that it is like C++ with garbage collection (so it’s a bit like Java) and with new keywords such as ref, out, etc for parameter passing. The ref is nice and clearer than pointers or C++ references so that’s good.

But the out keyword? I think it’s ugly. First it’s not necessary, just pass a ref parameter and have the method update it. A better solution would be to let methods return multiple values in a — ohh — tuple! That cannot be so hard, especially because it could be implemented as syntactic sugar for the out parameters. Several languages (Python, ML and Haskell comes to mind) have such a construct and it is very useful.

Also, who uses goto these days? I think that is a bit too low-level. But maybe it reflects that C# is mostly C++ with a standard garbage-collector?

The choice of having methods being non-virtual as default is also unfortunate. For you simply cannot predict if a given method will need to be specialized in the future. It hurts extendability if you have to go change the base class when you want to specialize a function in an derived class — the source for the base class might not even be available in which case you are stuck. The efficiency concern of virtual methods is not valid, IMHO. First, the compiler should be able to look at the class tree and note that method foo() isn’t redefined and thus make code that jumps to foo() just as efficiently as if it had been declared non-virtual. Second, the extra pointer indirection incurred for virtual functions shouldn’t be a problem with our modern fast CPUs and with normal programs that spend 99.9% of their time waiting for user input anyway.

In the book Object-Oriented Software Construction it is described how it is always right to call the redefined method B::foo() and often wrong to call A::foo() if class B extends A and if you want to take advantage of polymorphism. The problem is with invariants: since foo() is redefined in B it presumeably adds some functionality to A::foo(), it might update some internal state in the object to maintain a class invariant in B. Class A does not have the same class invariant as B and so calling A::foo() destoys it whereas calling B::foo() would have been the right thing to do. The method B::foo() is always safe to call since it must fulfill both the invariants of class A but also of B. Even though class invariants is something you mostly see in Eiffel code, the principle applies to all languages.

I also don’t get why delegates was invented — aren’t we just talking about a function pointer? It seems that I can declare a delegator for comparing numbers like this

public delegate int Comparer(object obj1, object obj2);

after which I can say

Comparer cmp = new Comparer(CompareFirstNames);

when the CompareFirstNames() method has a signature matching with the delegate just declared. One can then of course pass cmp to a sort() method as usual. But why could I not just pass CompareFirstNames to sort() directly? Why do I need to make a new delegate type (if it is a type, I’m not really sure…)? The notion of agents in Eiffel seem much more elegant and powerful. I would be happily surpriced if someone tells me that you can pass a semi-instantiated function as a delegator in C#. Agents in Eiffel work much like lambda functions in functional languages, giving you the possibility to easily adapt functions for use as callbacks. (Agents are described and compared with .NET delegates here, see also the OOSC course homepage.)

That was a lot of initial comments about what I’ve seen from C#. Don’t take this as meaning that it’s all bad — the idea of properties is a good one, for example. A garbage collector is also something which I’ve come to expect from a modern language, so that too is a welcome change from C/C++.

16 Comments

  1. Michel Fortin:

    You may also be interested in taking a look at D which is another language that can fit well your definition of a more modern C++ that looks a little like Java. I think D will become one of my favorite languages in the comming years.

  2. Mikkel:

    I think some of the design decisions might make more sense in a bigger context. I think I would need more knowledge of and experience with the language before drawing too many conclusions. But I can certainly see what you mean.

    Another thing. I’m sitting here in my office, and some guy has been going back and forth right outside it for like 10 minutes now. He’s on some kind of red motorized bicycle inspired vehicle. Very suspicious.

  3. Martin Geisler:

    Thanks for the tip, it looks very cool, sort of a cross between C++, C#, Java, and Eiffel. I like the idea of having support for invariants in the code as in Eiffel, writing these things down explicitly is important.

  4. Martin Geisler:

    Yes, you’re right — none of us have actually worked with the language yet… As for the bigger perspective, then I think it’s clear that C# had to be sort of backward compatible with C++ so that people can port their code easily.

  5. Kristian Kristensen:

    I guess you’re kind of forcing me to make a follow up post, being that I’m a Microsoft Student Partner.
    Alas, it won’t be now :-)

  6. Martin Geisler:

    Tihi! :-D You have to believe me when I say that it was not my intention to provoke you, this post is borne out of the questions Mikkel and I had yesterday when we were looking at this new member of the language zoo.

  7. Janus:

    hen I think it’s clear that C# had to be sort of backward compatible with C++ so that people can port their code easily.

    I think they would port it to C++ for .NET instead - and then maybe write the rest of the code in C# or java for .NET (J#) instead :)

  8. Martin Geisler:

    Aha… so there is another language, something like C++# or what? I sort of assumed that C# was meant to be the .NET-language of choice for former C++ programmers.

  9. Janus' Weblog:

    C# Clarification

    First of all, I think C# is closer to Java than C++ but it is very blurry because C# seems to incorporate the best of both languages (especially in C# 2.0).
    In C# there are a few new keywords for passing parameters to functions: ref and out. The differ…

  10. Martin Geisler:

    It seems that I’m not the only one who has wondered why C# has both ref and out. Having such an “Ask a Language Designer” section is a very nice idea!

  11. Janus:

    If you have to give Microsoft credit for one thing - it must be the quality of their developer documentation. So, of course they have a “Ask a Language Designer” :)
    If you want to see some really cool addition to C# that Anders Hejlsberg is working on - google for LINQ.

  12. Martin Geisler:

    Yeah, I heard about that… SQL-like queries embedded in your code. If I remember correctly there was also something about Xpath for querying XML files. It will be interesting to see how it turns out.

  13. Kristian Kristensen:

    Just to make a few clarifications.
    J# is an implementation of the Java language for the .NET platform. That is J# has the syntax of Java, but uses the API’s of .NET.
    “C++#” as you call it :-) Is called Managed C++, and is an implementation of C++ for the .NET platform. That means it is compiled to Intermediate Language (IL), and executed on the CLR.
    Actually Managed C++ is an equal member of the .NET platform, and valid alternative for other languages. It includes some pretty nifty things such as determistic destructors/finalizers.

    “In old times” there were also a language called J++ (one of the first that Anders Hejlsberg worked on at Microsoft), which was a Microsoft implementation of Java for Windows. It never really caught any mainstream attention.

    LINQ is pretty cool! The Database relevant part is called DLINQ, the XML relevant part XLINQ. With a background in C-Omega, a Micrsoft Research project, it introduces some of the things that’ll be included in C# 3.0.

  14. Martin Geisler:

    Hi Kristian, thanks for the explainations. I had seen this Managed C++ thingy mentioned in a couple of places, but I didn’t realize that it was a language in it’s own right.

  15. Peter Kehl:

    Martin,

    as you mention Eiffel’s Tuples and Agents, you may be interested in their implementation for Java. It also includes Collectors - a reusable abstraction of Agents. See http://reuse.sourceforge.net/eiffel

  16. Martin Geisler:

    Sounds interesting… I haven’t looked at it yet, but having tuples and agents in Java would surely be useful.

Leave a comment