Skip to content

Instantly share code, notes, and snippets.

@musoftware
Last active February 28, 2019 16:30
Show Gist options
  • Save musoftware/9d2cd841df024b743c3b31b20263ef55 to your computer and use it in GitHub Desktop.
Save musoftware/9d2cd841df024b743c3b31b20263ef55 to your computer and use it in GitHub Desktop.
DelegateSample
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateSample
{
class Program
{
static int TotalSum = 0;
// pointer to contains many function of same pointer
public delegate void MultiFunction(int i);
// apply delegate that to same input
public static void ApplyMultiFunction(MultiFunction function)
{
// Here I don't know the name of function I liked with delegate MultiFunction
function.Invoke(3);
}
// function 1 associated with delegate
public static void DelegateFunctionWithSameParameters(int i)
{
TotalSum += i;
}
// function 2 associated with delegate
public static void DelegateFunctionWithSameParameters2(int i)
{
TotalSum *= i;
}
static void Main(string[] args)
{
MultiFunction func; // delegate
func = DelegateFunctionWithSameParameters; // func1
func += DelegateFunctionWithSameParameters2; // func2
ApplyMultiFunction(func); // apply func1 and 2
Console.WriteLine(TotalSum); // print answer
// must print 9 in my mind
Console.ReadKey(); // wait for exit
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment