Last week was very hectic with Devscovery Redmond in full swing. It was a great event and I really think I connected with the audience. I had a lot of conversations between sessions and at the end of each day. In particular, during the reception, I had a whiteboard session where I was with 4 attendees architecting their client/server/web service architecture – what could be more fun than this?!
 
I also had 2 questions from attendees that I thought were interesting and caused me to write some code to verify how things worked. The first question was about delegates. The attendee asked if delegates work when the callback is to a virtual method. That night, I coded up the code below to test things out. Without peeking, can you guess what the output from this code is? “B: 34“ or “D: 34“?
 
using System;
 
class B {
   delegate String Del(Int32 x);
 
   static void Main() {
      B b = new D();
      Del del = new Del(b.M);
      Console.WriteLine(del(34));
   }
   protected virtual String M(Int32 x ) {
      return “B: ” + x;
   }
}
 
class D : B {
   protected override String M(Int32 x) {
      return “D: ” + x;
   }
}
 
The answer is “D: 34” as the delegate contains a reference to a D object and calling the instance method M DOES in fact happen polymorphically.
 
The second question was about the difference between System.Exception’s Message property and its ToString method. I had forgotten what ToString on an Exception actually does so I used Lutz’s Reflector to check the code for these two members. The Message property returns just the exception’s message while ToString returns the class name, the Message text, the Stack trace, and repeats this for all nested exceptions too.
 
My next event is Devscovery Atlanta where, I’m sure I’ll have just as much fun AND, I hope I learn some more while I’m there.