Is it possible in C# to have a class that implement an interface that has 10 methods declared but implementing only 5 methods i.e defining only 5 methods of that interface??? Actually I have an interface that is implemented by 3 class and not all the methods are used by all the class so if I could exclude any method???
I have a need for this. It might sound as a bad design but it’s not hopefully.
The thing is I have a collection of User Controls that needs to have common property and based on that only I am displaying them at run time. As it’s dynamic I need to manage them for that I’m having Properties. Some Properties are needed by few class and not by all. And as the control increases this Properties might be increasing so as needed by one control I need to have in all without any use. just the dummy methods. For the same I thought if there is a way to avoid those methods in rest of the class it would be great. It sounds that there is no way other than having either the abstract class or dummy functions 🙁
You can make it an abstract class and add the methods you don’t want to implement as abstract methods.
In other words:
If these classes all need to be concrete, not abstract, then you’ll have to throw a
NotImplementedException/NotSupportedExceptionfrom inside the methods. But a much better idea would be to split up the interface so that implementing classes don’t have to do this.Keep in mind that classes can implement multiple interfaces, so if some classes have some of the functionality but not all, then you want to have more granular interfaces:
This is probably the design you really should have.