static void Main(string[] args) { Console.WriteLine("***** Simple delegate example *****\n"); // Create a BinaryOp delegate object that "points to" SimpleMath(). SimpleMath m = new SimpleMath(); SimpleDelegate.BinaryOp b = new SimpleDelegate.BinaryOp(m.Add); // Invoke Add() method indirectly using delegate object. Console.WriteLine($"10 + 10 is {b(10, 10)}"); DisplayDelegateInfo(b); Console.ReadLine(); }
static void Main(string[] args) { BinaryOp b = new BinaryOp(SimpleMath.Add); Console.WriteLine("10 + 10 is {0}", b(10,10)); Console.WriteLine("10 + 10 is {0}", b.Invoke(10, 10)); //Also ok Console.WriteLine(); DisplayDelegateInfo(b); SimpleMath m = new SimpleMath(); BinaryOp bInst = new BinaryOp(m.Subtract); DisplayDelegateInfo(bInst); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Simple Delegate Example *****\n"); SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); DisplayDelegateInfo(b); Console.WriteLine("10 + 10 is {0}", b(10, 10)); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("**** Simple Delegates Examples ****"); //Create a BinaryOp delegate object that //points to simpleMath.Add() SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); DisplayDelegateInfo(b); //Invoke Add() method indirectly using delegate object Console.WriteLine($"10 + 10 = {b(10,10)}"); Console.WriteLine(); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("**** Simple delegate example ****"); SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); Console.WriteLine("10 + 10 = {0}", b(10, 10)); Console.WriteLine(); DisplayDelegateInfo(b); //BinaryOp b2 = new BinaryOp(SimpleMath.SquareNumber); // Can't be compiled Console.WriteLine(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Simple Delegate Example *****\n"); // Create a BinaryOp delegate object that // "points to" SimpleMath.Add(). SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); // Invoke Add() method indirectly using delegate object. Console.WriteLine("10 + 10 is {0}", b(10, 10)); DisplayDelegateInfo(b); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("***** Simple Delegate Example *****\n"); // Create a BinaryOp object that // 'points to' SimpleMath.Add() SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); // Show information about this object. DisplayDelegateInfo(b); // Invoke Add() method using delegate. Console.WriteLine("\n10 + 10 is {0}", b(10, 10)); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Simple Delegate Example *****\n"); // .NET delegates can also point to instance methods as well. SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); // Error! Method does not match delegate pattern! // BinaryOp b2 = new BinaryOp(SimpleMath.SquareNumber); // Invoke Add() method using delegate. Console.WriteLine("10 + 10 is {0}", b.Invoke(10, 10)); DisplayDelegateInfo(b); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Simple Delegate Example *****"); SimpleMath m = new SimpleMath(); //创建一个指向SimpleMath.Add()方法的BinaryOp对象 BinaryOp b1 = new BinaryOp(SimpleMath.Add); //指向静态方法 //.NET委托也可以指向实例方法 BinaryOp b2 = new BinaryOp(m.Subtract); //指向非静态方法 DisplayDelegateInfo(b1); //这行代码的结果没有显示Type Name是因为b1委托指向静态方法,所以没有对象被引用 DisplayDelegateInfo(b2); //使用委托对象间接调用Add()方法 Console.WriteLine("10+10 is {0}", b1(10, 10));//Invoke()在这里被调用了,也可显式调用Invoke(),如:b.Invoke(10,10) Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Simple Delegate Example *****\n"); // .NET delegates can also point to instance methods as well. SimpleMath m = new SimpleMath(); // Create a BinaryOp delegate object that // "points to" SimpleMath.Add(). BinaryOp b = new BinaryOp(m.Add); DisplayDelegateInfo(b); Console.WriteLine(); // Invole Add() method indireclty using delegate object. Console.WriteLine("10 + 10 is {0}", b(10, 10)); Console.WriteLine(); }
static void Main( string[] args ) { Console.WriteLine("***** Simple Delegate Example *****\n"); // Create a BinaryOp delegate object that // "points to" SimpleMath.Add(). SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); DisplayDelegateInfo(b); // Invoke Add() method indirectly using delegate object. Console.WriteLine("10 + 10 is {0}", b(10, 10)); // Compiler error! Method does not match delegate pattern! // BinaryOp b2 = new BinaryOp(SimpleMath.SquareNumber); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("*****Simple Delegate Example * ****\n"); // Создать объект делегата BinaryOp, который "указывает" на SimpleMath.Add() //BinaryOp b = new BinaryOp(SimpleMath.Add); // Делегаты .NET могут также указывать на методы экземпляра. SimpleMath simpleMath = new SimpleMath(); BinaryOp b = new BinaryOp(simpleMath.Add); // Вывести сведения об объекте DisplayDelegateInfo(b); // Вызвать метод Add() косвенно с использованием объекта делегата. Console.WriteLine("10 + 10 is {0}", b(10, 10)); Console.WriteLine("10 + 10 is {0}", b.Invoke(10, 10)); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Simple Delegate Example *****\n"); // 创建一个指向SimpleMath.Add()方法的BinaryOp对象 SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); DisplayDelegateInfo(b); // 使用委托对象间接调用Add()方法 // Invoke()在这里被调用了 Console.WriteLine("10 + 10 is {0}", b.Invoke(10, 10)); // 编译器错误!方法不匹配委托的模式 //BinaryOp b2 = new BinaryOp(SimpleMath.SquareNumber); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Simple Delegate Example *****\n"); // Create a BinaryOp delegate object that // "points to" SimpleMath.Add(). SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); DisplayDelegateInfo(b); // Invoke Add() method indirectly using delegate object. Console.WriteLine("10 + 10 is {0}", b(10, 10)); // Compiler error! Method does not match delegate pattern! // BinaryOp b2 = new BinaryOp(SimpleMath.SquareNumber); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Simple Delegate Example *****\n"); ////create a BinaryOp delegate obj that POINTS to SimpleMath.Add ////using STATIC in the methods the delegate is pointing to //BinaryOp b = new BinaryOp(SimpleMath.Add); //DisplayDelegateInfo(b); ////Invoke Add() indirectly via delegate //Console.WriteLine("10 + 10 is {0}", b(10,10)); //not using STATIC in the methods the delegate points to SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); //show info about instantiated object (m) DisplayDelegateInfo(b); Console.WriteLine("10 + 10 is {0}", b(10,10)); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Simple Delegate Example *****\n"); // Ошибка! Метод не может быть вызван через данный деоегат! // BinaryOp b2 = new BinaryOp(SimpleMath.SquareNumber); // Делегаты .NET могут также указывать на методы экземпляров. SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); DisplayDelegateInfo(b); // Вызовем метод Add() косвенным образом, используя делегат. Console.WriteLine("10 + 10 is {0}", b.Invoke(10, 10)); BinaryOp b2 = new BinaryOp(m.Subtract); Console.WriteLine("10 - 10 is {0}", b2.Invoke(10, 10)); Console.ReadLine(); }
static void Main( string[] args ) { Console.WriteLine("***** Simple Delegate Example *****\n"); // Create a BinaryOp delegate object that // "points to" SimpleMath.Add(). SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); DisplayDelegateInfo(b); // Invoke Add() method indirectly using delegate object. Console.WriteLine("10 + 10 is {0}", b(10, 10)); b = new BinaryOp(m.Subtract); DisplayDelegateInfo(b); // Invoke Add() method indirectly using delegate object. Console.WriteLine("10 - 10 is {0}", b(10, 10)); // Compiler error! Method does not match delegate pattern! BinaryOpTest b2 = new BinaryOpTest(m.SquareNumber); DisplayDelegateInfo(b2); Console.WriteLine("10 * 10 is {0}", b2(10)); SimpleMath sm = new SimpleMath(); BinaryOp bTest = delegate(int x, int y) { return y + x; }; Console.WriteLine("10 + 10 is {0}", bTest(10,10)); bTest = (x, y) => y + x; Console.WriteLine("10 + 10 is {0}", bTest(10, 10)); SimpleMath math = new SimpleMath(); //math.TestHandler += new SimpleMath.TestEvent(); Console.ReadLine(); }