{"id":18798784,"url":"https://github.com/liteobject/csharp-deconstruct","last_synced_at":"2026-01-02T12:30:14.586Z","repository":{"id":90788541,"uuid":"537858712","full_name":"LiteObject/csharp-deconstruct","owner":"LiteObject","description":"Example of deconstructing operation - unpacking variables from types - in C#","archived":false,"fork":false,"pushed_at":"2022-09-17T21:12:58.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-12-29T18:24:34.880Z","etag":null,"topics":["csharp","deconstruct","dotnet"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LiteObject.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-17T15:59:47.000Z","updated_at":"2023-04-09T16:40:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"87f8c34f-480b-471b-bfdd-cdf0abe13d58","html_url":"https://github.com/LiteObject/csharp-deconstruct","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2Fcsharp-deconstruct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2Fcsharp-deconstruct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2Fcsharp-deconstruct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2Fcsharp-deconstruct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LiteObject","download_url":"https://codeload.github.com/LiteObject/csharp-deconstruct/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239727259,"owners_count":19687139,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","deconstruct","dotnet"],"created_at":"2024-11-07T22:12:59.334Z","updated_at":"2026-01-02T12:30:14.533Z","avatar_url":"https://github.com/LiteObject.png","language":"C#","readme":"# Deconstruction in C#: Unpacking variables from types \n\u003eDeconstructing is unpacking types into single pieces; for instance, a tuple into its items or a class into its properties.\n\nLet's learn more about this process by looking at the following code examples.\n\n## Example: Deconstruction of a tuple object \n\n```csharp\n// declare a tuple\nvar book = (\"C-Sharp Basics\", \"Jon Doe\", 1.99);\n\n// now we can destruct the tuples into individual items\nvar (title, author, price) = book;\n\n// use them like variables\nConsole.WriteLine($\"Title: {title}\");\nConsole.WriteLine($\"Author: {author}\");\nConsole.WriteLine($\"Price: {price}\");\n\n```\n\n## Example: Deconstruction of a dictionary object\n\n```csharp\n// declare a dictionary object\nvar books = new Dictionary\u003cstring, string\u003e{\n    {\"Book One\", \"Author One\"},\n    {\"Book Two\", \"Author Two\"},\n};\n\n// unpack each item into a key/value pair (title/author pair) and loop through\nforeach((string k, string v) in books)\n{\n    Console.WriteLine($\"\\\"{k}\\\" written by {v}\");\n}\n```\n## Example: Deconstruction of a class\nWe need to implement the `Deconstruct` method. We can have multiple implementation of this method by overloading.\n\n```csharp\n// declare a class with Deconstruct method(s)\nclass MyBook\n{\n    public int Id {get; set;}\n    public string Title {get; set;}\n    public string Author {get; set;}\n\n    public MyBook(int id, string title, string author)\n    {\n        Id = id;\n        Title = title;\n        Author = author;\n    }\n\n    // must have the \"out\" modifier with each param\n    public void Deconstruct(out int id, out string title, out string author)\n    {\n        id = Id;\n        title = Title;\n        author = Author;\n    }\n\n    // must have the \"out\" modifier with each param\n    public void Deconstruct(out string title, out string author)\n    {\n        title = Title;\n        author = Author;\n    }\n}\n\n```\nLet's use the MyBook class\n\n```csharp\n// instantiate the \"MyBook\" class\nvar mybook = new MyBook(1, \"CSharp Bacis\", \"Jon Doe\");\n\n// deconstruct class properties into a collection of variables\nvar (myBookTitle, myBookAuthor) = mybook;\n\n// use the variables as needed\nConsole.WriteLine($\"\\\"{myBookTitle}\\\" by {myBookAuthor}\");\n```\n\n## Example: Deconstruction of a record\n\n```csharp\n// define a record called \"Person\" with the props\npublic record Person(string FirstName, string LastName);\n```\n```csharp\n// instantitate a Person record\nvar person = new Person(\"Jon\", \"Doe\");\nvar (firstName, lastName) = person;\nConsole.WriteLine($\"My name is {firstName} {lastName}\");\n```\n\n## Example: Deconstruction using extension method \n\n```csharp\n// implemeted an extension method\npublic static void Deconstruct(this DateTimeOffset date, out int day, out int month, out int year) =\u003e\n            (day, month, year) = (date.Day, date.Month, date.Year);\n```\n\n```csharp\n // now instantitate DateTimeOffset\nvar date = new DateTimeOffset(2022, 9, 17, 0, 0, 0, 0, TimeSpan.Zero);\n\n// deconstruct DateTimeOffset objcet\n(int day, int month, int year) = date; \nConsole.WriteLine($\"I wrote this example on: {month}/{day}/{year}\");\n```\n\n---\n## Links:\n- [Deconstructing tuples and other types](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/deconstruct)\n- [More on C# Record Types](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/records)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliteobject%2Fcsharp-deconstruct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliteobject%2Fcsharp-deconstruct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliteobject%2Fcsharp-deconstruct/lists"}