Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 10, 2014 16:19
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/65aed1af98f4975df7a9 to your computer and use it in GitHub Desktop.
Save Fhernd/65aed1af98f4975df7a9 to your computer and use it in GitHub Desktop.
Demostración del uso del delgado genérico Action<T1, T2> con expresiones lambda en C#.
using System;
using System.IO;
namespace Articulos.Cap04
{
public sealed class UsoActionT1T2
{
public static void Main()
{
// Par de cadenas a concatenar:
string cadena1 = "OrtizOL - Experiencias ";
string cadena2 = "Construcción Software (xCSw)";
// Uso del delegado genérico Action<T1, T2>:
Action<string, string> concatenar;
// Si el número de argumentos de la línea de comandos
// es mayor a 1:
if (Environment.GetCommandLineArgs().Length > 1)
{
concatenar = (s1, s2) => Console.WriteLine ("{0}{1}", s1, s2);
}
else // ...en caso contrario...
{
concatenar = (s1, s2) => EscribirAArchivo(s1, s2);
}
concatenar(cadena1, cadena2);
}
// Método que concatena dos cadenas y las escribe en un archivo de texto:
public static void EscribirAArchivo(string s1, string s2)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter("usoactiont1t2.txt", true);
sw.WriteLine ("{0}{1}", s1, s2);
}
catch
{
Console.WriteLine ("La operación de escritura ha fallado.");
}
finally
{
if (sw != null)
{
sw.Close();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment