Anonymous Method / no type / no return

A

Andrew Robinson

Is there any general purpose delegate that will allow me to call a method
anonymously. The delegate does not pass any data as part of the method
signature and has a void return. Data is passed as part of the method body.
I am thinking something along the line of Action<null>. Just wondering if
there is a simpler method other than declaring the delegate FredDelegate()?

example:

private delegate void FredDelegate();
private void Fred(FredDelegate action)
{
// code here

action();

// more clode here
}

public void Fred()
{
Fred(delegate() { });
}

public void Fred(FredKey key)
{
Fred(delegate() { this.status = key.status; });
}
 
J

Jon Skeet [C# MVP]

Andrew Robinson said:
Is there any general purpose delegate that will allow me to call a method
anonymously. The delegate does not pass any data as part of the method
signature and has a void return. Data is passed as part of the method body.
I am thinking something along the line of Action<null>. Just wondering if
there is a simpler method other than declaring the delegate FredDelegate()?

ThreadStart or MethodInvoker are the ones I know off the top of my
head. In .NET 3.5 there's plain Action, which is a nicer alternative :)
 
A

Andrew Robinson

Jon,

Exactly what I was after. Coded my own Action() in a base class with a note
to remove once upgraded to the 3.5 framework.

Thanks
 

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