Search code examples
c#design-patternsabstract-class

Define an abstract method without specifying parameters


I am writing an abstract class with an abstract method (thus, all classes inheriting from it must implement that method). However, I do not want to specify the parameters which the method must use, as each method may take in different or no parameters. Only the name and return value should be the same.

Is there a way to do this in C#?

Thanks for any help!


Solution

  • No, and it would be pointless to do so. If you didn't declare the parameters, you wouldn't be able to call the method given only a reference to the base class. That's the point of abstract methods: to allow callers not to care about the concrete implementation, but to give them an API to use.

    If the caller needs to know the exact method signature then you've tied that caller to a concrete implementation, making the abstraction essentially useless.

    Perhaps if you could give more details, we could suggest a more appropriate approach? For example, you might be able to make the type generic:

    public class Foo<T>
    {
        public abstract void Bar(T t);
    }
    

    Concrete subtypes could either also be generic, or derive from Foo<string> for example.