Skip to main content
gamedev.net
🔒 Locked

C# Delegates & Interfaces why?

Started by GDKnight Oct 29, 2005 at 4:05 AM 2 replies 450+ views
Original Post
GDKnight
GDKnight
Why would you need C# delegates? I realize that they are a simply a function pointer to be very broad. I have yet to see a real need for them though. Could someone please explain where you would use these? Only area I can think of is multi-threading and im not even competely sure on that. Another thing I don't understand why you would need it is interfaces. I realize the concept of an interface is to define a contract for some subclass. The only real area ive seen this needed frequently is perhaps the IDisposable interface. Could someone please explain why you would use an interface and not an abstract class?
GDKnight
Teknofreek
Teknofreek
Delagates are extremely useful. The event/delegate construct in C# provides a subtle addition to the callback schemes used in C/C++. In particular, each event you declare hold a list of delegates, instead of a single callback function. This makes it very easy for you to add a callback function without worrying if you'll override/replace another existing callback that needs to perform its functions.

In addition, in contrast to C++, delegates provide a clean method of allowing you to use member functions for your callbacks. This is a subtle, but huge improvement. In C++, each distinct classes member functions have a different signature. Depending on your pain threshold, some amount of template meta-programming can circumvent this limitation to some degree, but it's clumsy and awkward. Delegates make this simple.

As far as interface classes go, they're really nothing different than a class in C++ that defines all pure virtual functions. What is different is that C# doesn't allow multiple inheritance of classes, even abstract ones. They do this to protect you from the pitfalls of multiple inheritance. Really, it does lessen your flexibility in exchange for predictability. You can, however, derive a class from any number of interfaces as well as a single concrete class, if you desire. So basically, interfaces provide the only form of multiple inheritance in C#.

Hope that clears things up! =)

-John
John
GDKnight
GDKnight
So a single delegate can call multiple functions?
GDKnight
JD
JD
I think single event can dispatch to multiple objects. In C++ only functors with the help of templates can be treated as function pointers. Other methods need a command design pattern that you can then decide if multiple commands are allowed or not. Quake2 uses the command pattern for its commands albeit it does it thru normal function pointers. The culprit in C++ is ofcourse the crazyness of supplying an object that pointer to member is invoked on. That's the reason for command pattern for example. Basically a workaround for callbacks. I use callbacks to iterate over my containers as I don't bother to repeat the iteration code. I assume delegates are like command pattern.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.