Skip to content

Instantly share code, notes, and snippets.

@rstropek
Last active June 6, 2021 07:25
Show Gist options
  • Save rstropek/b5000e681e1c7b495fef3d96ad319502 to your computer and use it in GitHub Desktop.
Save rstropek/b5000e681e1c7b495fef3d96ad319502 to your computer and use it in GitHub Desktop.
using System;
var v1 = new Vector2d(1d, 2d);
v1.X = 3d; // This works because get and set are generated by default
Console.WriteLine(v1.X);
Console.WriteLine(v1); // record structs implement ToString
var v2 = v1 with { X = 4d }; // We can use the with keyword
Console.WriteLine(v2.X);
// Note: At the time of writing, stackallow can be compiled on sharplab.io, but cannot be executed.
// If you want to run this code on sharplab.io, comment the following statement.
Span<Vector2d> vectors = stackalloc Vector2d[]
{
new Vector2d(1d, 2d),
new Vector2d(3d, 4d),
};
// New struct record (=value type)
record struct Vector2d(double X, double Y)
{
public static Vector2d operator +(Vector2d first, Vector2d second) =>
new Vector2d(first.X + second.X, first.Y + second.Y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment