Anonymous Method Sample

  • Thread starter Thread starter Nick
  • Start date Start date
N

Nick

Hi,
I was just playing around and found that the following two ways of
calling the same method using the WaitCallback method that takes an
object parameter. Can any one explain why there is no problem with the
first call even though the delegate doesn't take in the Object
Parameter??? What is the complier doing "under the covers".

Thanks for any help
Nick

Object state = new Object();
// Using WaitCallback
// Note no parameter passed in
WaitCallback oInvokeNoParameter = delegate {
MyMethodCall(); };
oInvokeNoParameter.Invoke(state);


WaitCallback oInvokeAParameter = delegate(Object oObject) {
MyMethodCall(); };
oInvokeAParameter.Invoke(state);

.......
static void MyMethodCall()
{
Console.WriteLine("MyMethodCall has been invoked");
}
 
Hi,
I was just playing around and found that the following two ways of
calling the same method using the WaitCallback method that takes an
object parameter. Can any one explain why there is no problem with
the
first call even though the delegate doesn't take in the Object
Parameter??? What is the complier doing "under the covers".
Thanks for any help
Nick
Object state = new Object();
// Using WaitCallback
// Note no parameter passed in
WaitCallback oInvokeNoParameter = delegate {
MyMethodCall(); };
oInvokeNoParameter.Invoke(state);
WaitCallback oInvokeAParameter = delegate(Object oObject)
{
MyMethodCall(); };
oInvokeAParameter.Invoke(state);
......
static void MyMethodCall()
{
Console.WriteLine("MyMethodCall has been invoked");
}

The compiler is still generating the same WaitCallback delegate with a state
parameter under the covers. However, with an anonymous method, you aren't
required to actually declare the parameters unless you use them in the anonymous
method's body.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
Also, if method was "static void MyMethodCall(object value){}",
you could also do:

WaitCallback cb = MyMethodCall;
cb(null);

--
William Stacey [C# MVP]

| Hi,
| I was just playing around and found that the following two ways of
| calling the same method using the WaitCallback method that takes an
| object parameter. Can any one explain why there is no problem with the
| first call even though the delegate doesn't take in the Object
| Parameter??? What is the complier doing "under the covers".
|
| Thanks for any help
| Nick
|
| Object state = new Object();
| // Using WaitCallback
| // Note no parameter passed in
| WaitCallback oInvokeNoParameter = delegate {
| MyMethodCall(); };
| oInvokeNoParameter.Invoke(state);
|
|
| WaitCallback oInvokeAParameter = delegate(Object oObject) {
| MyMethodCall(); };
| oInvokeAParameter.Invoke(state);
|
| ......
| static void MyMethodCall()
| {
| Console.WriteLine("MyMethodCall has been invoked");
| }
|
 

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

Back
Top