{"id":18482967,"url":"https://github.com/denismurphy/emulating-associated-value-enums-csharp","last_synced_at":"2025-08-08T05:33:48.974Z","repository":{"id":190055948,"uuid":"600132983","full_name":"denismurphy/emulating-associated-value-enums-csharp","owner":"denismurphy","description":"Emulating Associated Value Enums in C#","archived":false,"fork":false,"pushed_at":"2024-08-18T16:59:15.000Z","size":18,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-03T11:56:32.195Z","etag":null,"topics":["associated-values","csharp","default-method","dotnet","dotnetcore","educational","enum","interface","net7","netcore","struct","swift"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/denismurphy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2023-02-10T16:54:48.000Z","updated_at":"2025-04-23T21:29:00.000Z","dependencies_parsed_at":"2023-08-23T03:31:08.469Z","dependency_job_id":"a051c37b-917b-4440-a545-f19a6ae4f448","html_url":"https://github.com/denismurphy/emulating-associated-value-enums-csharp","commit_stats":null,"previous_names":["denismurphy/emulating-associated-value-enums-csharp"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/denismurphy/emulating-associated-value-enums-csharp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denismurphy%2Femulating-associated-value-enums-csharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denismurphy%2Femulating-associated-value-enums-csharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denismurphy%2Femulating-associated-value-enums-csharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denismurphy%2Femulating-associated-value-enums-csharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/denismurphy","download_url":"https://codeload.github.com/denismurphy/emulating-associated-value-enums-csharp/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denismurphy%2Femulating-associated-value-enums-csharp/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269371267,"owners_count":24406265,"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","status":"online","status_checked_at":"2025-08-08T02:00:09.200Z","response_time":72,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["associated-values","csharp","default-method","dotnet","dotnetcore","educational","enum","interface","net7","netcore","struct","swift"],"created_at":"2024-11-06T12:32:33.635Z","updated_at":"2025-08-08T05:33:48.937Z","avatar_url":"https://github.com/denismurphy.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🎭 Emulating Associated Value Enums in C#\n\nThis repository provides an example of how to emulate Swift's Associated Value Enums in C#. It demonstrates how to create a C# interface that acts like an enum type, and uses default interface methods to provide struct implementations that store associated values. 🧠💡\n\n## 📚 Example from Swift Documentation\n\nLet's take an example from the Swift documentation:\n\n```swift\nenum Barcode {\n    case upc(Int, Int, Int, Int)\n    case qrCode(String)\n}\n```\n\nThe `Barcode` enum has two cases, `upc` and `qrCode`. The `upc` case has four associated values of type `Int`, and the `qrCode` case has one associated value of type `String`. 🏷️\n\n## 🖥️ C# Implementation\n\nThe best way to emulate this in C# is to create an interface `IBarcode` that acts like the `Barcode` enum type. Each value of the enum has a default interface method and a struct that stores the associated value. 🛠️\n\n```csharp\npublic interface IBarcode {\n    public UPC Upc(int numberSystem, int manufacturer, int product, int check)\n    {\n        return new UPC { NumberSystem = numberSystem, Manufacturer = manufacturer, Product = product, Check = check };\n    }\n\n   public QRCode QrCode(string productCode)\n    {\n        return new QRCode { ProductCode = productCode };\n    }\n}\n```\n\nIn this example, `UPC` and `QRCode` are two structs that implement the `IBarcode` interface. `UPC` has four properties that correspond to the associated values of the `upc` case in the Swift example, and `QRCode` has one property that corresponds to the associated value of the `qrCode` case. 🏗️\n\n```csharp\npublic struct UPC : IBarcode\n{\n    public int NumberSystem { get; set; }\n    public int Manufacturer { get; set; }\n    public int Product { get; set; }\n    public int Check { get; set; }\n}\n\npublic struct QRCode : IBarcode\n{\n    public string ProductCode { get; set; }\n}\n```\n\n## 🚀 Using the Emulated Enum in C#\n\nTo use the emulated enum in C#, you can create an instance of the `IBarcode` interface with the associated values of the struct. For example, to create a `Barcode.upc` instance with values `8`, `85909`, `51226`, and `3`, you would do the following:\n\n```csharp\nIBarcode barcode = new UPC { NumberSystem = 8, Manufacturer = 85909, Product = 51226, Check = 3 };\n```\n\nTo create a `Barcode.qrCode` instance with value `\"ABCDEFGHIJKLMNOP\"`, you would do the following:\n\n```csharp\nIBarcode barcode = new QRCode { ProductCode = \"ABCDEFGHIJKLMNOP\" }; \n```\n\nYou can use the emulated enum in a switch statement to perform different actions depending on the type of the associated value. For example, to print the associated values of a `Barcode.upc` instance or `Barcode.qrCode` instance, you can use the following code:\n\n```csharp\nswitch (barcode)\n{\n    case UPC upc:\n        Console.WriteLine($\"UPC: {upc.NumberSystem}, {upc.Manufacturer}, {upc.Product}, {upc.Check}.\");\n        break;\n    case QRCode qrCode:\n        Console.WriteLine($\"QR code: {qrCode.ProductCode}.\");\n        break;\n} \n```\n\n## 📜 License\nThis code is licensed under the MIT License.\n\n## 📚 References\n\n - [Swift Documentation: Enumerations - Associated\n   Values](https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html#ID148)\n - [Microsoft C# 8.0 - Default Interface\n   Methods](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenismurphy%2Femulating-associated-value-enums-csharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdenismurphy%2Femulating-associated-value-enums-csharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenismurphy%2Femulating-associated-value-enums-csharp/lists"}