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;//ww w . j av  a2  s  . c o m
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

Delegate with reference paramemters


using System;//  w  ww. j  av a2s.co  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 »
    C# Types »
      C# Delegate
C# Delegate
C# Multicast Delegates
delegate parameters
C# Generic delegate
C# Func and Action
C# Anonymous delegate