Evil Code of the Day: variance and overloading

(Note that this kind of breakage was mentioned a long time ago in Eric Lippert’s blog, although not in this exact form.) Whenever a conversion becomes available where it wasn’t before, overload resolution can change its behaviour. From C# 1 to C# 2 this happened due to delegate variance with method group conversions – now the same thing is true for generic variance for interfaces. What does the following code print? using System; using System.Collections.Generic; class Base {     public void Foo(IEnumerable<string> strings)     {         Console.WriteLine(“Strings”);     } } class Derived : Base {     public void Foo(IEnumerable<object> objects)     {         Console.WriteLine(“Objects”);     } } class Test {     static void Main()     {         List<string> strings = new List<string>();         new Derived().Foo(strings);     } } The correct answer is “it … Continue reading Evil Code of the Day: variance and overloading

Non-review: The Data Access Handbook by John Goodson and Robert A. Steward

A while ago I agreed to write a review of this book (which the publisher sent me a free copy of) but I haven’t had time to read it fully yet. I’ve been skimming through the first couple of chapters though, and it’s pretty interesting. I’ll post a full review when I have more time (along with reviews of CLR via C# and a bunch of other books) but I thought it would be at least worth mentioning the book in advance. It’s really a performance book – as far as I can tell that’s its sole purpose (and I’m … Continue reading Non-review: The Data Access Handbook by John Goodson and Robert A. Steward

Faking COM to fool the C# compiler

C# 4 has some great features to make programming against COM components bearable fun and exciting. In particular: PIA linking allows you to embed just the relevant bits of the Primary Interop Assembly into your own assembly, so the PIA isn’t actually required at execution time Named arguments and optional parameters make life much simpler for APIs like Office which are full of methods with gazillions of parameters "ref" removal allows you to pass an argument by value even though the parameter is a by-reference parameter (COM only, folks – don’t worry!) Dynamic typing allows you to remove a load … Continue reading Faking COM to fool the C# compiler

Books going cheap

I’m delighted to say that Manning is having a promotional week, with a one-day discount voucher on each of the books I’ve been working on. Here’s the list of what’s going cheap when: Tuesday July 7th: C# in Depth, 1st edition – 50% off with code twtr0708 Wednesday July 8th: Groovy in Action, 2nd edition – 50% off with code twtr0709 (includes ebook of 1st edition) Thursday July 9th: Functional Programming for the Real World – 50% off with code twtr0710 Friday July 10th: C# in Depth, 2nd edition – 50% off with code twtr0711 (includes ebook of 1st edition) … Continue reading Books going cheap

Evil code of the day

At a glance, this code doesn’t look particularly evil. What does it do though? Compile it with the C# 4.0b1 compiler and run it… using System; class Base {     public virtual void Foo(int x, int y)     {         Console.WriteLine("Base: x={0}, y={1}", x, y);     } } class Derived : Base {     public override void Foo(int y, int x)     {         Console.WriteLine("Derived: x={0}, y={1}", x, y);     } } class PureEvil {     static void Main()     {         Derived d = new Derived();         Base b = d;                  b.Foo(x: 10, y: 20);         d.Foo(x: 10, y: 20);     } … Continue reading Evil code of the day