Last active
August 29, 2015 13:56
-
-
Save markrendle/9050611 to your computer and use it in GitHub Desktop.
Destructuring arbitrary types with attributes
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
// 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