Ian (Fluxtah) Warwick's blog RSS 2.0
# Thursday, February 12, 2009

Recently at a job interview during a technical test I had to answer a question:

What is the difference between a delegate and a multicast delegate?

Unfortunately I did not have the answer, I have used delegates plenty but I have never obviously used them enough to know this difference, and the dissapointing thing is that the answer was glaringly obvious.

A multicast delegate is multiple instances of the same delegate chained together using the + operator, Events in .NET make use of this to attach multiple event handlers to an event.

An example: 

public delegate void SaySomething(string something);

 class Program
 {
     static void Main(string[] args)
     {
         // At first, something is just a delegate
         SaySomething something = new SaySomething((s) => Console.WriteLine(s));

         // By adding together more instances of the SaySomething delegate.
         // we get a multicast delegate
         something += new SaySomething((s) => Console.WriteLine(s.ToUpper()));
         something += new SaySomething((s) => Console.WriteLine(s.ToLower()));

         something("Hello");
     }
 } 

The output would give:

Hello
HELLO
hello

It pains me to think I have now been at this for 6 years and there are still aspects to C# that I have not yet come across, yet I do not think of myself as a bad developer, I am sure there are plenty of things that I am aware of that the interviewer has no idea about, and it makes me wonder if the whole point is to see areas of where I could improve my knowledge, or the purpose is to scrutinise my knowledge in a way that an incorrect answer would make me out to be some kind of fraud.

I guess at least now if this question comes up again I will be able to answer it.

Thursday, February 12, 2009 6:20:00 AM (GMT Standard Time, UTC+00:00)  #    Comments [3] -
C#
Thursday, February 12, 2009 10:36:03 AM (GMT Standard Time, UTC+00:00)
All delegates can be multicast delegates. That would have been my answer.

The question seems flawed as it is trying to compare/extract a behavior of the delegate vs the definition of a delegate itself.
Thursday, February 12, 2009 4:18:44 PM (GMT Standard Time, UTC+00:00)
I like that answer :)
Friday, February 13, 2009 2:11:08 PM (GMT Standard Time, UTC+00:00)
Those kind of Gotcha! questions are stupid to ask in an interview IMHO, so don't feel so bad. I once had someone print out a list of questions off the internet and ask me to answer them. I had to ask the interviewer to clarify some of the questions, and he couldn't. These kind of questions are usually asked by people who feel insecure at their position in companies and like to take it out on possible new hires.
Comments are closed.
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Ian Warwick
Sign In
Statistics
Total Posts: 33
This Year: 0
This Month: 0
This Week: 0
Comments: 4
Themes
Pick a theme:
All Content © 2012, Ian Warwick
DasBlog theme 'Business' created by Christoph De Baene (delarou)