List<T>.ForEach vs foreach(…)

A thread came up yesterday on the C# newsgroup about when to use the “normal” foreach and when to use List<T>.ForEach (assuming, of course, that one is dealing with a List<T> in the first place). Obviously there are readability issues, but we ended up focusing on performance. (Isn’t that telling in its own right? How often is the iteration part rather than the body going to dominate and be a significant bottleneck? Anyway, I digress.) So, I wrote a small benchmark, and Patrick asked me to blog about it. I’ve refactored the test I posted on the newsgroup and added … Continue reading List<T>.ForEach vs foreach(…)

Singletons and inheritance

For a long time, when people have asked about having inheritance and singletons, I’ve stated flatly that a singleton can’t be derived from. It stops being a singleton at that point. Even if the class is internal and you can prove that no other classes in the assembly do derive from the singleton and break the pattern’s effect, it’s still not a genuine singleton. It was only when I was thinking about one of the comments about my enhanced enums proposal that I realised there’s an alternative approach. You can derive from a singleton as nested types of the Singleton … Continue reading Singletons and inheritance

ICloneable? Not quite…

I don’t usually post personal news on my blog, but this is fairly major. Some of you may know that my wife, Holly, is pregnant. What you won’t know – and what we didn’t know until today – was that we’re having twins. Eek! A lovely surprise, if somewhat scary. For those of you who like piccies, the scans are on my web site… Now if you’ll excuse me, I think I’ll go and lie down. And to think I was going to write up C# 2.0 features tonight…

Nice doc comment idea

I’ve just been reading the transcript of a whiteboard session with Anders Hejlsberg and one of the questions is really, really good: Question: My problem is I’ve got these XML doc comments that are duplicated. I just strip off one. I guess it would be a neat language feature to be able to somehow indicate this is my primary- my big method, right? With all the parameters. Then the other ones are just going to borrow that XML doc comment. Hejlsberg: Yes, okay. Now that I think is- that’s not a bad idea. That yes, they should be able to … Continue reading Nice doc comment idea

Enhanced enums in C#

This will be an evolving post, hopefully. (If no-one comments on it, it probably won’t change unless I come up with better ideas myself.) Since working on a Java project last year, I’ve been increasingly fed up with C#’s enums. They’re really not very object oriented: they’re not type-safe (you can cast from one enum to another via a cast to their common underlying type), they don’t allow any behaviour to be specified, etc. They’re just named constant integral values. Until I played with Java 1.5’s enum support, that wouldn’t have struck me as being a problem, but (at least … Continue reading Enhanced enums in C#