Tuesday, May 26, 2009

First Session

Yesterday we had the first session of our study group. We prepared Chapter two of the book, and discussed items that seemed especially of interest, surprising or unclear for at least one of the group.
Not very surprisingly, few things were unclear, as it was a detailed refresher of basic parts of C# 1.
Still, for me as a beginner in C# (still less than four months of experience), I got a lot out of the book as well as the discussion.
Some were minor points, as the aggregation of delegates and also their immutability. Furthermore that delegates prevent garbage collection of the instance (if delegate instances) is pretty obvious, but previously it never occured to me.

A surprise to me was (as I never started with C# 1), that covariance and contravariance was a problem when dealing with delegates (signature had to be exactly the same). When discussing this, we also had to clarify the meaning of the words covariance and contravariance.
In simple words: covariance is "cast" to a more general object, contravariance to a more specific.
I.e. a cast from string to object would be covariant, object to string contravariant.

When talking about Extensions (which were new to me), we discovered that the usage of the string type is not completely intuitive to us. Methods like string.Format() are not usable like "hello {0}".Format("world"); The reason we came to this was that you could provide this behavior in writing an Extension for string.

When talking about the handling of strings, we also came to the discussion, if a parameter of a method is a string, sometimes you check it for null and emptyness.
You could throw two different, separate exceptions for this. Our feeling was though, that this would increase work, without really giving anyone that much information. If a string that is null or empty produces an exception the information whether it is null or empty is not that crucial. Although my feeling is that there are people who might not concur.


  1. How clear was the discussion of covariance/contravariance? It's always a tricky topic to talk about precisely, in my experience.

    As for the "null and emptiness" - I would probably throw ArgumentNullException for null, and just ArgumentException otherwise. In some ways this "is" the same exception in that ArgumentNullException derives from ArgumentException, but it gives more consistency with other methods IMO.

    You're absolutely right about the extension method option for formatting - there are lots of times when that would be handy. Mind you, I've said elsewhere that I'd rather like a "Template" class or something similar which could parse the format string once and then be reused :)


  2. We did not really have a discussion about covariance/contravariance. I think neither of us was familiar with the words, so we just took the wikipedia article. Due to this, I did not seem tricky to me (and neither the others as far as I can tell). Problem is, we might have gotten only a fraction of it.

    What about ArgumentOutOfRangeException if string is empty? As it is only an Exception if an empty string is not valid, and thus out of range of the expected/valid value range.

    I'm not sure if I understood your Template class correctly. Wouldn't that result in a static Template class? And wouldn't that be the same as string.Format()?

  3. ArgumentOutOfRangeException: I prefer to keep that for genuine ranges (where I can say it was "too big" or "too small"). Just ArgumentException is normally okay, IMO.

    The template class wouldn't be a static class. You'd do something like Template t = new Template ("Name: {0} Date of Birth: {1}"); t.Format("Jon", new DateTime(...))";. That way the parsing of the format string only needs to happen once, and you could then format with that template frequently. String.Format has to parse the format string every time you use it.