Examples showing several ways to return data using Deconstructing
Created
August 21, 2024 13:52
-
-
Save karenpayneoregon/5c7da046b54c7e7e8eb80f022fb8ac67 to your computer and use it in GitHub Desktop.
Deconstruction basics
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
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); | |
} | |
} |
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
public record struct ItemContainer(int Id, string ContentId, string Answer); |
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
public record ItemsContainer(int Id, string ContentId, string Answer); |
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
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