static void ProcessAndDisplayNumber(DoubleOp action, double value)//委托类型做参数;可接受与委托方法签名一样的方法;实现方法做参数 { double result = action(value); Console.WriteLine( "Value is {0}, result of operation is {1}", value, result); }
//执行普通委托 static void ProcessAndDisplayNumber(DoubleOp action, double value) { //调用委托 double result = action(value); Console.WriteLine($"Value is {value}, result of operation is {result}"); }
// 指向不同的函数, 调用不同的函数 static void ProcessAndDisplayNumber(DoubleOp action, double value) { double result = action(value); Console.WriteLine( "Value is {0}, result of operation is {1}", value, result); }
/// <summary> /// 把操作应用到值上 把算法应用到数据上 /// </summary> /// <param name="action">委托</param> /// <param name="value">带处理的值</param> static void ProcessAndDisplayNumber(DoubleOp action, double value) { double result = action(value); //action 代表接收一个double参数 返回double值的一个委托 //在这里action代表 委托绑定的具体实现方法 具体的算法 WriteLine($"Value is {value},result of operation is {result}"); }
static void ProcessAndDisplayNumber(DoubleOp action, double value) { // 调用action委托实例封装的方法,相当于operations[i](value) double result = action(value); Console.WriteLine("Value is {0}, result of operations is {1}", value, result); }
static void Main() { DoubleOp op = new DoubleOp(MathOperations.MultiplyBy2); op += new DoubleOp(MathOperations.Square); ProcessAndDisplay(op, 2.0); ProcessAndDisplay(op, 17.5); ProcessAndDisplay(op, 2.5454); }
static void Main() { DoubleOp operations = MathOperations.MultiplyByTwo; operations += MathOperations.Square; ProcessAndDisplayNumber(operations, 2.0); ProcessAndDisplayNumber(operations, 7.94); ProcessAndDisplayNumber(operations, 1.414); Console.WriteLine(); }
public void TestDelegate1() { //DoubleOp operation = new DoubleOp(MathsOperations.MultiplyByTwo); DoubleOp operation = MathsOperations.MultiplyByTwo; double result = operation(123); Func <double, double> operation1 = MathsOperations.MultiplyByTwo; double result1 = operation1(1123); //Delegate[] delegates = operation1.GetInvocationList(); // 迭代委托方法 //foreach (Action d in delegates) // d(); }
private static void DelegateTest() { DoubleOp[] operations = new DoubleOp[] { MathOperations.MultiplyByTwo, MathOperations.Square }; for (int i = 0; i < operations.Length; i++) { Console.WriteLine("Using operations[{0}]:", i); ProcessAndDisplayName(operations[i], 2.0); ProcessAndDisplayName(operations[i], 7.94); ProcessAndDisplayName(operations[i], 1.414); Console.WriteLine(); } }
public delegate double DoubleOp(double db);//DoubleOp 为委托名 public static void Main(String[] args) { //使用委托 (这里虽然只用了静态方法 但委托可以用实例方法) DoubleOp gs = new DoubleOp(MathOperator.MultiplyByTwo); //依次执行委托中的方法 但顺序是不确定的 gs += MathOperator.Square; //这是上面形式的简写形式 gs(2.3); // 等价于 gs.Invoke() Console.WriteLine("xxxxxxxxxxxxx"); //用Action模版代替委托(注意 action 要求 函数返回值为void) Action <double> actionOp = MathOperator.MultiplyByTwoV; //其中一个函数抛出异常 整个委托调用就停止 actionOp += MathOperator.SquareV; //自己迭代委托中的函数 Delegate[] delegates = actionOp.GetInvocationList(); foreach (Action <double> d in delegates) { d(2.3); } Console.WriteLine("xxxxxxxxxxxxx"); //委托的数组 DoubleOp[] operArray = { MathOperator.MultiplyByTwo, MathOperator.Square }; for (int i = 0; i < 2; ++i) { ProcessAndDisplayNumber(operArray[i], 2.2); } Console.WriteLine("xxxxxxxxxxxxx"); //改成委托模版 Func <double, double>[] operT = { MathOperator.MultiplyByTwo, MathOperator.Square }; for (int i = 0; i < 2; ++i) { ProcessAndDisplayNumber(operT[i], 3.2); } Console.WriteLine("xxxxxxxxxxxxx"); }
static void Main(string[] args) { DoubleOp Func1 = new DoubleOp(MathOperations.MultiplyByTwo); double x = Func1(3); System.Console.WriteLine(x); Func1 = new DoubleOp(MathOperations.Square); x = Func1(3); System.Console.WriteLine(x); //passing a delegate to another function CSimpleDelegate.MyFunction(Func1, 4); System.Console.ReadLine(); /* Callthefactorial factptr = null; * * factptr = calculateFactorial; * * Console.WriteLine(factptr.Invoke(5).ToString()); * * Console.ReadLine(); */ }
static void ProcessAndDisplay(DoubleOp action , double val) { double res = action(val); Console.WriteLine("Value is {0}, action is {1 , 0:F2}", val , res); }
static void ProcessAndDisplayNumber(DoubleOp action, double valueToProcess) { Console.WriteLine("\nProcessAndDisplayNumber called with value = " + valueToProcess); action(valueToProcess); }
//What if I make it non static public static void MyFunction(DoubleOp op, double vals) { double res = op(vals); System.Console.WriteLine(res); }
//委托作为函数参数 static void ProcessAndDisplayNumber(DoubleOp action, double db) { double result = action(db); Console.WriteLine(result); }
static void ProcessAndDisplayNumber(DoubleOp action, double value) => Console.WriteLine($"Value is {value}, result of operation is {action(value)}");
private static void ProcessAndDisplayNumbers(DoubleOp action, double value) { double result = action(value); Console.WriteLine("value is => {0}, result of operation is => {1:f2}", value, result); }
static void ProcessAndDisplayNumber(DoubleOp action, double valueToProcess) { Console.WriteLine("\nProcessAndDisplayNumber called with value = " + valueToProcess); action(valueToProcess); }
static void ShowDouble(DoubleOp op, double double_num) { double result = op(double_num); Console.WriteLine("值为:" + result); }
void ProcessAndDisplayNumber(DoubleOp action, double value) { double result = action(value); Debug.WriteLine($"Value is {value}, result of operation is {result}"); }
static void ProcessAndDisplayNumber(DoubleOp action, double value) { double result = action(value); WriteLine($"Value is {value}, result of operation is {result}"); }
static void ProAndDis(DoubleOp action, double val) { double res = action(val); Con.WriteLine($"Value is {val}, result of operation is {res}"); }
static void ProcessAndDisplayNumber(DoubleOp action, double value) => Console.WriteLine($"原始数据:{value}, 对数据处理后: {action(value)}");
static void ProcessAndDisplay(DoubleOp action, double val) { action(val); }
static void ProcessAndDisplayNumber(DoubleOp action, double value) //把一个委托作为其第一个参数 { double result = action(value); //调用ProcessAndDisplayNumber方法 Console.WriteLine("value is {0},result of operation is {1}", value, result); }
internal Method Double(DoubleOp op) { return(x => FObject.NewF64(op.Invoke(x[0].IsFloat()))); }
static void ProcessAndDisplayNumber(DoubleOp action, double value) { double result = action(value); Console.WriteLine("value is {0},result of operation is {1}", value, result); }
public void ProcessAndDisplayNumber(DoubleOp operation, double number) { Console.WriteLine($"The result is: {operation(number)}"); }