12

What is the name of the default delegate in C# which takes no parameters and returns void? I remember there existed such a delegate but I don't remember its name.

Tuxedo
  • 121
  • 1
  • 3
  • 7
    What is the name of that seach engine that you can use to find stuff with? ;) http://www.google.se/search?q=c%23+delegate+no+parameters+returns+void – Guffa Nov 01 '10 at 06:52
  • 1
    @Guffa: Which now happens to point back to this question... – davidg Apr 07 '13 at 10:10
  • @davidg: That usually happens after only a few minutes, regardless of whether there are any useful answers or not. Just because something appears in a google result isn't any guarantee that there is any useful information there... – Guffa Apr 07 '13 at 10:20

5 Answers5

13

Prior to .NET 3.5, it was fairly common to declare your own. Now, Action is a good candidate, but ThreadStart was commonly used (fairly confusingly), or MethodInvoker if you were already referencing winforms.

A quick test (note, running in .NET 4.0, using just some libraries - so not exhaustive):

var qry = from asm in AppDomain.CurrentDomain.GetAssemblies()
          from type in asm.GetTypes()
          where type.IsSubclassOf(typeof(Delegate))
          let method = type.GetMethod("Invoke")
          where method != null && method.ReturnType == typeof(void)
          && method.GetParameters().Length == 0
          orderby type.AssemblyQualifiedName
          select type.AssemblyQualifiedName;
foreach (var name in qry) Console.WriteLine(name);

shows some more candidates:

System.Action, mscorlib...
System.CrossAppDomainDelegate, mscorlib...
System.IO.Pipes.PipeStreamImpersonationWorker, System.Core...
System.Linq.Expressions.Compiler.LambdaCompiler+WriteBack, System.Core...
System.Net.UnlockConnectionDelegate, System...
System.Runtime.Remoting.Contexts.CrossContextDelegate, mscorlib...
System.Threading.ThreadStart, mscorlib...
System.Windows.Forms.AxHost+AboutBoxDelegate, System.Windows.Forms...
System.Windows.Forms.MethodInvoker, System.Windows.Forms...
Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
  • 5
    I vote for alternating between AxHost.AboutBoxDelegate and LambdaCompiler.WriteBack. I shall start ripped uses of "Action" out of my code forthwith. – Jon Skeet Nov 01 '10 at 07:17
8

There are several such delgates, but I think you are looking for Action. One other option is MethodInvoker (in System.Windows.Forms).

Fredrik Mörk
  • 147,210
  • 26
  • 277
  • 333
  • Its only for version 3.5 and above? Are there any for 2.0? – Tuxedo Nov 01 '10 at 06:46
  • @Tuxedo: `MethodInvoker` has been around since 1.1. If you want something in the `System` namespace there is the `CrossAppDomainDelegate`, but its name is so specific so it will probably seem a bit weird for more general purpose use. – Fredrik Mörk Nov 01 '10 at 06:50
5

You are probably looking for "Action".

Some related reading:

Community
  • 1
  • 1
Nippysaurus
  • 19,402
  • 18
  • 71
  • 124
3

I may be interpreting your question differently to everyone else, but:

If you are thinking of the delegate to use for event handling, convention is to use EventHandler. This delegate doesn't take 'no' parameters, but it is essentially information-less.

Benjol
  • 57,639
  • 51
  • 180
  • 252
2

In .net 2.0 the MethodInvoker delegate is the recomended way. It is the most generic. As it's name suggest it invokes a method. Others might have the same properties you described but they have different name that suggest other usages or very specific usages in specific areas.

Liviu Mandras
  • 6,314
  • 1
  • 38
  • 61