E-mail
// can be used to create a variable that can point to any method that takes an int parameter and returns a string public delegate string GetSecretIngredient(int amount);
// so variable ingredientMethod of type delegate GetSecretIngredient currently points to a null method GetSecretIngredient ingredientMethod = null;
private void getSuzanne_Click(object sender, EventArgs e) { // suzanne.MySecretIngredientMethod property returns a new instance of the the GetSecretIngredient delegate thats // pointing to her secret ingredient method. ingredientMethod = new GetSecretIngredient(suzanne.MySecretIngredientMethod); }
private void useIngredient_Click(object sender, EventArgs e) { if (ingredientMethod != null) Console.WriteLine("I'll add " + ingredientMethod((int)amount.Value)); else Console.WriteLine("I dont have a secret ingredient"); }
Remember Me
a@href@title, strike