Thursday, October 21, 2010

delagtes Übersicht

delegate void TestDelegate(string s);
TestDelegate testDelA = new TestDelegate(M); //C#1.0 explicite Method M must me declared
TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); }; //C#2.0 inline "anonymos method
TestDelegate testDelC = (x) => { Console.WriteLine(x); }; // C# 3.0. lambda expr.
Action TestDelegate; //C#4.0 DelegateDefinition einfacher //C# 4.0 Actions

In Windows Forms muss eine anonyme Methode noch zu einem MethodInvoker konvertiert werden:
this.Invoke(new MethodInvoker(delegate() { pgbStatus.Minimum = 0; }));

In Wpf zu einer Action:
Application.Current.Dispatcher.Invoke((Action)delegate
{
SetButtonsEnabled(true);
}, null);

No comments: