Advertisement
Fhernd

UsoActionT1T2.cs

Sep 7th, 2014
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace Articulos.Cap04
  5. {
  6.     public sealed class UsoActionT1T2
  7.     {
  8.         public static void Main()
  9.         {
  10.             // Par de cadenas a concatenar:
  11.             string cadena1 = "OrtizOL - Experiencias ";
  12.             string cadena2 = "Construcción Software (xCSw)";
  13.            
  14.             // Uso del delegado genérico Action<T1, T2>:
  15.             Action<string, string> concatenar;
  16.            
  17.             // Si el número de argumentos de la línea de comandos
  18.             // es mayor a 1:
  19.             if (Environment.GetCommandLineArgs().Length > 1)
  20.             {
  21.                 concatenar = (s1, s2) => Console.WriteLine ("{0}{1}", s1, s2);
  22.             }
  23.             else // ...en caso contrario...
  24.             {
  25.                 concatenar = (s1, s2) => EscribirAArchivo(s1, s2);
  26.             }
  27.            
  28.             concatenar(cadena1, cadena2);
  29.         }
  30.        
  31.         // Método que concatena dos cadenas y las escribe en un archivo de texto:
  32.         public static void EscribirAArchivo(string s1, string s2)
  33.         {
  34.             StreamWriter sw = null;
  35.            
  36.             try
  37.             {
  38.                 sw = new StreamWriter("usoactiont1t2.txt", true);
  39.                 sw.WriteLine ("{0}{1}", s1, s2);
  40.             }
  41.             catch
  42.             {
  43.                 Console.WriteLine ("La operación de escritura ha fallado.");
  44.             }
  45.             finally
  46.             {
  47.                 if (sw != null)
  48.                 {
  49.                     sw.Close();
  50.                 }
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement