Created
May 31, 2014 14:33
-
-
Save Fhernd/a32fd34b9e1a0785afad to your computer and use it in GitHub Desktop.
Delegados vs Interfaces 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 | |
{ | |
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