Created
June 10, 2014 16:19
-
-
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#.
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; | |
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