Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default delegate in C#

Tags:

c#

delegates

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.

like image 959
Tuxedo Avatar asked Nov 01 '10 06:11

Tuxedo


2 Answers

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...
like image 138
Marc Gravell Avatar answered Oct 02 '22 21:10

Marc Gravell


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

like image 40
Fredrik Mörk Avatar answered Oct 02 '22 21:10

Fredrik Mörk