-1

There s a class from a library that doesn't implement IComparable.

I want to implement it but the problem is I can't modify the class because it s in a library.

This class is used a lot in my project so if possible I don't want to have to create my own inherited version which implements IComparable.

Fred
  • 677
  • 2
  • 7
  • 21
  • 3
    This feels like a XY Problem - https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem . Why do you think you need to do this? – mjwills Feb 13 '19 at 01:59
  • Possible duplicate of [When to use IComparable Vs. IComparer](https://stackoverflow.com/questions/538096/when-to-use-icomparablet-vs-icomparert) – mjwills Feb 13 '19 at 02:00
  • 1
    You could look at using the [Adapter Pattern](https://en.wikipedia.org/wiki/Adapter_pattern). – Enigmativity Feb 13 '19 at 02:30

1 Answers1

2

I can't modify the class because it s in a library

You answered your own question right there.

create my own inherited version which implements IComparable

This is the solution. There's no way around it. You don't have the code for the class to modify at design-time, and you can't modify classes at runtime (even if you could at runtime, the compiler wouldn't know what you're doing, so you'd have to do weird things to get around compiler errors).

But you can inherit from them (as long as they weren't declared sealed).

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84