Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/denismurphy/emulating-associated-value-enums-csharp
Emulating Associated Value Enums in C#
https://github.com/denismurphy/emulating-associated-value-enums-csharp
associated-values csharp default-method dotnet dotnetcore educational enum interface net7 netcore struct swift
Last synced: 2 days ago
JSON representation
Emulating Associated Value Enums in C#
- Host: GitHub
- URL: https://github.com/denismurphy/emulating-associated-value-enums-csharp
- Owner: denismurphy
- License: mit
- Created: 2023-02-10T16:54:48.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-18T16:59:15.000Z (3 months ago)
- Last Synced: 2024-08-18T18:13:07.871Z (3 months ago)
- Topics: associated-values, csharp, default-method, dotnet, dotnetcore, educational, enum, interface, net7, netcore, struct, swift
- Language: C#
- Homepage:
- Size: 17.6 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🎭 Emulating Associated Value Enums in C#
This 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. 🧠💡
## 📚 Example from Swift Documentation
Let's take an example from the Swift documentation:
```swift
enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
```The `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`. 🏷️
## 🖥️ C# Implementation
The 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. 🛠️
```csharp
public interface IBarcode {
public UPC Upc(int numberSystem, int manufacturer, int product, int check)
{
return new UPC { NumberSystem = numberSystem, Manufacturer = manufacturer, Product = product, Check = check };
}public QRCode QrCode(string productCode)
{
return new QRCode { ProductCode = productCode };
}
}
```In 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. 🏗️
```csharp
public struct UPC : IBarcode
{
public int NumberSystem { get; set; }
public int Manufacturer { get; set; }
public int Product { get; set; }
public int Check { get; set; }
}public struct QRCode : IBarcode
{
public string ProductCode { get; set; }
}
```## 🚀 Using the Emulated Enum in C#
To 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:
```csharp
IBarcode barcode = new UPC { NumberSystem = 8, Manufacturer = 85909, Product = 51226, Check = 3 };
```To create a `Barcode.qrCode` instance with value `"ABCDEFGHIJKLMNOP"`, you would do the following:
```csharp
IBarcode barcode = new QRCode { ProductCode = "ABCDEFGHIJKLMNOP" };
```You 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:
```csharp
switch (barcode)
{
case UPC upc:
Console.WriteLine($"UPC: {upc.NumberSystem}, {upc.Manufacturer}, {upc.Product}, {upc.Check}.");
break;
case QRCode qrCode:
Console.WriteLine($"QR code: {qrCode.ProductCode}.");
break;
}
```## 📜 License
This code is licensed under the MIT License.## 📚 References
- [Swift Documentation: Enumerations - Associated
Values](https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html#ID148)
- [Microsoft C# 8.0 - Default Interface
Methods](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods)