Skip to content

Instantly share code, notes, and snippets.

@manuelleduc
Last active November 4, 2015 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuelleduc/d846a2a84aa7004c42a5 to your computer and use it in GitHub Desktop.
Save manuelleduc/d846a2a84aa7004c42a5 to your computer and use it in GitHub Desktop.
Demo delegate + lambda c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProtoLambdaCSharp
{
public delegate void KCallback(object p);
public delegate void Runner();
public class KExtCallback {
public readonly Runner _run1;
public readonly Runner _run2;
public KExtCallback(Runner run1, Runner run2)
{
this._run1 = run1;
this._run2 = run2;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProtoLambdaCSharp
{
class Program
{
static void Main(string[] args)
{
var s = new Service();
KCallback cb = (p) => Console.WriteLine(p.ToString());
s.executeCallback(cb);
int i = 0;
Runner run1 = () =>
{
i = i + 1;
Console.WriteLine("Hello " + i);
};
Runner run2 = () =>
{
i = i + 2;
Console.WriteLine("world " + i);
};
KExtCallback ecb = new KExtCallback(run1, run2);
s.executeKExtCallback(ecb);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProtoLambdaCSharp
{
class Service
{
internal void executeCallback(KCallback cb)
{
cb("Called");
}
internal void executeKExtCallback(KExtCallback ecb)
{
ecb._run1();
ecb._run2();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment