PC Review


Reply
Thread Tools Rate Thread

C# Generics bug ? : Delegate can be used as Type in Generic Dictionary

 
 
Bill Woodruff
Guest
Posts: n/a
 
      24th Jul 2006
<
note : this message was sparked in part by comments by David Browne on a
previous thread : "inserting an anonymous method as a value in a generic
dictionary ?" : David had shown the use of 'Delegate as a valid Type
declaration for the Value of a Generic Dictionary.
>


I am curious as to why I can compile and use this syntax : it seems to me to
violate the requirement that the Value of a Generic Dictionary be a Type
Name.

private delegate void theHandler();

private Dictionary<Control, theHandler> typeDict;

typeDict = new Dictionary<Control, theHandler>();

I now have a dictonary where the Key is of Type Control, and the Value of
the Key can be a named instance of theHandler :

theHandler theHandler1 = new theHandler(delegate {
MessageBox.Show("hello"); });

TreeView treeView1 = new TreeView();

typeDict.Add(treeView1, theHandler1);

typeDict[treeView1].DynamicInvoke();

Or I could can use an anonymous procedure inserted as a Value :

typeDict.Add
(
treeView1,
delegate
{
TreeNode currentNode = tv1.SelectedNode;
MessageBox.Show("treeview" + " " + currentNode.Text);
}
);





 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      24th Jul 2006
Not sure what the problem is here... what did you expect? theHandler *is* a
type name (declaration); it is a class that derives from MulticaseDelegate
which derives from Delegate which derives from Object. I happily use
delegates in typed collections - on those occasions where I want a
collection of typed delegates!

What is the issue? Sorry if I have missed the point...

Marc

"Bill Woodruff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> <
> note : this message was sparked in part by comments by David Browne on
> a previous thread : "inserting an anonymous method as a value in a generic
> dictionary ?" : David had shown the use of 'Delegate as a valid Type
> declaration for the Value of a Generic Dictionary.
>>

>
> I am curious as to why I can compile and use this syntax : it seems to me
> to violate the requirement that the Value of a Generic Dictionary be a
> Type Name.
>
> private delegate void theHandler();
>
> private Dictionary<Control, theHandler> typeDict;
>
> typeDict = new Dictionary<Control, theHandler>();
>
> I now have a dictonary where the Key is of Type Control, and the Value of
> the Key can be a named instance of theHandler :
>
> theHandler theHandler1 = new theHandler(delegate {
> MessageBox.Show("hello"); });
>
> TreeView treeView1 = new TreeView();
>
> typeDict.Add(treeView1, theHandler1);
>
> typeDict[treeView1].DynamicInvoke();
>
> Or I could can use an anonymous procedure inserted as a Value :
>
> typeDict.Add
> (
> treeView1,
> delegate
> {
> TreeNode currentNode = tv1.SelectedNode;
> MessageBox.Show("treeview" + " " + currentNode.Text);
> }
> );
>
>
>
>
>



 
Reply With Quote
 
Bill Woodruff
Guest
Posts: n/a
 
      24th Jul 2006
"Marc Gravell" wrote :

> Not sure what the problem is here... what did you expect? theHandler *is*
> a type name (declaration); it is a class that derives from
> MulticaseDelegate which derives from Delegate which derives from Object. I
> happily use delegates in typed collections - on those occasions where I
> want a collection of typed delegates!
>
> What is the issue? Sorry if I have missed the point...


.... original post by bw snipped for brevity ...

Marc, thanks for your response ! I posted this example because I was
puzzled by being able to do things this way.

I had always thought of a Delegate as a being more the declaration of a
"signature" of a future Method, rather than as a first-class Type.

While a declaration of the form :

Dictionary<Delegate, Object> theDictionary = new
Dictionary<Delegate, Object>();

Would have still raised questions in my mind, but would have been easily
acceptable, being able to do :

private delegate void theMethodBody;

Dictionary<theMethodBody, Object> theDictionary = new
Dictionary<theMethodBody, Object>();

Somehow seems intutively wrong. So I am just asking for some help in
understanding why this works.

As, so often, the problem being in my assumptions.

thanks, Bill Woodruff


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      24th Jul 2006
> I had always thought of a Delegate as a being more the declaration of a
> "signature" of a future Method, rather than as a first-class Type.


