C# C# 6.0
Ricardo Gonzalez Vargas
Microsoft Regional Director - Bogota
CEO – Androcial / WomyAds.com
Sénior IT Architecture & SD Consultant
http://about.me/ricardo.gonzalez
C#
Quien les habla?
C#
Agenda
• Evolución de C#
• C# 6.0 = C# + Roslyn
• Nuevas características
• Ejemplos
C#
Evolución de C#
C# 1.0 /
Managed
Code
C# 2.0 /
Generics
C# 3.0 / LINQ
C# 4.0 /
Dynamic
Programming
C# 5.0 / Async
Programming –
Windows
Runtime
C# 6.0 /
Compiler
Platform
C#
C# 6.0 y Roslyn
• Reimplementacion del compilador del lenguaje, escrito en el mismo
lenguaje
• Expone APIs para diferentes niveles de abstracción del proceso de
compilación
• Permite, análisis, diagnostico, manipulación y generación del código
basado en los servicios de lenguaje
C#
Filosofia de diseno
 No hay conceptos muy cambiantes
 Muchas funcionalidades pequeñas
 Enfocado en hacer mas limpio el código
C#
Propiedades automaticas de solo lectura
public class Point
{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist
{
get { return Math.Sqrt(X * X + Y * Y); }
}
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
}
C#
Propiedades automaticas de solo lectura
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist
{
get { return Math.Sqrt(X * X + Y * Y); }
}
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
}
C#
Propiedades automaticas de solo lectura
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist
{
get { return Math.Sqrt(X * X + Y * Y); }
}
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
}
C#
Inicializadores para propiedades automaticas
public class Point
{
public int X { get; } = 5;
public int Y { get; } = 7;
public Point(int x, int y) { X = x; Y = y; }
public double Dist
{
get { return Math.Sqrt(X * X + Y * Y); }
}
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
}
C#
Uso de clases estaticas
using System.Math;
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist
{
get { return Math.Sqrt(X * X + Y * Y); }
}
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
}
C#
Uso de clases estaticas
using System.Math;
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist
{
get { return Math.Sqrt(X * X + Y * Y); }
}
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
}
C#
Uso de clases estaticas
using static System.Math;
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist
{
get { return Sqrt(X * X + Y * Y); }
}
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
}
C#
Interpolación de cadenas
using System.Math;
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist
{
get { return Sqrt(X * X + Y * Y); }
}
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
}
C#
Interpolacion de cadenas
using System.Math;
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist
{
get { return Sqrt(X * X + Y * Y); }
}
public override string ToString()
{
return "({X}, {Y})";
}
}
C#
Metodos con expresiones como cuerpo
using System.Math;
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist
{
get { return Sqrt(X * X + Y * Y); }
}
public override string ToString()
{
return "({X}, {Y})";
}
}
C#
Metodos con expresiones como cuerpo
using System.Math;
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist
{
get { return Sqrt(X * X + Y * Y); }
}
public override string ToString()
{
return "({X}, {Y})";
}
}
() => { return "({X}, {Y})"; }
() => "({X}, {Y})"
C#
Metodos con expresiones como cuerpo
using System.Math;
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist
{
get { return Sqrt(X * X + Y * Y); }
}
public override string ToString() => "({X}, {Y})";
}
C#
Propiedades con expresiones como cuerpo
using System.Math;
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist
{
get { return Sqrt(X * X + Y * Y); }
}
public override string ToString() => "({X}, {Y})";
}
C#
Propiedades con expresiones como cuerpo
using System.Math;
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist => Sqrt(X * X + Y * Y);
public override string ToString() => "({X}, {Y})";
}
C#
Propiedades con expresiones como cuerpo
using System.Math;
public class Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) { X = x; Y = y; }
public double Dist => Sqrt(X * X + Y * Y);
public override string ToString() => "({X}, {Y})";
}
C#
Inicializadores de Indices
public class Point
{
public int X { get; }
public int Y { get; }
…
public JObject ToJson()
{
var result = new JObject();
result["x"] = X;
result["y"] = Y;
return result;
}
}
C#
Inicializadores de Indices
public class Point
{
public int X { get; }
public int Y { get; }
…
public JObject ToJson()
{
var result = new JObject() { ["x"] = X, ["y"] = Y };
return result;
}
}
C#
Inicializadores de Indices
public class Point
{
public int X { get; }
public int Y { get; }
…
public JObject ToJson()
{
return new JObject() { ["x"] = X, ["y"] = Y };
}
}
C#
Inicializadores de Indices
public class Point
{
public int X { get; }
public int Y { get; }
…
public JObject ToJson() =>
new JObject() { ["x"] = X, ["y"] = Y };
}
C#
Operadores condicionados con null
public static Point FromJson(JObject json)
{
if (json != null &&
json["x"] != null &&
json["x"].Type == JTokenType.Integer &&
json["y"] != null &&
json["y"].Type == JTokenType.Integer)
{
return new Point((int)json["x"], (int)json["y"]);
}
return null;
}
C#
Operadores condicionados con null
public static Point FromJson(JObject json)
{
if (json != null &&
json["x"]?.Type == JTokenType.Integer &&
json["y"]?.Type == JTokenType.Integer)
{
return new Point((int)json["x"], (int)json["y"]);
}
return null;
}
C#
Operadores condicionados con null
public static Point FromJson(JObject json)
{
if (json != null &&
json["x"]?.Type == JTokenType.Integer &&
json["y"]?.Type == JTokenType.Integer)
{
return new Point((int)json["x"], (int)json["y"]);
}
return null;
}
?.
C#
Operadores condicionados con null
public static Point FromJson(JObject json)
{
if (json != null &&
json["x"]?.Type == JTokenType.Integer &&
json["y"]?.Type == JTokenType.Integer)
{
return new Point((int)json["x"], (int)json["y"]);
}
return null;
}
C#
Operadores condicionados con null
public static Point FromJson(JObject json)
{
if (json?["x"]?.Type == JTokenType.Integer &&
json?["y"]?.Type == JTokenType.Integer)
{
return new Point((int)json["x"], (int)json["y"]);
}
return null;
}
C#
Operadores condicionados con null
OnChanged(this, args);
C#
Operadores condicionados con null
if (OnChanged != null)
{
OnChanged(this, args);
}
C#
Operadores condicionados con null
{
var onChanged = OnChanged;
if (onChanged != null)
{
onChanged(this, args);
}
}
C#
Operadores condicionados con null
OnChanged?.Invoke(this, args);
C#
Operador nameOf
public Point Add(Point point)
{
if (point == null)
{
throw new ArgumentNullException("point");
}
}
C#
Operador nameOf
public Point Add(Point other)
{
if (other == null)
{
throw new ArgumentNullException("point");
}
}
C#
Operador nameOf
public Point Add(Point point)
{
if (point == null)
{
throw new ArgumentNullException(nameof(point));
}
}
C#
Operador nameOf
public Point Add(Point other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
}
C#
Filtros de Excepcion
try
{
…
}
catch (ConfigurationException e)
{
}
finally
{
}
C#
Filtros de Excepcion
try
{
…
}
catch (ConfigurationException e) if (e.IsSevere)
{
}
finally
{
}
C#
Await en catch y finally
try
{
…
}
catch (ConfigurationException e) if (e.IsSevere)
{
await LogAsync(e);
}
finally
{
await CloseAsync();
}
C#
https://github.com/dotnet/roslyn
https://github.com/dotnet/roslyn/wiki/Langu
ages-features-in-C%23-6-and-VB-14
Learn more at …
C#

