Search code examples
c#extension-methodspartial-classes

Conversion Class Extensiblity (Extension Methods vs Parital Class)


I am creating a conversion class for frequency, and I wanted to allow my team to be able to add additional conversions when they are needed.

Frequency myFrequency = new Frequency(100, MHz);
double value = myFrequency.InKhz();

The source code for the class will not be include in future projects, so I will either have to have the class be a partial class or the additional conversions will need to be extensions. An example would be adding a conversion to GHz

myFrequency.InGHz()

Which is the best way to proceed with this?

Update: After reading Randolpho's answer I am going with the extension method approach. As time allows the extensions will get rolled into the base code, but I didn't want to make the other team members wait for updated assemblies and this allows them to move on to the next task a little faster.


Solution

  • Partial classes will not work unless you have the original source. If you are denying the developers access to that source, their only alternative is to use extension methods.

    That said, you probably just want to have a process to update the library with new additions; it looks like they'll be rare.