473,395 Members | 1,641 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,395 software developers and data experts.

Anonymous Method / no type / no return

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; });
}
Oct 23 '07 #1
2 1430
Andrew Robinson <ne****@nospam.nospamwrote:
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 :)

--
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
Oct 23 '07 #2
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
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Andrew Robinson <ne****@nospam.nospamwrote:
>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 :)

--
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

Oct 23 '07 #3

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

Similar topics

0
by: Cordell Lawrence | last post by:
Okay guys, We are wondering if this is a bug in Framework 2.0.40607 and looking for some clarification on the issue. Take a look at the folowing code. public delegate bool BoundryTest(int...
7
by: Rene | last post by:
We all know that we can't call custom methods or properties form generic type parameters (<T>) by default because the compiler will complain about his. For example, the following won't normally...
9
by: John Smith | last post by:
I really can not appreciate why Microsoft has introduced Anonymous methods. It promotes quick and dirty style of programming and as I can see it offers no advantages over normal methods. I have...
4
by: Harold Howe | last post by:
I am running into a situation where the compiler complains that it cannot infer the type parameters of a generic method when one of the function arguments is an anonymous method. Here is a...
0
by: Coder Guy | last post by:
I am trying to subscribe to an event using reflection. The guarantee is that the the event delegate will be of type EventHandler or at least the e parameter inherits from EventArgs. Hard coding...
12
by: =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post by:
Hi, I need to initialize my class level dictionary (in .Net 2.0). I wanted to make it inline and employ anonymous methods as I do not use this code for anything else. Something similar to the...
4
by: Frankie | last post by:
I have just gotten up to speed on what anonymous methods are (syntax, capabilities, etc), and how they can be used with /called via delegates. What I am wondering is... 1. Are they only/mostly...
3
by: William Stacey [C# MVP] | last post by:
Maybe an Oxymoron, but this would be very useful. Anonomous types are great for doing projections (especially in Linq), but not being able to pass or return them is a bummer. Why couldn't they add...
8
by: Tim Chase | last post by:
I've got code similar to the following class Action: def __init__(self, ...): pass def __call__(self, ...): pass def get_help(self, ...): pass class Backend: class _Load(Action): def...
8
by: =?Utf-8?B?UGFvbG8=?= | last post by:
Is there any way I can avoid the ugly and repetitive 'if' construct shown below? I can't refactor into a separate method because qryTrans is an anonymous type. I don't think I can use a switch...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.