Queste sono le nuove funzionalità
- Auto property initializers
- Static members
- String interpolation
- Expression-bodied methods
- Expression-bodied properties
- Index initializers
- Null-conditional operators
- The nameof operator
- Exception filters
- Await in catch and finally
E questo un esempio di utilizzo (ho usato MonoDevelop 5.9)
using System;
using Newtonsoft.Json.Linq;
using System.Configuration;
//Con - Static members
using static System.Math;
namespace Azzan.Mono4Sample
{
public class Point
{
/*
//Prima di - Auto property initializers
public int X {get; private set;}
public int Y {get; private set;}
public Point()
{
X = 4;
Y = 9;
}
*/
//Con - Auto property initializers
public int X {get; } = 4;
public int Y {get; } = 9;
public Point(int x, int y) { X = x; Y = y; }
//Prima di Expression-bodied properties
/*
public double Dist
{
//Prima di - Static members
//get { return Math.Sqrt(X * X + Y * Y); }
//Con - Static members
get { return Sqrt(X * X + Y * Y); }
}
*/
//Con - Expression-bodied properties
public double Dist => Sqrt(X * X + Y * Y);
//Prima di - Expression-bodied methods
/*
public override string ToString()
{
//Prima di - String interpolation
//return string.Format("[Point: X={0}, Y={1}, Dist={2}]", X, Y, Dist);
//Con - String interpolation
return $"[Point: X={X}, Y={Y}, Dist={Dist}]";
}
*/
//Con - Expression-bodied methods
public override string ToString() => $"[Point: X={X}, Y={Y}, Dist={Dist}]";
//Prima di - Index initializers
/*
public JObject ToJson()
{
var result = new JObject();
result["x"] = X;
result["y"] = Y;
return result;
}
*/
//Con - Index initializers
public JObject ToJson() => new JObject() { ["x"] = X, ["y"] = Y };
//Prima di - 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;
}
*/
//Con - 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;
}
public Point Add(Point point)
{
if(point == null)
{
//Prima di - The nameof operator
//throw new ArgumentNullException("point");
//Con - The nameof operator
throw new ArgumentNullException(nameof(point));
}
return point;
}
public void DoSomething()
{
//Prima di - Exception filters
try
{
} //Con - Exception filters
catch(ConfigurationException ex) //when (ex.IsSevere)
{
//Con - Await in catch and finally
//await LogAsync(ex);
}
finally
{
//Con - Await in catch and finally
//await CloseAsync();
}
}
}
}
Qui trovate i sorgenti dell'esempio creati con MonoDevelop 5.9: Download esempioPurtroppo ho notato però che l'editor di MonoDevelop 5.9 ha ancora qualche problema nel riconoscere la sintassi C# 6.0 ma non sono cose gravi.
Invece gli "Exception filters" proprio non funzionano, se de-commentate il codice del mio esempio il compilatore vi da errore.
Che versione di Mono è installata?
mono --version

Nessun commento:
Posta un commento