I’m making a server library in which the packet association is done by enum.
public enum ServerOperationCode : byte
{
LoginResponse = 0x00,
SelectionResponse = 0x01,
BlahBlahResponse = 0x02
}
public enum ClientOperationCode : byte
{
LoginRequest = 0x00,
SelectionRequest = 0x01,
BlahBlahRequest = 0x02
}
That works fine when you’re working in your own project – you can compare which enum member is returned (i.e. if (packet.OperationCode == ClientOperationCode.LoginRequest)). However, since this is a class library, the user will have to define its own enum.
Therefore, I have two enums to add as “abstract” – ServerOperationCode and ClientOperationCode. I know it’s not possible to implement abstract enums in C#. How would I go doing this?
I like to use static instances on my classes when I need to do this. It allows you to have some default values but also lets it be extensible through the usual means of inheritance and interface implementations:
assuming
packet.OperationCodereturn a byte, you will likely have to implement an == operator for byte. put this code into your abstract OperationCode class.this will allow you to have the same check as you showed: