Skip to content

Instantly share code, notes, and snippets.

@ufcpp
Created April 3, 2019 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ufcpp/7b26d997724cc602b052f6b2f30fe08e to your computer and use it in GitHub Desktop.
Save ufcpp/7b26d997724cc602b052f6b2f30fe08e to your computer and use it in GitHub Desktop.
C# 8.0 preview in VS 2019 (16.0) の注意点
struct X
{
public int Value;
public X(int value) => Value = value;
// + と & で全く同じ処理を書いとく
public static X operator +(X x, X y) => new X(x.Value + y.Value);
public static X operator &(X x, X y) => new X(x.Value + y.Value);
public static explicit operator X(int x) => new X(x);
public void Deconstruct(out int x) => x = Value;
public override string ToString() => Value.ToString();
}
class Program
{
static void Main()
{
var x = new X(1);
var y = new X(2);
// + か & か以外は全く同じ
var add = x + y switch { X(2) => (X)10, X(3) => (X)20, _ => default };
var and = x & y switch { X(2) => (X)10, X(3) => (X)20, _ => default };
System.Console.WriteLine(add); // 今、 + の方が優先度高い → 20 (次(VS 16.1)のプレビューで変更予定。11 になる)
System.Console.WriteLine(and); // switch の方が優先度高い → 11
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment