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;
}
}
}
dXNpbmcgU3lzdGVtOwoKbmFtZXNwYWNlIEFydGljdWxvcy5DYXAwNAp7CiAgICAvLyBEZWxlZ2FkbyBwYXJhIHRyYW5zZm9ybWFyIHZhbG9yZXM6CiAgICBwdWJsaWMgZGVsZWdhdGUgaW50IFRyYW5zZm9ybWFkb3IoaW50IHgpOwogICAgCiAgICBwdWJsaWMgY2xhc3MgVXRpbGl0YXJpbwogICAgewogICAgICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBUcmFuc2Zvcm1hcihpbnRbXSB2YWxvcmVzLCBUcmFuc2Zvcm1hZG9yIHQpCiAgICAgICAgewogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHZhbG9yZXMuTGVuZ3RoOyArK2kpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHZhbG9yZXNbaV0gPSB0KHZhbG9yZXNbaV0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgCiAgICBwdWJsaWMgY2xhc3MgVXRpbE1ldG9kb1BsdWdpbgogICAgewogICAgICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBNYWluKCkKICAgICAgICB7CiAgICAgICAgICAgIGludFtdIHZhbG9yZXMgPSB7MSwgMiwgMywgNCwgNX07CiAgICAgICAgICAgIFV0aWxpdGFyaW8uVHJhbnNmb3JtYXIodmFsb3JlcywgQ3VhZHJhZG8pOwogICAgICAgICAgICAKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIkN1YWRyYWRvcyBwYXJhIGxvcyBlbGVtZW50b3MgZGUgYFZhbG9yZXNgOiIpOwogICAgICAgICAgICBmb3JlYWNoIChpbnQgaSBpbiB2YWxvcmVzKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlKCJ7MH0gIiwgaS5Ub1N0cmluZygpKTsKICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIGludFtdIHZhbG9yZXMyID0gezEsIDIsIDMsIDQsIDV9OwogICAgICAgICAgICBVdGlsaXRhcmlvLlRyYW5zZm9ybWFyKHZhbG9yZXMyLCBDdWJvKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJDdWJvIHBhcmEgbG9zIGVsZW1lbnRvcyBkZSBgVmFsb3Jlc2A6Iik7CiAgICAgICAgICAgIGZvcmVhY2ggKGludCBpIGluIHZhbG9yZXMyKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlKCJ7MH0gIiwgaS5Ub1N0cmluZygpKTsKICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoKTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgcHVibGljIHN0YXRpYyBpbnQgQ3VhZHJhZG8oaW50IHgpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4geCAqIHg7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIHB1YmxpYyBzdGF0aWMgaW50IEN1Ym8oaW50IHgpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4geCAqIHggKiB4OwogICAgICAgIH0KICAgIH0KfQ==