Is there a way to force a C# class to implement certain static functions?

No, there is no language support for this in C#. There are two workarounds that I can think of immediately:

  • use reflection at runtime; crossed fingers and hope…
  • use a singleton / default-instance / similar to implement an interface that declares the methods

(update)

Actually, as long as you have unit-testing, the first option isn’t actually as bad as you might think if (like me) you come from a strict “static typing” background. The fact is; it works fine in dynamic languages. And indeed, this is exactly how my generic operators code works – it hopes you have the static operators. At runtime, if you don’t, it will laugh at you in a suitably mocking tone… but it can’t check at compile-time.

Leave a Comment