Skip to content

Instantly share code, notes, and snippets.

@sakapon
Last active August 31, 2020 05:46
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 sakapon/ac87a691cdb62b1debfdfe551de321f7 to your computer and use it in GitHub Desktop.
Save sakapon/ac87a691cdb62b1debfdfe551de321f7 to your computer and use it in GitHub Desktop.
OperatorsSample/Vector2 struct
namespace OperatorsLib.Structs
{
public struct Vector2
{
public double X { get; }
public double Y { get; }
public Vector2(double x, double y) => (X, Y) = (x, y);
public void Deconstruct(out double x, out double y) => (x, y) = (X, Y);
public static implicit operator Vector2((double x, double y) v) => new Vector2(v.x, v.y);
public static explicit operator (double, double)(Vector2 v) => (v.X, v.Y);
}
}
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OperatorsLib.Structs;
namespace UnitTest.Structs
{
[TestClass]
public class Vector2Test
{
[TestMethod]
public void Cast()
{
Vector2 v = (3, 4);
var (a, b) = v;
var t = ((double c, double d))v;
Assert.AreEqual(v, (a, b));
Assert.AreEqual(v, t);
Assert.AreEqual((v.X, v.Y), (t.c, t.d));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment