c# 6 - do it right vedran kaldi senior
DESCRIPTION
Visual Studio 2015 CTP 5 File -> New -> Project How to use today?TRANSCRIPT
#hashtag@speaker
C# 6 - do it right
Vedran KaldiSenior Developer @Five
No big new conceptsMany small featuresClean up your code
C# v6 design philosophy
Visual Studio 2015 CTP 5File -> New -> Project
How to use today?
Null-conditional operatorspublic 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;}
Null-conditional operatorspublic 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;}
?.
Null-conditional operatorspublic 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;}
Null-conditional operatorspublic 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;}
Null-conditional operators
OnChanged(this, args);
Null-conditional operators
if (OnChanged != null) { OnChanged(this, args); }
Null-conditional operators{ var onChanged = OnChanged; if (onChanged != null) { onChanged(this, args); }}
Null-conditional operators
OnChanged?.Invoke(this, args);
String interpolationusing 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); }}
String interpolationusing 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})"; }}
Expression-bodied methodsusing 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})"; }}
Expression-bodied methodsusing 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})"
Expression-bodied methodsusing 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})";}
Expression-bodied propertiesusing 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() => $"({X}, {Y})";}
Expression-bodied propertiesusing 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 => Sqrt(X * X + Y * Y); public override string ToString() => $"({X}, {Y})";}
Expression-bodied propertiesusing 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 => Sqrt(X * X + Y * Y); public override string ToString() => $"({X}, {Y})";}
The nameof operatorpublic Point Add(Point point){ if (point == null) { throw new ArgumentNullException("point"); }}
The nameof operatorpublic Point Add(Point other){ if (other == null) { throw new ArgumentNullException("point"); }}
The nameof operatorpublic Point Add(Point point){ if (point == null) { throw new ArgumentNullException(nameof(point)); }}
The nameof operatorpublic Point Add(Point other){ if (other == null) { throw new ArgumentNullException(nameof(other)); }}
Exception filterstry{ … }catch (ArgumentException e){ }finally{ }
Exception filterstry{ … }catch (ArgumentException e) if (e.ParamName == nameof(other)){ }finally{ }
Exception filterstry{ … }catch (ArgumentException e) if (e.ParamName == nameof(other)){}catch (ArgumentException e) if (e.ParamName == nameof(param2)){}finally{ }
Getter-only auto-propertiespublic 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); }}
Getter-only auto-propertiespublic 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); }}
Getter-only auto-propertiespublic class Point{ public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; // samo u konstruktoru!!! } public double Dist { get { return Math.Sqrt(X * X + Y * Y); } }
public override string ToString() { return String.Format("({0}, {1})", X, Y); }}
Getter-only auto-propertiespublic class Point{ public int X { get; } // Pitanje: kako ovo trenutno riješavamo? 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); }}
Getter-only auto-propertiespublic class Point{ public int X { get; private set; } 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); }}
Initializers for auto-propertiespublic 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); }}
Using static classes
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); }}
Using static classesusing 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); }}
Index initializerspublic class Point{ public int X { get; } public int Y { get; }
…
public Dictionary<string, int> ToJson() { var result = new Dictionary<string, int>(); result["x"] = X; result["y"] = Y; return result; }}
Index initializerspublic class Point{ public int X { get; } public int Y { get; }
…
public Dictionary<string, int> ToJson() { var result = new Dictionary<string, int>() { ["x"] = X, ["y"] = Y }; return result; }}
Index initializerspublic class Point{ public int X { get; } public int Y { get; }
…
public Dictionary<string, int> ToJson() { return new Dictionary<string, int>() { ["x"] = X, ["y"] = Y }; }}
Index initializerspublic class Point{ public int X { get; } public int Y { get; }
…
public Dictionary<string, int> ToJson() => new Dictionary<string, int>() { ["x"] = X, ["y"] = Y };}
Await in catch and finallytry{ … }catch (ConfigurationException e) if (e.IsSevere){ await LogAsync(e);}finally{ await CloseAsync();}
https://github.com/dotnet/roslynhttps://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14
Learn more at …