It is both.

> So I am just asking for some help in understanding why this works.


Actually, Dictionary<theMethodBody, Object> would be a poor choice, as I'm
not sure how useful a delegate would be as a key, but that's irrelvant...
OK; "theMethodBody" here is the signature, but it also defines a type, with
an .Invoke method matching the signature provided by your
delegate-declaration. The key here is getting your head around
"theMethodBody" actually being a class declaration, similar to (not
correct):

public class theMethodBody : MulticastDelegate {
public Invoke(); // matches declares signature; note also Begin/End Invoke
// ctor is a little different to normal, so not included
}

Thus theMethodBody inherits all the features of MulticastDelegate, such as
GetInvocationList(), plus all those features of Delegate such as the
non-typesafe DynamicInvoke(); however, if you know you have a theMethodBody
instance you can use the type-safe Invoke(). All this is done at compile
time.

(As it happens, "theMethodBody" could be substituted for ThreadStart,
MethodInvoker, etc - as these match on signature. )

At runtime, we can create an instance of "theMethodBody"; as with .Net
generally, this delegate-instance is an object instance:
theMethodBody runMe = new theMethodBody(SomeSuitableMethod);
As you know, I can then invoke this either as runMe(), or runMe.Invoke();

Now suppose I want to run a number of methods (with equal signature) based
on the action a user has performed; then yes, I could have some uber-switch
statement, or I could use a Dictionary of typed-delegates:
Dictionary<int, theMethodBody> actions = new Dictionary<int,
theMethodBody>();
// fill
//blah
// user has selected an option; execute the matching code:
actions[selectedActionIndex].Invoke(); // lazy; no checking for existance
etc

It all "just works"; you could use a similar strategy to craft a bespoke
back-end storage for an event mechanism instead of a delegate instance with
+= and -= (although I'm at a loss for a good reason to do so). If this isn't
helping, I suggest reading up on delegates on MSDN.

Marc

"Bill Woodruff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Marc Gravell" wrote :
>
>> Not sure what the problem is here... what did you expect? theHandler *is*
>> a type name (declaration); it is a class that derives from
>> MulticaseDelegate which derives from Delegate which derives from Object.
>> I happily use delegates in typed collections - on those occasions where I
>> want a collection of typed delegates!
>>
>> What is the issue? Sorry if I have missed the point...

>
> ... original post by bw snipped for brevity ...
>
> Marc, thanks for your response ! I posted this example because I was
> puzzled by being able to do things this way.
>
> I had always thought of a Delegate as a being more the declaration of a
> "signature" of a future Method, rather than as a first-class Type.
>
> While a declaration of the form :
>
> Dictionary<Delegate, Object> theDictionary = new
> Dictionary<Delegate, Object>();
>
> Would have still raised questions in my mind, but would have been easily
> acceptable, being able to do :
>
> private delegate void theMethodBody;
>
> Dictionary<theMethodBody, Object> theDictionary = new
> Dictionary<theMethodBody, Object>();
>
> Somehow seems intutively wrong. So I am just asking for some help in
> understanding why this works.
>
> As, so often, the problem being in my assumptions.
>
> thanks, Bill Woodruff
>



 
Reply With Quote
 
Adam Clauss
Guest
Posts: n/a
 
      24th Jul 2006
"Marc Gravell" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>> I had always thought of a Delegate as a being more the declaration of a "signature" of a future Method, rather than as a
>> first-class Type.

>
> It is both.


One way that might make it "look" better - since "theMethodBody" is acting as a type here, capitalize it:
TheMethodBody

Just a style thing, but it would help fit the concept.

--
Adam Clauss


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generic delegate type not inferred by compiler? Adam Clauss Microsoft C# .NET 6 15th Sep 2009 11:01 PM
Cannot convert lambda expression of type System.delegate becauseit is not a delegate type Satish Microsoft C# .NET 5 24th Apr 2008 07:19 PM
Generics Question - how to implement a method on a generic thatreturns the same type as the generic IUnknown Microsoft C# .NET 0 20th Mar 2008 06:34 PM
Generics & Dictionary riftimes Microsoft C# .NET 18 30th May 2006 11:37 AM
Serializing a Generics Dictionary? Benton Microsoft C# .NET 1 20th Nov 2005 04:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:38 AM.