Herramientas de usuario

Herramientas del sitio


highlevel:csharp:xtra2

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
highlevel:csharp:xtra2 [2009/03/11 14:29]
alfred
highlevel:csharp:xtra2 [2020/05/09 09:25] (actual)
Línea 2: Línea 2:
  
  
 +===== Sockets =====
 +  * [[code:​tools#​sockets|Explicación de sockets]].
  
 +==== Creación de un servidor RAW ====
 +<code csharp>
 +System.Net.Sockets.TcpListener server = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Any,​ 2005);
 +server.Start();​
 +Byte[] bytes = new Byte[256];
 +String data = null;
  
 +while (true)
 +{
 +    System.Net.Sockets.TcpClient client = server.AcceptTcpClient();​
 +    data = null;
 +    System.Net.Sockets.NetworkStream stream = client.GetStream();​
 +    int i;
 +    while ((i = stream.Read(bytes,​ 0, bytes.Length)) != 0)
 +    {
 +        data = System.Text.Encoding.ASCII.GetString(bytes,​ 0, i);
 +        Console.WriteLine("​Received:​ {0}", data);
 +        data = data.ToUpper();​
 +        byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);​
 +        stream.Write(msg,​ 0, msg.Length);​
 +        Console.WriteLine("​Sent:​ {0}", data);
 +    }
 +}
 +</​code>​
 +Para enviar un "salto de línia"​ haremos:
 +<code csharp>
 +byte[] msg = System.Text.Encoding.ASCII.GetBytes(data + "​\n\r"​);​
 +stream.Write(msg,​ 0, msg.Length);​
 +</​code>​
  
 +===== Expresiones Regulares =====
 +==== In a nutshell ====
 +En ''​System.Text.RegularExpressions''​ existe la clase ''​Regex''​ que se encarga de localizar combinaciones (matches) de expresiones regulares en un texto. Por ejemplo, su método estático ''​Matches''​ recibe un texto y una expresión regular y devuelve objetos ''​Match''​ de las coincidencias. \\ 
 +Un objeto ''​Match''​ tiene las siguientes propiedades interesantes:​
 +  * ''​NextMatch()'': ​
 +  * ''​Index'':​ Posición dentro del string original donde se encuentra el primer carácter de la coincidencial.
 +  * ''​Lenght'':​
 +  * ''​Success'':​
 +  * ''​Value'':​ Recoge el substring de la coincidencia encontrada.
  
- +<code csharp> 
- +string startBy = "​src=\"​|src = \"​|src= \"|src =\"";​ 
 +var matches = System.Text.RegularExpressions.Regex.Matches(text,​ startBy, System.Text.RegularExpressions.RegexOptions.IgnoreCase);​ 
 +List<​string>​ lImgNames = new List<​string>​();​ 
 +for (int i = 0; i < matches.Count;​ i++) 
 +
 +   var match = matches[i];​ 
 +   int start = match.Index + match.Value.Length;​ 
 +   int end = text.IndexOf("​\"",​ start); 
 +   ​string file = text.Substring(start,​ end - start); 
 +   ​lImgNames.Add(file);​ 
 +
 +</​code>​
  
 ===== Comunicación entre procesos ===== ===== Comunicación entre procesos =====
Línea 75: Línea 124:
  
 ===== Configuración ===== ===== Configuración =====
 +
 +
  
  
Línea 92: Línea 143:
 Utilizaremos un objeto de la clase **AppSettingsReader** y su método ''​GetValue'':​ Utilizaremos un objeto de la clase **AppSettingsReader** y su método ''​GetValue'':​
 <code csharp> <code csharp>
-new System.Configuration.AppSettingsReader().GetValue("​logFile", typeof(String))+(string)new System.Configuration.AppSettingsReader().GetValue("​dbFile", typeof(String));
 </​code>​ </​code>​
 === Agregar secciones === === Agregar secciones ===
Línea 138: Línea 189:
 IDictionary confTable = (IDictionary)System.Configuration.ConfigurationManager.GetSection("​atm.ws/​dbInfo"​);​ IDictionary confTable = (IDictionary)System.Configuration.ConfigurationManager.GetSection("​atm.ws/​dbInfo"​);​
 string dataSource = (string)confTable["​DataSource"​];​ string dataSource = (string)confTable["​DataSource"​];​
 +</​code>​
 +
 +
 +
 +===== Programación declarativa con C# =====
 +  * [[code:​best-practices#​programacion_declarativa|Programación declarativa]]
 +==== Métodos ====
 +Podemos realizar un bucle ''​foreach''​ con el método ''​List<​T>​.ForEac''​ y una expresión lambda.
 +<code csharp>
 +using System;
 +using System.Collections.Generic;​
 +
 +class Example
 +{
 +    static void Main()
 +    {
 +        new List<​Int32>​ { 1, 2, 3 }
 +            .ForEach(i => Console.WriteLine(i));​
 +    }
 +}
 +</​code>​
 +De una forma parecida podemos sacar partido del delagado ''​Action<​T>'',​ el cual permite una funcion ''​Action<​Int32>''​ el cual encaja con ''​Console.WriteLine''​.
 +<code csharp>
 +using System;
 +using System.Collections.Generic;​
 +
 +class Example
 +{
 +    static void Main()
 +    {
 +        new List<​Int32>​ { 1, 2, 3 }
 +            .ForEach(Console.WriteLine);​
 +    }
 +}
 +</​code>​
 +También el método ''​Enumerable.Range'':​
 +<code csharp>
 +using System;
 +using System.Linq;​
 +
 +class Example
 +{
 +    static void Main()
 +    {
 +        Int32 sum = Enumerable.Range(0,​ 99)
 +                      .Where(i => i % 2 == 0)
 +                      .Sum();
 +        Console.WriteLine(sum);​
 +    }
 +}
 </​code>​ </​code>​
highlevel/csharp/xtra2.1236781795.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)