public void AddShortCut(Keys[] keys, FuncDel func, object[] args) { if (IsCapturing) { MessageBox.Show("You cannot add another shortcut while the script is capturing shortcuts!"); } else { if (keys.Length > 0) { bool nFound = false; if (shortcuts.Count > 0) { foreach (ShortcutInfo shortcut in shortcuts) { if (shortcut.Shortcut.Length == keys.Length) { for (int i = 0; i < shortcut.Shortcut.Length; i++) { if (shortcut.Shortcut[i] != keys[i]) { nFound = true; break; } } if (!nFound) { break; } } } } else { nFound = true; } if (nFound) { ShortcutInfo si = new ShortcutInfo(); si.Callback = func; si.Shortcut = keys; si.Arguments = args; shortcuts.Add(si); } else { MessageBox.Show("The shortcut \"" + String.Join("+", keys) + "\" already exists."); } } } }
static void Main(string[] args) { Func <double, double> fn = Math.Sin; Console.WriteLine($"Value of Sin: {fn(30.0) }"); //user defined function for delegate FuncDel funcDel = Math.Sin; Console.WriteLine($"Value of Sin: {funcDel(30.0) }"); fn = Program.Square; // calling user defined function Console.WriteLine($"Square of a number: {fn(5.00)}"); Func <int, bool> evenOdd = Program.IsEven; // calling user defined function Console.WriteLine($"Given number is Even: {evenOdd(11)}"); Func <string, string, string, string> maxString = Program.Max; // calling user defined function-Max Console.WriteLine($"Largest String: {maxString("Sujeet", "Tin", "Zhing")}"); Func <double, double, double> sum = Program.Sum; // calling user defined function-Sum Console.WriteLine($"Addition : { sum(12.5, 2.4)}"); Func <double, double> delSq = Square; Console.WriteLine(delSq(4)); Func <string, string, string, string> s4 = Max; Console.WriteLine(s4("me", "she", "he")); Console.WriteLine($"largest of three iS {s4("me", "she", "he")}"); Func <double, double, double> dSum = sum; Console.WriteLine($"sum of two num is {dSum(5,6)}"); Action <double> sq2 = Square2; sq2(5); } // end Main()
private double calcS(FuncDel f, double a, double b, int split) { // Вычисление площади трапеций double S = 0; double dX = (b - a) / split; double y1 = f(a); for (int i = 0; i < split; i++) { double y2 = f(a + dX * (i + 1)); S += (y1 + y2) / 2.0f * dX; y1 = y2; } return(S); }
public double calculate(FuncDel f, double a, double b, double precision, out double fault, out int split) { double S1, S2; split = 1; S2 = calcS(f, a, b, split); do { S1 = S2; split *= 2; S2 = calcS(f, a, b, split); } while (Math.Abs(S2 - S1) > precision && split < 10000000); fault = Math.Abs(S2 - S1); // оценка Рунге return(S2); }
//delegate double DelSquare(double number); // delgate declaration static void Main(string[] args) { Func <double, double> fn = Math.Sin; Console.WriteLine($"Value of Sin: {fn(30.0) }"); FuncDel funcDel = Math.Sin; Console.WriteLine($"Value of Sin: {funcDel(30.0) }"); fn = Program.Square; // calling user defined function Console.WriteLine($"Square of a number: {fn(5.00)}"); // it is a built in template for delgates Func <int, bool> evenOdd = Program.IsEven; // calling user defined function Console.WriteLine($"Given number is Even: {evenOdd(11)}"); Func <string, string, string, string> maxString = Program.Max; // calling user defined function-Max Console.WriteLine($"Largest String: {maxString("Sujeet", "Tin", "Zhing")}"); Func <double, double, double> sum = Program.Sum; // calling user defined function-Sum Console.WriteLine($"Addition : { sum(12.5, 2.4)}"); //DelSquare delsquare = Square; // create a delgate variable and assign a function to that. Func <double, double> delsquare = Square; Console.WriteLine(delsquare(4)); Func <string, string, string, string> delmax = Max; Console.WriteLine(delmax("Sujeet", "Jonh", "Rick")); Console.WriteLine("Largest of three strings: {0}", delmax("Sujeet", "Jonh", "Rick")); Console.WriteLine($"Largest of three strings: { delmax("Sujeet", "Jonh", "Rick")}"); Action <double> delsqr2 = Square2; } // end Main()
static void Main(string[] args) { FuncDel f1 = Func1; Console.WriteLine(f1(5)); f1 += Func2; Console.WriteLine(f1(5)); processFunc(4.5, f1); FuncPrint <double, FuncDel> printDel = processFunc; printDel += processFunc2; printDel(6.5, f1); Func <double, double> f2 = Func2; f2 += Func1; Console.WriteLine(f2(3)); }
public string CallFuncDel(FuncDel someFunc) => someFunc(Name, 5);
/// <summary> /// 使用委托的无返回值的函数 /// </summary> /// <param name="del">委托类型参数</param> public void FuncFromDelegate(FuncDel del) { }
public Function(string name, FuncDel f) { Name = name; Func = f; }
static void processFunc2(double x, FuncDel f) { Console.WriteLine(f(x)); Console.WriteLine(f(x + 1)); }