C
C#13mo 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 });
}
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?)
6 Replies
Angius
Angius13mo ago
Not sure what you mean by "eliminate the redundancy of a class" C# is object-oriented It will use classes whether you like it or not (also, interfaces should start with capital I)
anita
anita13mo ago
Maybe you are looking for default interface methods
anita
anita13mo ago
Default interface methods - C# 8.0 draft feature specifications
This feature specification describe the syntax updates necessary to support default interface methods. This includes declaring bodies in interface declarations, and supporting modifiers on declarations.
Tim
TimOP13mo ago
eh I guess sometimes I lean more towards the functional approach but it's fine just dislike the boilerplate mhehe I don't Java is my main language so I don't wanna learn myself this eh I cleaned up the classes a bit with the methods with => and it looks fine now I guess
Angius
Angius13mo ago
Well, you're using C# not Java So use C# conventions
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server