© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
41 replies
Tim

✅ Enum and generics

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

This
Identifier
Identifier
should be unique, but currently we assign these manually:
Packet<String> SendMessage = new Packet<String>(0, CodecString.UTF8);
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
Packet
instance holding the codec.

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

But that is obviously not supported.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Reflections and generics
C#CC# / help
4y ago
❔ Generics and nullable types
C#CC# / help
3y ago
Generics and type inference
C#CC# / help
4y ago
❔ Generics
C#CC# / help
3y ago