static void Main(string[] args) { MyDelegat my = new MyDelegat(TestMethos); my += TestMethos1; my("Galya"); }
static void Main(string[] args) { MyDelegat c = new MyDelegat(SimpleMath.summ); Console.WriteLine("Summ of 2 elemnt is:{0}", c(14.5, 18.2)); c = new MyDelegat(SimpleMath.decr); Console.WriteLine("Summ of 2 elemnt is:{0}", c(14.5, 18.2)); Console.ReadLine(); }
static void Main(string[] args) { MyDelegat my = delegate(string str) //анонимен метод { Console.WriteLine("result is: " + str); }; my("AZ"); }
public void NamedAndOptionalArgumentsTest() { MyDelegat del1 = Meth; del1(true, dt: DateTime.Now, i: 11); Meth(false, sss: "string", ii: 111); MyDelegat del2 = delegate(bool b, int i, string s, DateTime dt) {}; MyDelegat del3 = (bool b, int i, string s, DateTime dt) => {}; }
static void Main(string[] args) { MyDelegat delegat1 = () => { return("hello"); }, deleget2 = () => { return("World"); }; Functional functional = delegate(MyDelegat d1, MyDelegat d2) { return(delegate() { return d1.Invoke() + d2.Invoke(); }); }; Console.WriteLine(functional.Invoke(delegat1, deleget2)); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("enter the first number"); double a = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("enter the second number"); double b = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("enter the operator (+,-,*,/)"); string c = Console.ReadLine(); MyDelegat op = null; switch (c) { case "+": op = (x, y) => { return(x + y); }; break; case "-": op = (x, y) => { return(x - y); }; break; case "*": op = (x, y) => { return(x * y); }; break; case "/": op = (x, y) => { if (y != 0) { return(x / y); } else { Console.WriteLine("cante dividet by 0"); return(0); } }; break; default: Console.WriteLine("wrong operator"); break; } Console.WriteLine(new string('-', 30)); if (op != null) { Console.WriteLine("{0,-12:#.###}{1,-12:#.###}", op(a, b), op(a, b)); } Console.ReadKey(); }
static void Main(string[] args) { MyDelegat a = (x, y) => x + y; MyDelegat s = (x, y) => x - y; MyDelegat m = (x, y) => x * y; MyDelegat d = (x, y) => x / y; int add = a(12, 23); int sub = s(15, 10); int mul = m(2, 3); int del = d(4, 2); Console.WriteLine("Додавання {0} Віднімання {1} Множення {2} Розподіл {3}", add, sub, mul, del); Console.ReadKey(); }
static void Main() { Console.WriteLine("Введите действие--> +,-,*,/"); string a = Console.ReadLine(); Console.WriteLine("Первое число -->"); int x = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Второе число -->"); int y = Convert.ToInt32(Console.ReadLine()); switch (a) { case "+": MyDelegat myDelegat = (c, b) => c + b; // Выполнение сумирования чисел double Add = myDelegat(x, y); Console.WriteLine("Ответ:{0}", Add); break; case "-": MyDelegat myDelegat1 = (c, b) => c - b; // Выполнение вычитания чисел double Sub = myDelegat1(x, y); Console.WriteLine("Ответ:{0}", Sub); break; case "*": MyDelegat myDelegat2 = (c, b) => c * b; // Выполнение умножения чисел double Muv = myDelegat2(x, y); Console.WriteLine("Ответ:{0}", Muv); break; case "/": MyDelegat myDelegat3 = (c, b) => b != 0 ? c / b : 0; // Выполнение деления чисел, проверка 0 // b=0, то вернет 0, :?-тернарный оператор. double Div = myDelegat3(x, y); Console.WriteLine("Ответ:{0}", Div); break; } Console.ReadKey(); }
static int Find(Object[] arr, MyDelegat myDelegat) => myDelegat(arr);