20150415 csharp6.0

  • 1.
    C# C# 6.0 RicardoGonzalez Vargas Microsoft Regional Director - Bogota CEO – Androcial / WomyAds.com Sénior IT Architecture & SD Consultant http://about.me/ricardo.gonzalez
  • 2.
  • 3.
    C# Agenda • Evolución deC# • C# 6.0 = C# + Roslyn • Nuevas características • Ejemplos
  • 4.
    C# Evolución de C# C#1.0 / Managed Code C# 2.0 / Generics C# 3.0 / LINQ C# 4.0 / Dynamic Programming C# 5.0 / Async Programming – Windows Runtime C# 6.0 / Compiler Platform
  • 5.
    C# C# 6.0 yRoslyn • Reimplementacion del compilador del lenguaje, escrito en el mismo lenguaje • Expone APIs para diferentes niveles de abstracción del proceso de compilación • Permite, análisis, diagnostico, manipulación y generación del código basado en los servicios de lenguaje
  • 6.
    C# Filosofia de diseno No hay conceptos muy cambiantes  Muchas funcionalidades pequeñas  Enfocado en hacer mas limpio el código
  • 7.
    C# Propiedades automaticas desolo lectura public class Point { public int X { get; set; } public int Y { get; set; } public Point(int x, int y) { X = x; Y = y; } public double Dist { get { return Math.Sqrt(X * X + Y * Y); } } public override string ToString() { return String.Format("({0}, {1})", X, Y); } }
  • 8.
    C# Propiedades automaticas desolo lectura public class Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public double Dist { get { return Math.Sqrt(X * X + Y * Y); } } public override string ToString() { return String.Format("({0}, {1})", X, Y); } }
  • 9.
    C# Propiedades automaticas desolo lectura public class Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public double Dist { get { return Math.Sqrt(X * X + Y * Y); } } public override string ToString() { return String.Format("({0}, {1})", X, Y); } }
  • 10.
    C# Inicializadores para propiedadesautomaticas public class Point { public int X { get; } = 5; public int Y { get; } = 7; public Point(int x, int y) { X = x; Y = y; } public double Dist { get { return Math.Sqrt(X * X + Y * Y); } } public override string ToString() { return String.Format("({0}, {1})", X, Y); } }
  • 11.
    C# Uso de clasesestaticas using System.Math; public class Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public double Dist { get { return Math.Sqrt(X * X + Y * Y); } } public override string ToString() { return String.Format("({0}, {1})", X, Y); } }
  • 12.
    C# Uso de clasesestaticas using System.Math; public class Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public double Dist { get { return Math.Sqrt(X * X + Y * Y); } } public override string ToString() { return String.Format("({0}, {1})", X, Y); } }
  • 13.
    C# Uso de clasesestaticas using static System.Math; public class Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public double Dist { get { return Sqrt(X * X + Y * Y); } } public override string ToString() { return String.Format("({0}, {1})", X, Y); } }
  • 14.
    C# Interpolación de cadenas usingSystem.Math; public class Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public double Dist { get { return Sqrt(X * X + Y * Y); } } public override string ToString() { return String.Format("({0}, {1})", X, Y); } }
  • 15.
    C# Interpolacion de cadenas usingSystem.Math; public class Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public double Dist { get { return Sqrt(X * X + Y * Y); } } public override string ToString() { return "({X}, {Y})"; } }
  • 16.
    C# Metodos con expresionescomo cuerpo using System.Math; public class Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public double Dist { get { return Sqrt(X * X + Y * Y); } } public override string ToString() { return "({X}, {Y})"; } }
  • 17.
    C# Metodos con expresionescomo cuerpo using System.Math; public class Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public double Dist { get { return Sqrt(X * X + Y * Y); } } public override string ToString() { return "({X}, {Y})"; } } () => { return "({X}, {Y})"; } () => "({X}, {Y})"
  • 18.
    C# Metodos con expresionescomo cuerpo using System.Math; public class Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public double Dist { get { return Sqrt(X * X + Y * Y); } } public override string ToString() => "({X}, {Y})"; }
  • 19.
    C# Propiedades con expresionescomo cuerpo using System.Math; public class Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public double Dist { get { return Sqrt(X * X + Y * Y); } } public override string ToString() => "({X}, {Y})"; }
  • 20.
    C# Propiedades con expresionescomo cuerpo using System.Math; public class Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public double Dist => Sqrt(X * X + Y * Y); public override string ToString() => "({X}, {Y})"; }
  • 21.
    C# Propiedades con expresionescomo cuerpo using System.Math; public class Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public double Dist => Sqrt(X * X + Y * Y); public override string ToString() => "({X}, {Y})"; }
  • 22.
    C# Inicializadores de Indices publicclass Point { public int X { get; } public int Y { get; } … public JObject ToJson() { var result = new JObject(); result["x"] = X; result["y"] = Y; return result; } }
  • 23.
    C# Inicializadores de Indices publicclass Point { public int X { get; } public int Y { get; } … public JObject ToJson() { var result = new JObject() { ["x"] = X, ["y"] = Y }; return result; } }
  • 24.
    C# Inicializadores de Indices publicclass Point { public int X { get; } public int Y { get; } … public JObject ToJson() { return new JObject() { ["x"] = X, ["y"] = Y }; } }
  • 25.
    C# Inicializadores de Indices publicclass Point { public int X { get; } public int Y { get; } … public JObject ToJson() => new JObject() { ["x"] = X, ["y"] = Y }; }
  • 26.
    C# Operadores condicionados connull public static Point FromJson(JObject json) { if (json != null && json["x"] != null && json["x"].Type == JTokenType.Integer && json["y"] != null && json["y"].Type == JTokenType.Integer) { return new Point((int)json["x"], (int)json["y"]); } return null; }
  • 27.
    C# Operadores condicionados connull public static Point FromJson(JObject json) { if (json != null && json["x"]?.Type == JTokenType.Integer && json["y"]?.Type == JTokenType.Integer) { return new Point((int)json["x"], (int)json["y"]); } return null; }
  • 28.
    C# Operadores condicionados connull public static Point FromJson(JObject json) { if (json != null && json["x"]?.Type == JTokenType.Integer && json["y"]?.Type == JTokenType.Integer) { return new Point((int)json["x"], (int)json["y"]); } return null; } ?.
  • 29.
    C# Operadores condicionados connull public static Point FromJson(JObject json) { if (json != null && json["x"]?.Type == JTokenType.Integer && json["y"]?.Type == JTokenType.Integer) { return new Point((int)json["x"], (int)json["y"]); } return null; }
  • 30.
    C# Operadores condicionados connull public static Point FromJson(JObject json) { if (json?["x"]?.Type == JTokenType.Integer && json?["y"]?.Type == JTokenType.Integer) { return new Point((int)json["x"], (int)json["y"]); } return null; }
  • 31.
    C# Operadores condicionados connull OnChanged(this, args);
  • 32.
    C# Operadores condicionados connull if (OnChanged != null) { OnChanged(this, args); }
  • 33.
    C# Operadores condicionados connull { var onChanged = OnChanged; if (onChanged != null) { onChanged(this, args); } }
  • 34.
    C# Operadores condicionados connull OnChanged?.Invoke(this, args);
  • 35.
    C# Operador nameOf public PointAdd(Point point) { if (point == null) { throw new ArgumentNullException("point"); } }
  • 36.
    C# Operador nameOf public PointAdd(Point other) { if (other == null) { throw new ArgumentNullException("point"); } }
  • 37.
    C# Operador nameOf public PointAdd(Point point) { if (point == null) { throw new ArgumentNullException(nameof(point)); } }
  • 38.
    C# Operador nameOf public PointAdd(Point other) { if (other == null) { throw new ArgumentNullException(nameof(other)); } }
  • 39.
    C# Filtros de Excepcion try { … } catch(ConfigurationException e) { } finally { }
  • 40.
    C# Filtros de Excepcion try { … } catch(ConfigurationException e) if (e.IsSevere) { } finally { }
  • 41.
    C# Await en catchy finally try { … } catch (ConfigurationException e) if (e.IsSevere) { await LogAsync(e); } finally { await CloseAsync(); }
  • 42.
  • 43.