C#C
C#3y ago
Tim

✅ Enum and generics

Hello there!
In our current code, we use structs to represent a packet; Packet<Data>.
This packet (struct) has a ushort Identifier and a Codec<Data> Codec.

This Identifier should be unique, but currently we assign these manually:
Packet<String> SendMessage = new Packet<String>(0, CodecString.UTF8);

This isn't ideal, so I considered using an Enum to represent the packet Id's, however that would mean that the packets would be in 2 places, the enum and the Packet instance holding the codec.

Is there a way to combine this? The ideal solution would be an enum with generic <Data> to allow enforcing the codec used:
public void SendPacket<Data>(PacketEnum<Data> enumType, Data data){}

But that is obviously not supported.
Was this page helpful?