Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created August 21, 2024 13:52
Show Gist options
  • Save karenpayneoregon/5c7da046b54c7e7e8eb80f022fb8ac67 to your computer and use it in GitHub Desktop.
Save karenpayneoregon/5c7da046b54c7e7e8eb80f022fb8ac67 to your computer and use it in GitHub Desktop.
Deconstruction basics
public class Deconstruction
{
private static ItemContainer[] _responseKeys =
[
new ItemContainer(1, "A", "Yes"),
new ItemContainer(2, "B", "No"),
new ItemContainer(3, "C", "Yes"),
new ItemContainer(4, "A", "No")
];
private static ItemsContainer[] _responseKeys2 =
[
new ItemsContainer(1, "A", "Yes"),
new ItemsContainer(2, "B", "No"),
new ItemsContainer(3, "C", "Yes"),
new ItemsContainer(4, "A", "No")
];
public static ItemContainer Demo1(int id)
=> _responseKeys.FirstOrDefault(x => x.Id == id);
public static ItemsContainer Demo2(int id)
=> _responseKeys2.FirstOrDefault(x => x.Id == id);
public static (int id, string contentId, string answer) Conventional(int id)
{
var item = _responseKeys.FirstOrDefault(x => x.Id == id);
return (item.Id, item.ContentId, item.Answer);
}
}
public record struct ItemContainer(int Id, string ContentId, string Answer);
public record ItemsContainer(int Id, string ContentId, string Answer);
internal partial class Program
{
static void Main(string[] args)
{
{
var (_, contentId, answer) = Deconstruction.Demo1(1);
}
{
var (id, contentID, answer) = Deconstruction.Demo2(1);
}
{
var (id, contentId, answer) = Deconstruction.Conventional(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment