Skip to content

Instantly share code, notes, and snippets.

@markrendle
Last active August 29, 2015 13:56
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 markrendle/9050611 to your computer and use it in GitHub Desktop.
Save markrendle/9050611 to your computer and use it in GitHub Desktop.
Destructuring arbitrary types with attributes
// Modifiers on primary constructor params would be nice :-)
struct Point(private readonly double x, private readonly double y)
{
[DestructuringPosition(0)]
public double X { get; } = x;
[DestructuringPosition(1)]
public double Y { get; } = y;
}
class Program
{
static void Main()
{
var (x, y) = GetPosition();
Console.WriteLine("X = {0}, Y = {1}", x, y);
// or why not even?
Console.WriteLine("X = {0}, Y = {1}", GetPosition());
}
Point GetPosition()
{
// Structuring based on position of primary constructor parameters
return 100, 200;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment