} //method that prints hello1 public DelegatesExamples() { //normal delegate, define as a member testDel del = attachMethod; del.Invoke("hello1"); //anonymous delegate testDel aDel = x => Console.WriteLine(x + " from annoy"); aDel.Invoke("hello2"); //builtin Action delegate Action <string> actDel; actDel = x => Console.WriteLine("Action " + x); actDel.Invoke("hello3"); //builtin Func delegate, x="1". Return type is int. Param is a string. Func <string, int> funcDel; funcDel = x => Int32.Parse(x + x); //auto returns var gets = funcDel.Invoke("1"); Console.WriteLine("Func " + gets); //builtin Predicate delegate, can only return bool Predicate <string> predDel; predDel = x => x == "helloworld"; var isTrue = predDel.Invoke("helloworld"); }
static void Demo1() { testDel a = Func1; a += Func2; a("test"); }
static void Demo3() { Method(Func1); testDel a = Func1; a += Func2; Method(a); }
static void Main(string[] args) { StaticDelegateDeom(); InstanceDelegateDemo(); ChainDelegateDemo1(new Program()); ChainDelegateDemo2(new Program()); testDel td = new testDel(testM); }
void run() { testDel td = new testDel(test); if (this.textBox2.InvokeRequired) { this.Invoke(td); } else { test(); } }
public IHttpActionResult Get(int id) { testDel delItems = x => x * x; return(Ok(delItems(id))); }
static void Method(testDel a) { Console.WriteLine("it is my method"); a("jdljlfj"); }
static void Demo() { testDel a = Func1; a("test"); }