Skip to content

Instantly share code, notes, and snippets.

@krist00fer
Last active August 29, 2015 14:09
Show Gist options
  • Save krist00fer/8baa67bf99a36ac62977 to your computer and use it in GitHub Desktop.
Save krist00fer/8baa67bf99a36ac62977 to your computer and use it in GitHub Desktop.
What's New in C# 6.0?
namespace CSharp6
{
// What's New In C# 6.0?
//
// The following class and the implementation was created as a personal reference after
// watching Mads Torgersen's video. Most of this example is from him.
//
// - What's New In C# 6.0 (http://channel9.msdn.com/Events/Visual-Studio/Connect-event-2014/116)
//
// Features
//
// 1) Getter-only auto-properties.
// Similar to readonly fields and values can be set in constructor only (or default value)
// 2) Initializers for auto-properties.
// Similar to default values for fields
// 3) Using static classes.
// Let's you reference static classes not just namespaces
// 4) String interpolation.
// Replaces need for string.Format(...) by using escaped curly braces, \{...} , inside strings
// 5) Expression-bodied methods.
// Simplifies methods, with a single expression, using the lambda arrow, => ...
// 6) Expression-bodied properties.
// Simplifies properties, with a single expression, using the lambda arrow, => ...
// 7) Index initializers.
// Like initializing properties in an object initializer but for index setters
// 8) Null-conditional operators.
// Questionmark + dot, ?. aka. The Elvis Operator, or the Questionmark + Index Operator, ?[...]
// If left side is null, all is null
// 9) The nameof operator.
// Gets the name of a program element as a string. Among others, can help avoid refactoring errors
// when renaming program elements. Really usefull instead of "magic strings"
// 10) Exception filters.
// Do stuff like: try {...} catch (ConfigurationException e) if (e.IsSevere) {...}
// 11) Await in catch and finally.
// Let you call asynchronous methods in catch and finally clauses using the await keyword
//
// 10 & 11 is not currently demonstrated in the sample
using System;
using System.Math; // C#6.0 Feature 3
using Newtonsoft.Json.Linq;
public class Point
{
public int X { get; } = 5; // C#6.0 Feature 1 & 2
public int Y { get; } = 7; // C#6.0 Feature 1 & 2
public Point() { }
public Point(int x, int y) { X = x; Y = y; } // C#6.0 Feature 1
public double Dist => Sqrt(X * X + Y * Y); // C#6.0 Feature 3 & 6
public override string ToString() => "(\{X}, \{Y})"; // C#6.0 Feature 4 & 5
public JObject ToJson() => new JObject() {["x"] = X,["y"] = Y }; // C#6.0 Feature 5 7
public static Point FromJson(JObject json)
{
if (json?["x"]?.Type == JTokenType.Integer && // C#6.0 Feature 8
json?["y"]?.Type == JTokenType.Integer) // C#6.0 Feature 8
return new Point((int)json["x"], (int)json["y"]);
else
return null;
}
public Point Add(Point point)
{
if (point == null)
{
throw new ArgumentException(
message: "Missing required argument",
paramName: nameof(point)); // C#6.0 Feature 9
}
return new Point(X + point.X, Y + point.Y);
}
}
}
namespace CSharp5
{
// Implementation of the same class as above using C# 5.0 features only
using System;
using Newtonsoft.Json.Linq;
public class Point
{
private readonly int x = 5;
private readonly int y = 7;
public int X { get { return x; } }
public int Y { get { return y; } }
public Point() { }
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public double Dist
{
get
{
return Math.Sqrt(X * X + Y * Y);
}
}
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
public JObject ToJson()
{
var result = new JObject();
result["x"] = X;
result["y"] = Y;
return result;
}
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"]);
else
return null;
}
public Point Add(Point point)
{
if (point == null)
{
throw new ArgumentException(
message: "Missing required argument",
paramName: "point");
}
return new Point(x + point.X, y + point.Y);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment