Instance Versus Static Method Targets - CSharp Custom Type

CSharp examples for Custom Type:delegate

Introduction

The System.Delegate class's Target property represents this instance and will be null for a delegate referencing a static method.

For example:

Demo Code

using System;//from  w  w w .  j  a  v  a 2s. c o m
public delegate void ProgressReporter (int percentComplete);
class Test
{
   static void Main()
   {
      X x = new X();
      ProgressReporter p = x.InstanceProgress;
      p(99);                                 // 99
      Console.WriteLine (p.Target == x);     // True
      Console.WriteLine (p.Method);          // Void InstanceProgress(Int32)
   }
}
class X
{
   public void InstanceProgress (int percentComplete)
   => Console.WriteLine (percentComplete);
}

Result


Related Tutorials