Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
highlevel:csharp:xtra2 [2010/03/13 10:59] alfred |
highlevel:csharp:xtra2 [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| ====== C# Xtra (II) ====== | ====== C# Xtra (II) ====== | ||
| + | |||
| ===== Sockets ===== | ===== Sockets ===== | ||
| * [[code:tools#sockets|Explicación de 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 ===== | ===== Expresiones Regulares ===== | ||
| ==== In a nutshell ==== | ==== In a nutshell ==== | ||
| Línea 92: | Línea 124: | ||
| ===== Configuración ===== | ===== Configuración ===== | ||
| + | |||
| Línea 156: | 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> | ||