delegate parameters

In this chapter you will learn:

  1. Polymorphic parameters for delegate
  2. Delegate with reference paramemters

Polymorphic parameters for delegate

delegate can accept more specific parameters.

using System;// j  a  va 2 s  .c  om
delegate void Printer(object t);

class Test
{
    static void consolePrinter(object str)
    {
        Console.WriteLine(str);
    }
    static void Main()
    {
        Printer p = consolePrinter;

        object obj = "java2s.com";
        p(obj);
    }
}

The output:

Delegate with reference paramemters

using System;// ja v  a  2  s  .  c  o  m

delegate void FunctionToCall(ref int X);

class MainClass
{
   public static void Add2(ref int x) { 
       x += 2; 
   }
   
   public static void Add3(ref int x) { 
       x += 3; 
   }

   static void Main(string[] args)
   {
      FunctionToCall functionDelegate = Add2;
      functionDelegate += Add3;
      functionDelegate += Add2;

      int x = 5;
      functionDelegate(ref x);

      Console.WriteLine("Value: {0}", x);
   }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. delegate with generic type
  2. How to add constraint to delegate generic parameters
  3. Generic Delegate list
Home » C# Tutorial » delegate, lambda, event
delegate
Multicast delegate
delegate variables
delegate parameters
Generic delegate
delegate return
Func and Action
delegate new
chained delegate
Anonymous delegate
delegate array
Return a delegate
delegate as parameter
Event
event multicast
static event
EventHandler
Event pattern
lambda
lambda syntax
Func, Action and lambda
lambda with outer function
lambda iteration variables