Delegate and Lambda expressions in C #
Today, when I read the Curator source code, I found that the Retry Mechanism was encapsulated in the ZooKeeper cluster when I requested it. The Code is as follows:
Stat resultStat = RetryLoop.callWithRetry ( client.getZookeeperClient(), new Callable
() { @Override public Stat call() throws Exception { return client.getZooKeeper().setData(path, data, version); } } );
Public static
T callWithRetry (CuratorZookeeperClient client, Callable
Proc) throws Exception {T result = null; RetryLoop retryLoop = client. newRetryLoop (); while (retryLoop. shouldContinue () {try {client. internalBlockUntilConnectedOrTimedOut (); // call the Callable method with the returned value result = proc. call (); retryLoop. markComplete ();} catch (Exception e) {retryLoop. takeException (e) ;}} return result ;}
Use Callable in Java That is, the thread with the returned value T is used to pass the code segment to the retry function for calling.
Because I want to develop the corresponding version of C #, I also need to pass a piece of code (the Code requesting ZooKeeper) to such a function with the retry failure function, since I am not very familiar with C #, all I can think of is to declare a proxy and then define a function to operate on ZooKeeper and assign this function to the proxy, pass the proxy to a function with the retry function.
However, this method is bound to define a proxy and explicitly declare that a method is assigned to the proxy. The code is large and not very elegant, so I began to understand how C # is implemented in this respect, then we found the anonymous method introduced by C #2.0 and the Lambda expression introduced by C #3.0 to replace the anonymous method, which is the first choice for writing Inline code. Next we will provide a piece of test code that I have written to meet my needs, and then explain what an anonymous method and Lambda expression are.
CustomDelegate. cs:
using System;using System.Collections.Generic;using System.Linq;namespace DelegateAndLamda{ delegate string CustomDelegate(string material);}
PizzaMaker. cs:
Namespace DelegateAndLamda {class PizzaMaker {public static void makePizza (string material, CustomDelegate bake) {// There are a bunch of fixed processes before, but the subsequent baking method is different string result = bake (material ); console. writeLine (result );}}}
Program. cs
Namespace DelegateAndLamda {class Program {public static void Main (string [] args) {// in C # versions prior to 2.0, the only way to declare a delegate is to use the naming method CustomDelegate bake = new CustomDelegate (bakePizza); PizzaMaker. makePizza (Zhang San's Pizza, bake); // C #2.0 introduces the anonymous method PizzaMaker. makePizza (Pizza of Li Si, delegate (string material) {return String. format (bake {0} into bread, material) ;}); // in C #3.0 and later versions, Lambda expressions Replace the anonymous method, as the preferred way to write Inline code, PizzaMaker. makePizza (Pizza, material => {return String. format (bake {0} into Italian pizza, material) ;}) ;}public static string bakePizza (string material) {return String. format (bake {0} into watermelon, material );}}}