Après des années d’évolution, C# continue d’évoluer dans une 6ème version que l’on peut considérer comme mineure mais qui apporte une multitude de nouveautés très utiles. Je suis prêt à parier que bon nombre d’entre vous les utiliseront tous les jours !
3. The Evolution of C#
C# 1.0
C# 2.0
C# 3.0
Managed Code
Generics
Language Integrated Query
C# 4.0
Dynamic Programming
C# 5.0
Asynchrony (await)
4. .Net Compiler Platform: Roslyn
•INTRODUCTION
•EXPOSING THE COMPILER APIS
• COMPILER PIPELINE FUNCTIONAL AREAS
• API LAYERS
• Compiler APIs
• Workspaces APIs
•WORKING WITH SYNTAX
• SYNTAX TREES
• SYNTAX NODES
• SYNTAX TOKENS
• SYNTAX TRIVIA
• SPANS
• KINDS
• ERRORS
•WORKING WITH SEMANTICS
• COMPILATION
• SYMBOLS
• SEMANTIC MODEL
•WORKING WITH A WORKSPACE
• WORKSPACE
• SOLUTIONS, PROJECTS, DOCUMENTS
•SUMMARY
5. Getter-only auto-properties
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);
}
}
6. Getter-only auto-properties
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);
}
}
7. Getter-only auto-properties
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);
}
}
8. Initializers for auto-properties
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);
}
}
9. Using static classes
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);
}
}
10. Using static classes
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);
}
}
11. Using static classes
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);
}
}
12. String interpolation
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);
}
}
13. String interpolation
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})";
}
}
14. Expression-bodied methods
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})";
}
}
15. Expression-bodied methods
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})"
16. Expression-bodied methods
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})";
}
17. Expression-bodied properties
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})";
}
18. Expression-bodied properties
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})";
}
19. Expression-bodied properties
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})";
}
20. Index initializers
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;
}
}
21. Index initializers
public class Point
{
public int X { get; }
public int Y { get; }
…
public JObject ToJson()
{
var result = new JObject() { ["x"] = X, ["y"] = Y };
return result;
}
}
22. Index initializers
public class Point
{
public int X { get; }
public int Y { get; }
…
public JObject ToJson()
{
return new JObject() { ["x"] = X, ["y"] = Y };
}
}
23. Index initializers
public class Point
{
public int X { get; }
public int Y { get; }
…
public JObject ToJson() =>
new JObject() { ["x"] = X, ["y"] = Y };
}
24. Null-conditional operators
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;
}
25. Null-conditional operators
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;
}
26. Null-conditional operators
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;
}
?.
27. Null-conditional operators
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. Null-conditional operators
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;
}