I read some c# codes and can not understand the “this” key word in the function parameter? Could somebody tell me what it is used for? Thanks.
public static class ControlExtensions
{
public static void InvokeIfNeeded(this Control ctl,
Action doit)
{
if (ctl.InvokeRequired)
ctl.Invoke(doit);
else
doit();
}
public static void InvokeIfNeeded<T>(this Control ctl,
Action<T> doit, T args)
{
if (ctl.InvokeRequired)
ctl.Invoke(doit, args);
else
doit(args);
}
}
It’s used to specify a type on which the extension method operates. That is,
public static void InvokeIfNeeded(this Control ctl, Action doit)“adds” anInvokeIfNeededmethod toControlclass (and all derived classes). This method, however, can only be used if you explicitly import the namespace of a class they are declared in into your scope.