delegate parameters
In this chapter you will learn:
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: