473,549 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How often do you use "delegates" (Events) over "interfaces " anyway?

I understand delegates (static and non-static) and I agree they are
very useful, and that the "Forms" used in the Windows .NET API could
not work without them. That said, I'm curious as to how many heavy
duty pro programmers in this newsgroup have actually used a delegate
in their code, say in the last year of coding. By "use" I mean
constructed a delegate and using it in an Event Source class and an
Event Receiver class.

Interfaces seem like a more practical but less elegant way of doing
something akin to what a delegate does [a function pointer in C++
terminology] does, for most cases.

Or, simply calling an event receiver class directly from the "source"
class seems logical--if you wrote both classes you should now how they
work.

That said, if you are working with 100s of programmers on a team
project perhaps writing code using a delegate makes sense, since it's
a black box that other people can refer to for their code.

RL

Aug 13 '07 #1
5 2386
raylopez99 wrote:
I understand delegates (static and non-static) and I agree they are
very useful, and that the "Forms" used in the Windows .NET API could
not work without them. That said, I'm curious as to how many heavy
duty pro programmers in this newsgroup have actually used a delegate
in their code, say in the last year of coding. By "use" I mean
constructed a delegate and using it in an Event Source class and an
Event Receiver class.
I use delegates all the time. I also disagree that they are equivalent
to interfaces. The two serve very different purposes, even if in some
ways they behave similarly.

A delegate is a way to encapsulate a specific method (instance or
otherwise) in a way that allows it to be called. It may or may not be
used with an event.

An interface defines a contract to which an object must adhere. You
could use an interface in similar ways to delegates (especially as
delegates are used with interfaces), but they aren't the same.

Specifically: you could define an interface that has just one method and
then require a class to implement that interface rather than subscribe
to an event. However, a) you'd then have to maintain a list of
subscribing interfaces (assuming you want the multiple-receiver behavior
of events), and b) it seems sort of silly to declare a whole interface
when you just want one method.

Conversely, you could define a single interface that includes all of the
events an object might publish. But then any class that wants to
support just one of those events is required to implement _all_ of the
methods defined in the interface. This is wasteful. The existing event
model allows for an object to pick and choose which events it wants to
subscribe to.
Interfaces seem like a more practical but less elegant way of doing
something akin to what a delegate does [a function pointer in C++
terminology] does, for most cases.
I feel that interfaces are quite elegant for doing what they were
designed for: declaring a contract that objects can implement, so that
they can comply with that contract in an abstract way (that is, in a way
that doesn't require the user of the object to know anything else about
the object other than its compliance with the contract).
Or, simply calling an event receiver class directly from the "source"
class seems logical--if you wrote both classes you should now how they
work.
But you don't always write both classes. Not that you actually need to;
you could easily just declare some other contract, such as an interface
or a simply delegate type, for example. But events serve a particular
purpose, that is different from the purpose an interface serves, and is
also above and beyond what a delegate does.
That said, if you are working with 100s of programmers on a team
project perhaps writing code using a delegate makes sense, since it's
a black box that other people can refer to for their code.
Delegates serve their own purpose, and that purpose has very little to
do with how many coders are working on the project. I use them with
events, I use them for asynchronous calls (those that start with
Begin... and End...), I use them when using Control.Invoke( ) or
Control.BeginIn voke(), I use them for interacting with the threading
classes like BackgroundWorke r and Thread, and that's not even a complete
list.

Delegates are very useful, and are not at all some kind of "equivalent
alternative" to interfaces.

Pete
Aug 13 '07 #2
Peter Duniho wrote:
[...]
An interface defines a contract to which an object must adhere. You
could use an interface in similar ways to delegates (especially as
delegates are used with interfaces), but they aren't the same.
Sorry. Change "...are used with interfaces" to "...are used with
events". My typo.
Aug 13 '07 #3
Ray,
I'm not getting what you are after here. I, like Peter D and legions of
other programmers, use delegates all the time. To simplify, a delegate is a
special type of method that points to another method and has features that
allow asynchronous marshaling of results across threads, among other
features. A delegate encapsulates methods as objects.

With .NET 2.0, it is now possible to declare delegates inline as anonymous
methods, thereby making them even more useful.

If you are looking for some good resources on why delegates are useful and
some typical usage scenarios to make it all "come together", there are plenty
of good tutorials on the web.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"raylopez99 " wrote:
I understand delegates (static and non-static) and I agree they are
very useful, and that the "Forms" used in the Windows .NET API could
not work without them. That said, I'm curious as to how many heavy
duty pro programmers in this newsgroup have actually used a delegate
in their code, say in the last year of coding. By "use" I mean
constructed a delegate and using it in an Event Source class and an
Event Receiver class.

Interfaces seem like a more practical but less elegant way of doing
something akin to what a delegate does [a function pointer in C++
terminology] does, for most cases.

Or, simply calling an event receiver class directly from the "source"
class seems logical--if you wrote both classes you should now how they
work.

That said, if you are working with 100s of programmers on a team
project perhaps writing code using a delegate makes sense, since it's
a black box that other people can refer to for their code.

RL

Aug 13 '07 #4
"raylopez99 " <ra********@yah oo.comwrote in message
news:11******** *************@q 75g2000hsh.goog legroups.com...
>I understand delegates (static and non-static) and I agree they are
very useful, and that the "Forms" used in the Windows .NET API could
not work without them. That said, I'm curious as to how many heavy
duty pro programmers in this newsgroup have actually used a delegate
in their code, say in the last year of coding. By "use" I mean
constructed a delegate and using it in an Event Source class and an
Event Receiver class.
Quite often. I think I use an interface for this less often.

Michael
Aug 14 '07 #5
raylopez99 <ra********@yah oo.comwrote:
I understand delegates (static and non-static) and I agree they are
very useful, and that the "Forms" used in the Windows .NET API could
not work without them. That said, I'm curious as to how many heavy
duty pro programmers in this newsgroup have actually used a delegate
in their code, say in the last year of coding. By "use" I mean
constructed a delegate and using it in an Event Source class and an
Event Receiver class.
With C# 1, delegates were typically only used for events. In C# 2 and
particularly C# 3, they're much more important. They make it easy to
specify things like filters, comparisons, projections etc.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 14 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
5219
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go...
3
8871
by: Phill | last post by:
When should you use an interface over a delegate? Like if you have a class that needs to sort various types of objects. Should it use a delegate to do the sorting or require that the objects implement a sorting interface? I read that delegates are smaller and of finer granularity than interfaces. What does that mean exactly? Does it mean...
47
1941
by: Dickson Woo | last post by:
Just that.
4
2621
by: Ben R. | last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing with value versus object types. If I'm correct, when I do: dim x as integer There's no need for "new" to be called since this "value" type is all set to go. Is this because value types are stored on the stack so the memory initialization is already taken care of? ...
6
4794
by: Smithers | last post by:
Just looking to compile a list of "all the ways to implement events". I'm NOT looking to get into the merits or mechanics of each in this thread... just want to identify them all - good, bad, and ugly. Here's what I have so far 1. Implement via the 'event' keyword (with associated delegate, etc) 2. Expose a private delegate via a public...
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7446
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7715
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.