Some claim that delegates should be single-cast

B

beginwithl

hi

1)
I read somewhere that delegates should usually be single-cast.

a) Does the term single-cast mean that when we create a delegate
object, it should be used to only call on single event handler during
its whole lifetime or that it should only call one event handler at a
time?


b) Anyways, why should delegates be single-cast most of the time?





2)

a) Say some code X demands from client code Y to have a method with
certain signature in order for X to do some operations on Y --> does
the delegate object ( to which client code registers its method )
usually reside inside code X, or is it common for this delegate object
to reside inside code Y --> thus if the latter, then code X will
simply call this delegate residing in Y?

b) For example, if we were to implement IComparable via delegates,
should the delegate object be contained inside the very object that
implements comparable method, or in code ( say in array class ) that
calls this object’s comparable method?


thank you
 
K

Ken Foskey

hi

1)
I read somewhere that delegates should usually be single-cast.

a) Does the term single-cast mean that when we create a delegate object,
it should be used to only call on single event handler during its whole
lifetime or that it should only call one event handler at a time?


b) Anyways, why should delegates be single-cast most of the time?

I have implemented a series of events into my application yesterday.
Makes the whole class structure much cleaner, anyway...

I think that this might mean don't overload the event:

event status
status.type == connected
status.type == disconnected

Better to create two events:
event connect
event disconnect
 
M

miher

Hi,
1)
I read somewhere that delegates should usually be single-cast.

a) Does the term single-cast mean that when we create a delegate
object, it should be used to only call on single event handler during
its whole lifetime or that it should only call one event handler at a
time?

b) Anyways, why should delegates be single-cast most of the time?

Yes, single-cast mean that one delegate can reference only one method.
Basically when You declare a delegate it is multicast. For example a simple
delegate :
public delegate int MyDelegate(int param);
When You use it You can do things like this :
MyDelegate d = new MyDelegate(delegate(int p) {
Console.WriteLine("1"); return 2; });
d += delegate(int p) { Console.WriteLine("3"); return 4; };
Then call it:
int result = d(8);
Since this delegate references 2 methods, it will call both when You
"execute" the delegate.


2)

a) Say some code X demands from client code Y to have a method with
certain signature in order for X to do some operations on Y --> does
the delegate object ( to which client code registers its method )
usually reside inside code X, or is it common for this delegate object
to reside inside code Y --> thus if the latter, then code X will
simply call this delegate residing in Y?

b) For example, if we were to implement IComparable via delegates,
should the delegate object be contained inside the very object that
implements comparable method, or in code ( say in array class ) that
calls this object’s comparable method?

In my opinion one can think about delegates like this :
When You create a delegate You need to supply a method (and an instance
which the method should be called on) to call. Then the delegate saves the
instance reference of the object containing the method You would like to
call. A delegate is an object apart from X and Y, which holds a reference
to Y. Also X holds a reference to this object (the delegate).

Hope You find this useful.
-Zsolt
 
B

beginwithl

Hi

Sorry for the late reply


Where did you read that?  What was the context?

Some article on delegates had a link to the following interview:

http://www.artima.com/intv/simplexity2.html


In the link above they don’t explicitly say that we should only use
( for the most part ) use single-cast delegates, but the site that
provided the link, did argue that

I assume you don't mean to ask what the term "single-cast" means, but  
rather what the use of that term in the context of some advice is intended  
to mean.

That said, the question still doesn't make much sense.  A delegate is  
immutable.  When you "create a delegate object", at that point it is  
established how many methods will be invoked when the delegate itself is  
invoked.  This means that asking how the delegate should be used "during  
its whole lifetime" isn't meaningful; the delegate can be used only in one  
way ever during the entire lifetime of the delegate.

I understand that if we have delegate "D = some_method", then
"D+= some_other_method" creates a new delegate , with both some_method
and some_other_method in its invocation list.
What I meant was, should D reference only be used to point to
delegates that only ever invoke one method at the time

I see no reason to think they should be.  It is probably true that the  
invocation list for most delegate instances has only just one method; the 
multi-cast aspect of delegates is not commonly used, even for events.  But  
that's a statement of how they _are_ used, not how they _should be_ used.



What do you mean by "demand"?  How is class X going to use this method in  
class Y?  How is class X in a position to make any "demands" on class Y?

I was sort of generalizing, so I didn’t have any particular code
implementation in mind. By class ‘X demanding from class Y’ I meant
that if class Y wants certain services from class X, then it should
register a method ( with correct signature ) with a delegate D

What do you mean by "reside inside"?  Are you talking about a referenceto  
the delegate instance being stored in a member field somewhere?

I’m wondering whether delegate reference ( the one we will use to call
event handlers ) should be declared inside class X or Y?
In actuality, a very common way for one class to "demand" that some other 
class implement a method with a certain signature is to describe that  
method in an interface and then expect the other class to implement that  
interface.

Delegates can be thought of as a very simple, single-method interface.  
For one class X to "demand" some other class Y implements a method with a 
specific signature but without a specific name (as would be required for  
an interface), the _only_ possibility is for the class Y to somehow create  
a delegate referencing the implementation and passing it to class X at  
some point.

I assumed there could be some well known class X ( well known in the
programming circles ) and this class X would do “favors” to any class
that would register its methods with specific delegate object. Point
being that it is assumed that programmers of class Y know the method
signature class X demands ( if they know the method signature, then
delegate object could also be implemented inside X class )

This could be via a named property, but then what would the point of that 
be?  If class Y has to have some specific named member to support your  
"demand", it might as well be the method itself.  More generally, this is  
the problem with storing the delegate reference in class Y.  If the  
reference is in class Y, then class X needs a way to access it, and if it 
has a way to access it, then it could just as easily have had a way to  
access the method itself.

* But by having Y to implement delegate object instead of allowing X
to access the method directly, X class doesn’t have to know the name
of a method in class Y


* Anyways, I thought there was some general agreement in which of the
two classes should the “actual ”delegate reference be declared ( by
“actual” I mean one that will be used to fire up a delegate – while
both X and Y may have numerous references pointing to the same
delegate object as this “actual” delegate reference is pointing at,
but it will be the “actual” reference that will ultimately be used to
call the event handlers).


thank you all
 
B

beginwithl

hi

[...]
1)
I read somewhere that delegates should usually be single-cast.
Where did you read that?  What was the context?
Some article on delegates had a link to the following interview:

In the link above they don’t explicitly say that we should only use
( for the most part ) use single-cast delegates, but the site that
provided the link, did argue that

I'm not sure what relevance the link you offered has, if it's not actually  
where you saw the advice you're asking about.

Sorry about that. I can’t find the link to that site. I’m only
speculating, but knowing me, chances are I misread the article and
thus there never were any such claims made


thank you for helping me out

cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top