Last active
August 31, 2020 05:46
-
-
Save sakapon/ac87a691cdb62b1debfdfe551de321f7 to your computer and use it in GitHub Desktop.
OperatorsSample/Vector2 struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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