Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created May 31, 2014 14:33
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/a32fd34b9e1a0785afad to your computer and use it in GitHub Desktop.
Save Fhernd/a32fd34b9e1a0785afad to your computer and use it in GitHub Desktop.
Delegados vs Interfaces en C#.
using System;
namespace Articulos.Cap04
{
public interface ITransformador
{
int Transformar (int x);
}
public class Utilitario
{
public static void TransformarTodo (int[] valores, ITransformador t)
{
for (int i = 0; i < valores.Length; i++)
{
valores[i] = t.Transformar (valores[i]);
}
}
}
public class Cuadrado : ITransformador
{
public int Transformar(int x)
{
return x * x;
}
}
public class Cubico : ITransformador
{
public int Transformar (int x)
{
return x * x * x;
}
}
public class Aplicacion
{
public static void Main()
{
int[] valores = {1, 2, 3, 4, 5};
Utilitario.TransformarTodo(valores, new 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.TransformarTodo(valores2, new Cubico());
Console.WriteLine("Cubo para los elementos de `Valores`:");
foreach (int i in valores2)
{
Console.Write("{0} ", i.ToString());
}
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment