Created
May 31, 2014 04:59
-
-
Save Fhernd/fb07deabd07eff6f1b2e to your computer and use it in GitHub Desktop.
Demostración de métodos plug-in con delegados en C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Articulos.Cap04 | |
{ | |
// Delegado para transformar valores: | |
public delegate int Transformador(int x); | |
public class Utilitario | |
{ | |
public static void Transformar(int[] valores, Transformador t) | |
{ | |
for (int i = 0; i < valores.Length; ++i) | |
{ | |
valores[i] = t(valores[i]); | |
} | |
} | |
} | |
public class UtilMetodoPlugin | |
{ | |
public static void Main() | |
{ | |
int[] valores = {1, 2, 3, 4, 5}; | |
Utilitario.Transformar(valores, Cuadrado); | |
Console.WriteLine("Cuadrados para los elementos de `Valores`:"); | |
foreach (int i in valores) | |
{ | |
Console.Write("{0} ", i.ToString()); | |
} | |
Console.WriteLine(); | |
int[] valores2 = {1, 2, 3, 4, 5}; | |
Utilitario.Transformar(valores2, Cubo); | |
Console.WriteLine("Cubo para los elementos de `Valores`:"); | |
foreach (int i in valores2) | |
{ | |
Console.Write("{0} ", i.ToString()); | |
} | |
Console.WriteLine(); | |
} | |
public static int Cuadrado(int x) | |
{ | |
return x * x; | |
} | |
public static int Cubo(int x) | |
{ | |
return x * x * x; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment