Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created May 31, 2014 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/fb07deabd07eff6f1b2e to your computer and use it in GitHub Desktop.
Save Fhernd/fb07deabd07eff6f1b2e to your computer and use it in GitHub Desktop.
Demostración de métodos plug-in con delegados en C#.
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