Skip to content

Instantly share code, notes, and snippets.



Last active Aug 31, 2020
What would you like to do?
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
public class Vector2Test
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