public void LearnAnonymousDelegate() { SumDelegate delegater = delegate() { return(1 + 2); }; SumDelegate delegater1 = () => 1 + 2; Assert.Equal(delegater1(), delegater()); }
static void Main(string[] args) { SumDelegate objDelegate = sum; objDelegate(10, 20); Console.ReadLine(); }
public void Mymethod(int first, int second) { SumDelegate delObj = new SumDelegate((x, y) => { return(x + y); }); int result = delObj.Invoke(first, second); Console.WriteLine(result); }
static void Main(string[] args) { SingleDelegate d1 = new SingleDelegate(); // d1.sum(10, 20); //Console.WriteLine(Delegate1.Program(" " + "Amar")); //2.instantiating the delegating SumDelegate sd = new SumDelegate(d1.sum); sd(100, 200); //passing the required parameter and calling the delegate sd.Invoke(200, 300); ProgramDelegate pd = new ProgramDelegate(Program); Console.WriteLine(pd("Amar")); Console.WriteLine(pd.Invoke("Ram")); MulDelegate md = new MulDelegate(d1.Mul); //non-static method call by ref of instance class Console.WriteLine("Mul:" + md(10, 2)); DivDelegate dv = new DivDelegate(Div); Console.WriteLine("Div:" + dv(100, 2)); Console.Read(); }
public delegate void Log <T>(T msg); // Action<T1> static void Main(string[] args) { SumDelegate <int> sumCalculator = delegate(int a, int b) { return(a + b); }; SumDelegate <int> secondSumCalculator = sumCalculator; Console.WriteLine(secondSumCalculator(3, 4)); // Func<> does not require same generic parameter types Func <float, double, int> funcSumCalculator = delegate(float a, double b) { return((int)Math.Round(a + b)); }; Console.WriteLine(funcSumCalculator(7, 7)); Action <string> actionHelloWorld = delegate(string name) { Console.WriteLine($"Hello, {name}"); }; actionHelloWorld("Marto"); Console.WriteLine("Press any key to continue.."); Console.ReadKey(); }
static void Main(string[] args) { var currentThread = Thread.CurrentThread; Console.WriteLine($"Поток {currentThread.ManagedThreadId} работает..."); //first step: Get Problem with thread //var result = Sum(10, 15); //second step: trying to use delegate //SumDelegate sumDelegate = Sum; //var result = sumDelegate.Invoke(10, 15); //third step: async call method from delegate //SumDelegate sumDelegate = Sum; //var asyncResult = sumDelegate.BeginInvoke(10, 15, null, null); //while (!asyncResult.IsCompleted) //{ // Console.WriteLine("Дожидаемся ответа"); // Thread.Sleep(500); //} //var result = sumDelegate.EndInvoke(asyncResult); //fourth step: awareness of the inevitable. Function call back. SumDelegate sumDelegate = Sum; sumDelegate.BeginInvoke(10, 15, SumCallback, "Результат длительной операции сумма"); Console.WriteLine($"Поток {currentThread.ManagedThreadId} заканчивает работу"); Console.ReadLine(); }
public static void Init() { SumDelegate sumDelegate = Sum; Console.WriteLine(sumDelegate(10, 20)); int Sum(int num1, int num2) => num1 + num2; }
private string GetSum(SumDelegate funk) { string inp = fReader.ReadLine(); inp += inp[0]; int[] num = ParseMas(inp); fReader.BaseStream.Position = 0; return(funk(num).ToString()); }
static void Main(string[] args) { SayHello sayHello = new SayHello(WelcomeMessage); SayHello sayHello1 = new SayHello(SaySomething); //sayHello(); //sayHello1(); var example = new Example(); ShowMessage showMessage = new ShowMessage(example.PrintMessage); SumDelegate sumDelegate = new SumDelegate(example.SumOfTwoNumbers); //showMessage("Some message here ...", "Trajan"); //var sum = sumDelegate(4, 8); //Console.WriteLine(sum); SayHello sayHello2 = new SayHello(() => Console.WriteLine("Anonymous function in delegate")); ShowMessage showMessage1 = new ShowMessage((x, y) => Console.WriteLine($"Im x with value {x}, im y with value {y}")); SumDelegate sumDelegate1 = new SumDelegate((x, y) => x - y); SumDelegate sumDelegate2 = new SumDelegate((x, y) => { return(x - y); }); //sayHello2(); //showMessage1("You have new message", "Tosho"); //int result = sumDelegate1(10, 6); //Console.WriteLine(result); Action action = () => Console.WriteLine("Im action"); SayHello actionParam = new SayHello(action); ShowMessage showMessage10 = new ShowMessage((x, y) => Console.WriteLine($"Im x with value {x}, im y with value {y}")); showMessage10 += new ShowMessage((x, y) => Console.WriteLine($"Second fnc Im x with value {x}, im y with value {y}")); showMessage10("Im x", "Im y"); Filter filter = new Filter(x => x % 2 == 0); List <int> listOfIntegers = new List <int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; var result = listOfIntegers.Where(x => x % 2 == 0).ToList(); var result1 = listOfIntegers.Where(x => filter(x)).ToList(); PrintNumbers(listOfIntegers, filter); Console.ReadLine(); }
static void Main(string[] args) { AsyncCallback callback = new AsyncCallback(MyCallback); int state = 1000; SumDelegate d = new SumDelegate(Add); d.BeginInvoke(100, 200, callback, state); Console.WriteLine("Press any key to exit"); Console.ReadLine(); }
public static void MethodWithReturnType() { SumDelegate sumDelegate = delegate(int x, int y) { return(x + y); }; Console.WriteLine(sumDelegate(4, 5)); }
public static void Main(){ SumDelegate sumDelegate1 = new SumDelegate(Sum); SumDelegate sumDelegate2 = new SumDelegate(Sum); IAsyncResult result1 = sumDelegate1.BeginInvoke(1, 2, new AsyncCallback(WrapUp), sumDelegate1); IAsyncResult result2 = sumDelegate2.BeginInvoke(3, 4, new AsyncCallback(WrapUp), sumDelegate2); while(!(result1.IsCompleted && result2.IsCompleted)){ Thread.Sleep(10); } Console.WriteLine("\nMain thread exiting now...bye!"); } // end Main() method
static void Main(string[] args) { SumDelegate sd = new SumDelegate(program.Sum); sd(500, 600); DivDelegate dd = new DivDelegate(program1.Division); dd(8, 2); Console.ReadKey(); }
public static void MyCallback(IAsyncResult result) { AsyncResult ar = (AsyncResult)result; SumDelegate d = (SumDelegate)ar.AsyncDelegate; int state = (int)ar.AsyncState; int i = d.EndInvoke(result); Console.WriteLine(i); Console.WriteLine(state); Console.ReadLine(); }
public static void ExecuteMain() { //Declare Delegate Object SumDelegate objDelegate = null; //Point to method reference in our example "Sum" objDelegate = Sum; //Final Step Invoke Delegate objDelegate.Invoke(10, 20); Console.Read(); }
static void Main(string[] args) { //Declare Delegate Objetc SumDelegate objDelegate = null; //Point to method reference in our example "Sum" objDelegate = Sum; //Final Step Invoke Delegate objDelegate.Invoke(10, 20); Console.ReadLine(); }
static void Main() { // untuk membuat delegate kita membuat instance dari delegate HelloFunctionDelegate del = new HelloFunctionDelegate(Hello); SumDelegate sum = new SumDelegate(SumNumber); // secara otomatis function Hello akan di invoke dan delegate memberikan argumentnya del("Hello delegate"); int num1, num2; sum(out num1, out num2); Console.WriteLine("num 1 = {0}, num2 = {1}", num1, num2); }
public static void Main(){ SumDelegate sumDelegate1 = new SumDelegate(Sum); SumDelegate sumDelegate2 = new SumDelegate(Sum); IAsyncResult result1 = sumDelegate1.BeginInvoke(1, 2, null, null); IAsyncResult result2 = sumDelegate2.BeginInvoke(3, 4, null, null); while(!(result1.IsCompleted && result2.IsCompleted)){ Thread.Sleep(100); } int sum1 = sumDelegate1.EndInvoke(result1); int sum2 = sumDelegate2.EndInvoke(result2); Console.WriteLine("The result of the first async method call is: {0}", sum1); Console.WriteLine("The result of the second async method call is: {0}", sum2); Console.WriteLine("\nMain thread exiting now...bye!"); } // end Main() method
public static int Sum(int start, int end, int step, bool isRecursive = true) { SumDelegate sumDelegate; if (isRecursive) { sumDelegate = new SumDelegate(SumRecursive); } else { sumDelegate = new SumDelegate(SumIterative); } return(sumDelegate(start, end, step)); }
static void Main(string[] args) { Program p = new Program(); DisplayDelegate dd = new DisplayDelegate(Display); dd.Invoke(3.42f, 5.6f); SumDelegate sd = new SumDelegate(Sum); int result = sd.Invoke(60, 90); Console.WriteLine(result); GreetingDelegate gd = new GreetingDelegate(p.Greeting); bool name = gd.Invoke("kavya"); Console.WriteLine(name); Console.ReadKey(); }
static void Main() { MyDelegate d = null; d += Method1; d += Method2; d.Invoke(); ShowTextDelegate d2 = new ShowTextDelegate(ShowText); d2("Hello World..."); SumDelegate d3 = new SumDelegate(Sum); int result = d3.Invoke(12, 15); Console.WriteLine(result); }
static void Main(string[] args) { // 대리자에 메서드 등록 후 호출 PrintDelegate pd = new PrintDelegate(Print.Show); pd("대리자 사용"); // 무명메서드 사용 PrintDelegate am = delegate(string msg) { System.Console.WriteLine(msg); }; // 무명 메서드만 사용 SumDelegate sd = delegate(int x, int y) { System.Console.WriteLine(x + y); }; sd(5, 8); }
public Question_MaxContiguousSum(AlgoType type) { switch (type) { case AlgoType.MinesCorrect: GetMaxContiguousSum = GetMaxContiguousSumMinesCorrect; break; case AlgoType.MinesWrong: GetMaxContiguousSum = GetMaxContiguousSumMinesWrong; break; case AlgoType.Kadane: GetMaxContiguousSum = GetMaxContiguousSumKadane; break; } }
static void Main(string[] args) { Console.WriteLine("запуск приилож"); int mainThredId = Thread.CurrentThread.ManagedThreadId; Console.WriteLine("Поток-- {0}...", mainThredId); SumDelegate sumDelegate = new SumDelegate(Sum); // sumDelegate.Invoke(2.3, 5.6); //IAsyncResult result= sumDelegate.BeginInvoke(2.3, 5.6, null, null); IAsyncResult result = sumDelegate.BeginInvoke(2.3, 5.6, new AsyncCallback((incomingResult) => Console.WriteLine(sumDelegate.EndInvoke(incomingResult))), null); Console.WriteLine("Завершение работы"); Console.ReadLine(); }
public static void Main() { //[1] 메서드 직접 호출 Print.Show("안녕하세요."); //[2] 대리자에 메서드 등록 후 호출 PrintDelegate pd = new PrintDelegate(Print.Show); pd("반갑습니다."); //[3] 무명(익명) 메서드로 호출 : .NET 2.0 이상 PrintDelegate am = delegate(string msg) { Console.WriteLine(msg); }; am("또 만나요."); //[4] 무명메서드 호출 SumDelegate sd = delegate(int a, int b) { Console.WriteLine(a + b); }; sd(3, 5); // 8 }
static void Main(string[] args) { Console.Write("Enter the number of calculating: "); var n = int.Parse(Console.ReadLine()); var a = 1.0; var r = 1.0 / 2.0; Console.Write("1) 1 + 1/2 + 1/4 + 1/8 + 1/16 + ...\n2) 1 + 1/2! + 1/3! + 1/4! + 1/5! + ...\n3) 1 + 1/2 - 1/4 + 1/8 - 1/16 + ...\n"); Console.Write("Choose infinite series: "); switch (int.Parse(Console.ReadLine())) { case 1: Console.Clear(); SumDelegate first = FirstSum; Console.WriteLine($"Дурничка"); //Console.WriteLine($"Result: {first?.Invoke(a, r, n)}"); break; case 2: Console.Clear(); SumDelegate second = SecondSum; //Console.WriteLine($"Result: {second?.Invoke(a, r, n)}"); Console.WriteLine($"Дурничка"); break; case 3: Console.Clear(); SumDelegate third = ThirdSum; Console.WriteLine($"Дурничка"); break; default: Console.Clear(); Console.WriteLine("Something went wrong!"); break; } Console.ReadLine(); }
public static void Main(string[] args) { //[1] 다중 메서드 호출 : 따로 따로 호출 Hello.Hi(); Hello hello = new Hello(); hello.Bye(); //[2] 대리자를 통한 메서드 호출 // 대리자형식의 변수에 2개의 메서드 등록 EventHandler eh; eh = new EventHandler(Hello.Hi); eh += new EventHandler(hello.Bye); eh -= new EventHandler(Hello.Hi); //등록제거 eh(); // 대리자를 통한 2개의 메서드 호출 //[3] 매개변수가 있는 메서드 등록/호출 SumDelegate sum = new SumDelegate(Sum.Exec); sum(10, 20); // 30 sum(30, 30); }
//上面是委托的声明方法 //static SumDelegate sumd = new SumDelegate(Test.Sum);//也可以放在这,也可以放在下面 static void Main(string[] args) { //这里就是具体阐述这个命令是干什么的,本例是MrZhang.BuyTicket“小张买车票”这时候委托被附上了具体的方法 //注意,声明委托可以不使用delegate关键字 BugTicketEventHandler myDelegate = new BugTicketEventHandler(MrZhang.BuyTicket); //上面的语句声明一个委托并且订阅了小张买票,当使用委托时,小张买票函数就会执行 myDelegate += MrZhang.BuyMovieTicket; //多播委托 myDelegate(); //使用委托,括号内可以加参数,如果委托有返回值,这可以定义一个变量存放这个委托的返回值 Console.WriteLine(); myDelegate -= MrZhang.BuyMovieTicket; myDelegate(); Console.WriteLine(); myDelegate += () => { Console.WriteLine("匿名方法:3.0规范lambda表达式!突然出现一个人去买票!");//lambda表达式 }; myDelegate(); Console.WriteLine(); myDelegate += delegate() { Console.WriteLine("匿名方法:2.0版本的规范匿名方法实现!黄牛把票都买光了!");//lambda表达式 }; myDelegate(); //////////以下是简版委托 Console.WriteLine("简版委托"); SumDelegate sumd = new SumDelegate(Test.Sum);//也可以放在这,也可以放在上面面 int sumnum = sumd(5, 6); Console.WriteLine(); sumd += Test.Sum; sumd(5, 6); Console.WriteLine(); sumd += Test.Sub; sumd(5, 6); Console.WriteLine("sumnum=" + sumnum); Console.ReadKey(); }
static void Main(string[] args) { Console.Write("Введiть число для обчислення: "); var n = int.Parse(Console.ReadLine()); var a = 1.0; var r = 1.0 / 2.0; Console.WriteLine("1) 1 + 1/2 + 1/4 + 1/8 + 1/16 + ..."); Console.WriteLine("2) 1 + 1/2! + 1/3! + 1/4! + 1/5! + ..."); Console.WriteLine("3) 1 + 1/2 - 1/4 + 1/8 - 1/16 + ..."); Console.WriteLine("Виберiть бескiнечний ряд: "); switch (int.Parse(Console.ReadLine())) { case 1: SumDelegate first = FirstSum; Console.WriteLine($"Результат: {first?.Invoke(a, r, n)}"); break; case 2: SumDelegate second = SecondSum; Console.WriteLine($"Результат: {second?.Invoke(a, r, n)}"); break; case 3: SumDelegate third = ThirdSum; Console.WriteLine($"Результат: {third?.Invoke(a, r, n)}"); break; default: Console.WriteLine("Помилка!"); break; } Console.ReadLine(); }
static void Main(string[] args) { DisplayDelegate dd = delegate(float x, float y) { Console.WriteLine(x + y); }; dd.Invoke(3.02f, 4.2f); SumDelegate sd = delegate(int x, int y) { return(x + y); }; int result = sd.Invoke(60, 90); Console.WriteLine(result); GreetingDelegate gd = delegate(string name) { return(true); }; bool name1 = gd.Invoke("friends"); Console.WriteLine(name1); Console.ReadKey(); }
private long SumItems(IEnumerable<ProductionLeg> legs, SumDelegate d) { long result = 0; foreach (var leg in legs) { result += d(leg); } return result; }