Skip to content

Instantly share code, notes, and snippets.

@pavoldecky
Created May 4, 2016 07:35
Show Gist options
  • Save pavoldecky/f925e20d67ad5785df4c6d9f713d6cfb to your computer and use it in GitHub Desktop.
Save pavoldecky/f925e20d67ad5785df4c6d9f713d6cfb to your computer and use it in GitHub Desktop.
Fun with delegates
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FunWithDelegates
{
class Program
{
delegate int SumDelegate(int a, int b);
static void Main(string[] args)
{
SumDelegate t1 = new SumDelegate(Sum);
SumDelegate t2 = delegate (int x, int y) { return x + y; };
Func<int, int, int> t3 = delegate (int x, int y) { return x + y; };
Func<int, int, int> t4 = (int x, int y) => x + y;
var result = SumFuncDelegate(t1, 1, 2);
var result2 = SumFunc(t4, 1, 2);
var result3 = SumFunc((x, y) => x + y, 1, 2);
}
static int Sum(int a, int b)
{
return a + b;
}
static int SumFuncDelegate(SumDelegate func, int a, int b)
{
return func(a, b);
}
static int SumFunc(Func<int,int,int> func, int a, int b)
{
return func(a, b);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment