473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Delegate Question: How does it know?

Learning about delegates (again, I admit), I think I finally get it,
maybe.

I can reference any method in any class in the same namespace as long
as it has the same signature. Right?

But how does it know? Does the .NET runtime keep a list of all the
possible methods that could be called and then when the delegate is
invoked, just look for a match? For example:

A very basic example (SimpleDelegate1.cs):

using System;

namespace Akadia.BasicDelegate
{
// Declaration
public delegate void SimpleDelegate();

class TestDelegate
{
public static void MyFunc()
{
Console.WriteLine("I was called by delegate ...");
}

public static void Main()
{
// Instantiation
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

// Invocation
simpleDelegate();
}
}
}

Compile an test:

# csc SimpleDelegate1.cs
# SimpleDelegate1.exe
I was called by delegate ...

(taken from:
http://www.akadia.com/services/dotne...nd_events.html)

The system just "knows" to call MyFunc because it "knows" that delegate
can work with that type of method as defined by its signature?

Sep 7 '06 #1
5 1499

Maybe this can help out.

This is the actual "register for an event" code.

The keyword being GetInvocationList

protected static MessageArrivalHandler m_handler;

public event MessageArrivalHandler MessageArrival
{
//when new handler is register for the event, start the listener
//if it is not yet started
add
{
m_handler += value;
}
remove
{
m_handler -= value;
//stop the listener if no handler is listed
if (m_handler == null || m_handler.GetInvocationList().Length <= 0)
{
//nobody is registered
}
}
}


<ne***********@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
Learning about delegates (again, I admit), I think I finally get it,
maybe.

I can reference any method in any class in the same namespace as long
as it has the same signature. Right?

But how does it know? Does the .NET runtime keep a list of all the
possible methods that could be called and then when the delegate is
invoked, just look for a match? For example:

A very basic example (SimpleDelegate1.cs):

using System;

namespace Akadia.BasicDelegate
{
// Declaration
public delegate void SimpleDelegate();

class TestDelegate
{
public static void MyFunc()
{
Console.WriteLine("I was called by delegate ...");
}

public static void Main()
{
// Instantiation
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

// Invocation
simpleDelegate();
}
}
}

Compile an test:

# csc SimpleDelegate1.cs
# SimpleDelegate1.exe
I was called by delegate ...

(taken from:
http://www.akadia.com/services/dotne...nd_events.html)

The system just "knows" to call MyFunc because it "knows" that delegate
can work with that type of method as defined by its signature?

Sep 7 '06 #2
The delegate knows to call MyFunc because you told it to with the following
line of code.
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
This code creates an instance of the SimpleDelegate class and that instance
stores the reference to the static method MyFunc.

What may be confusing is that this line of code
public delegate void SimpleDelegate();
is doing two things: declaring the method signature of the delegate void
(void) and declaring a class called SimpleDelegate which the compiler
derives from MultiCastDelegate. Check out
'http://msdn2.microsoft.com/en-us/library/system.delegate.aspx for a
detailed explanation.
<ne***********@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
Learning about delegates (again, I admit), I think I finally get it,
maybe.

I can reference any method in any class in the same namespace as long
as it has the same signature. Right?

But how does it know? Does the .NET runtime keep a list of all the
possible methods that could be called and then when the delegate is
invoked, just look for a match? For example:

A very basic example (SimpleDelegate1.cs):

using System;

namespace Akadia.BasicDelegate
{
// Declaration
public delegate void SimpleDelegate();

class TestDelegate
{
public static void MyFunc()
{
Console.WriteLine("I was called by delegate ...");
}

public static void Main()
{
// Instantiation
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

// Invocation
simpleDelegate();
}
}
}

Compile an test:

# csc SimpleDelegate1.cs
# SimpleDelegate1.exe
I was called by delegate ...

(taken from:
http://www.akadia.com/services/dotne...nd_events.html)

The system just "knows" to call MyFunc because it "knows" that delegate
can work with that type of method as defined by its signature?

Sep 7 '06 #3
If you look at a delegate as a pointer to a function (or in the case of
C#, a reference to a method) everything else should make sense.

Hope that helps,

--
Sean
Sep 7 '06 #4
<ne***********@gmail.comwrote:
Learning about delegates (again, I admit), I think I finally get it,
maybe.

I can reference any method in any class in the same namespace as long
as it has the same signature. Right?
Namespaces are irrelevant. As of .NET 2.0, the signature doesn't have
to be exactly the same, just compatible.
But how does it know? Does the .NET runtime keep a list of all the
possible methods that could be called and then when the delegate is
invoked, just look for a match? For example:
No - when you create a delegate instance, you tell it which method you
want to call. It remembers.

See http://www.pobox.com/~skeet/csharp/events.html for a closer look at
events and delegates.
The system just "knows" to call MyFunc because it "knows" that delegate
can work with that type of method as defined by its signature?
No, it knows to call MyFunc because you've created a delegate instance
passing MyFunc as the parameter:

SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 7 '06 #5
"Fao, Sean" <en**********@yahoo.comI-WANT-NO-SPAMwrote in message
news:Or**************@TK2MSFTNGP03.phx.gbl...
If you look at a delegate as a pointer to a function (or in the case of
C#, a reference to a method) everything else should make sense.
Actually, although P/Invoke automatically compiles delegates into function
pointers, that may not be the internal representation of a delegate. By
reading the discussion of delegates and constrained execution regions
(http://msdn.microsoft.com/msdnmag/is.../Reliability/), it seems that
delegates are not pre-jitted.

So the original poster was very nearly correct. The runtime maintains a
list of all loaded methods (irregardless of namespace, and including all
assemblies). The delegate constructor checks the compatibility of the given
method (the C# compiler will do this in advance where possible), and stores
the metadata handle. Invoking the delegate causes the runtime to lookup the
method by its handle, JIT the MSIL if that hasn't been done yet, and then
run the native code generated by the JIT.

But nowhere does the runtime maintain a list of "methods compatible with
delegate type XYZ". It has a list of all methods, and enforces a subtype
constraint during compile or delegate construction (for construction by
reflection, see Delegate.CreateDelegate).
>
Hope that helps,

--
Sean

Sep 8 '06 #6

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

Similar topics

2
by: Nima | last post by:
The documentation for the Delegate and the MultiCastDelegate classes tell me that when we have a line like: public delegate void CheckAndPrintDelegate(string str); it causes the compiler to...
3
by: BoloBaby | last post by:
OK, I've managed to clarify my question (whew). I'll show two blocks of code - one in VB6.0 and one in VB.NET. The VB6.0 code manages to execute the callback function, the VB.NET does not. The...
5
by: Doug Handler | last post by:
Hi, I have a form (Form1) that contains a tab control which one tab has a customer user control (UserControl1). When the user double-clicks on the grid hosted there a new user control is...
3
by: lothar.behrens | last post by:
Hi, I am thinking about the delegate mechanism and try to understand it. I am coming from C++ and know about callbacks or member callbacks. In C++ I have this typedef for every class that...
4
by: Bob Cramer | last post by:
I don't have a copy of Reflector handy :-( so I'd appreciate if someone could confirm (or clarify) the following. In consideration of the following delegate declaration... delegate string...
2
by: Tony Johansson | last post by:
Hello!! Below is a program copied from a book. The program is easy but there is one thing about the program that I would like to have some more info about. Assume that you have for example 10...
6
by: damiensawyer | last post by:
Hi, Can someone please explain to me something about delegates? My understanding is as follows. A delegate is basically an object that can hold a reference to a "method" somewhere. That is,...
5
by: AliRezaGoogle | last post by:
Hi, I have a conceptual question on Events and Multicast Delegates. Let me explain: As we know an event is a multicast delegate. What we declare as an event is inherently a multicast delegate....
3
by: puzzlecracker | last post by:
Sort of new to internal of delegates. My question is when you create a delegate instance, does the surrounding block must be able to see/access method you want delegate to take on. Here is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.