C#C
C#3y ago
Tim

❔ Static interface methods

I have several classes implementing my Codec interface like this:
public sealed class CodecByte : Codec<byte>
{
    public byte Decode(EncodedData data) => data.Buffer[0];

    public EncodedData Encode(byte data) => new EncodedData(new byte[] { data });
}

Is there a better way to represent multiple actions (Encode and Decode) without using a class?

  • If there would have been a single method, I would start using a delegate. But a delegate can't do multiple things
  • This is essentially just logic, so having this in a class is a bit useless (and makes me want to turn this into a singleton because instances aren't necessary)
So basically looking to eliminate the redundancy of a class (because I only need 1 instance) here while having those 2 functions combined (with a delegate somehow?)
Was this page helpful?