public static int Insert(MyDelegateType callback) // or // public static int Insert(Func<IfxConnection,IfxTransaction,object> callback) { // ... callback(conn, tran); // ... }
public static int Main(string[] args) { DateTime dt = DateTime.Now; MyDelegateType del = new MyDelegateType(MyMath.AttachToDelegate); //multi-cast delegate, attaching multiple methods del += new MyDelegateType(MyMath.GetMonth); //delegates and anonymous methods, use the delegate keyword del += delegate { Console.WriteLine("anonymous method called"); }; del.Invoke(dt); Console.WriteLine("hello world!"); var result = from i in args select i; foreach (var item in result) Console.WriteLine(item); double x = 10, y = 20, temp; Console.WriteLine("x: {0}, y:{1}", x, y); temp = y; y = x; x = temp; Console.WriteLine("x: {0}, y:{1}", x, y); Display<double>.DisplayStrings(x, y); Console.WriteLine("Sum: {0:C}", MyMath.Add(10.345, 344.67, 89, 234.900, -20)); try { throw new MyCustomExceptionHandlingMechanism(); } catch (MyCustomExceptionHandlingMechanism e) { e.ShowExceptionDetails(); } DevelopmentEnvironment devEnv = new DevelopmentEnvironment(); devEnv.ShowMyEnvironment(); TestClass crossLanguage = new TestClass(); crossLanguage.MyFunc(); MyMath myObj = new MyMath(); myObj.InternalInstanceMethod(); Console.WriteLine("name {0}, value {1}", ColorCoding.blue.GetType().Name, Convert.ToInt32(System.Enum.Parse(ColorCoding.blue.GetType(),"red"))); Console.ReadLine(); return System.Environment.ExitCode = 0; }
static void Main() { //create object of Sample class Sample s = new Sample(); //create delegate object //MyDelegateType myDelegate = new MyDelegateType(s.Add); MyDelegateType myDelegate = s.Add; myDelegate += s.Subtract; //call the method indirectly using delegate object myDelegate.Invoke(10, 7); Console.ReadKey(); }
static void DelegateIntro() { // here we create an instance of our delegate // since HelloWorld() has the same signature (returns void, no params), we can assign it to our delegate MyDelegateType delegateInstance = HelloWorld; // calling delegateInstance() here will simply call HelloWorld() delegateInstance(); ComputeDelegate computeInstance = Compute; int result = computeInstance(5); Console.WriteLine("The result of computeInstance is: " + result); Console.ReadKey(); }
delegate int MyDelegateType(int a, int b); // delegate는 Type이다. (int, float 처럼) static void Main(string[] args) { int Plus(int a, int b) { return(a + b); } int Minus(int a, int b) { return(a - b); } // 2. 델리게이트 참조변수 생성 MyDelegateType Callback; // Callback : MyDelegate의 델리게이트 인스턴스 // 3. 델리게이트의 인스턴스를 생성, 이때 델리게이트가 참조할 메소드를 매개변수로 넘긴다. Callback = new MyDelegateType(Plus); // 메소드를 인자로 갖는 타입의 인스턴스 생성 // 4. 델리게이트를 호출한다. Console.WriteLine(Callback(3, 5)); // Plus(3, 5) Callback = new MyDelegateType(Minus); Console.WriteLine(Callback(7, 5)); // Minus(7, 5) Calcultor calc = new Calcultor(); MyDelegateType calcDelegate; // 1. 델리게이트 참조변수 생성 calcDelegate = new MyDelegateType(calc.Plus); // 2. 델리게이트 인스턴스 생성(메소드 참조) Console.WriteLine(calcDelegate(3, 5)); // 3. 델리게이트 사용 calcDelegate = new MyDeleMyDelegateTypegate(Calcultor.Minus); Console.WriteLine(calcDelegate(7, 5)); // 델리게이트 인스턴스를 생성할 때 생성자를 사용하지 않고, 참조할 메소드명을 이용할 수 있다. calcDelegate = calc.Plus; calcDelegate += Calcultor.Minus; // + 연산자로 델리게이트를 연결할 수 있다. calcDelegate(1, 2); }
static void Main(string[] args) { int Plus(int a, int b) { return(a + b); } int Subtract(int a, int b) { return(a - b); } // 2. 델리게이트 참조변수 생성 MyDelegateType myDelegate; // 3. 델리게이트의 인스턴스 생성, 이때 참조할 메소드를 설정한다. myDelegate = new MyDelegateType(Plus); // 4. 델리게이트 실행 Console.WriteLine(myDelegate(3, 4)); // 7 myDelegate = Subtract; Console.WriteLine(myDelegate(3, 4)); Calcultor calc = new Calcultor(); MyDelegateType calcDelegate; calcDelegate = new MyDelegateType(calc.Plus); Console.WriteLine(calcDelegate(5, 10)); calcDelegate = new MyDelegateType(Calcultor.Subtract); Console.WriteLine(calcDelegate(5, 10)); // 델리게이트는 인스턴스를 생성할 때 생성자 대신 메소드명을 이용할 수 있다. calcDelegate = calc.Plus; calcDelegate += Calcultor.Subtract; // 메소드를 2개 이상 연결할 수 있다. calcDelegate(1, 2); }
delegate int MyDelegateType(int a, int b); // delegate는 Type이다. static void Main(string[] args) { int Plus(int a, int b) { return(a + b); } int Subtract(int a, int b) { return(a - b); } // 2. 델리게이트 참조 변수 생성 MyDelegateType myDelegate; // 3. 델리게이트 인스턴스 생성 myDelegate = new MyDelegateType(Plus); // 4. 델리게이트 호출 Console.WriteLine(myDelegate(3, 4)); myDelegate = Subtract; Console.WriteLine(myDelegate(3, 4)); Calcuator calc = new Calcuator(); MyDelegateType calcDelegate; // 델리게이트 참조 변수 calcDelegate = new MyDelegateType(calc.Plus); Console.WriteLine(calcDelegate(5, 6)); calcDelegate = Calcuator.Subtract; Console.WriteLine(calcDelegate(10, 5)); calcDelegate = calc.Plus; calcDelegate += Calcuator.Subtract; Console.WriteLine(calcDelegate(7, 8)); }
void ShowProgress(Object sender, EventArgs e) { PiUpdateEventArgs pe = e as PiUpdateEventArgs; // To be run on UI thread MyDelegateType methodForUiThread = delegate { // Display progress in UI tblkResults.Text += pe.Pi; progressBar.Value = pe.DigitsSoFar; progressBar.Visibility = Visibility.Visible; if (pe.DigitsSoFar == pe.TotalDigits) { // Reset UI sbiStatus.Content = "Ready"; progressBar.Visibility = Visibility.Hidden; btnCalculate.IsEnabled = true; } }; this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, methodForUiThread); }
public void SetDelegate(MyDelegateType delegateMeth) { _delegateMethod = delegateMeth; }