Search code examples
c#abstract-classabstract-methods

Return type on abstract method


Not really dealt with abstract methods that much but am looking at an abstract method inside an abstract class.

    protected abstract bool Validate()
    {
    }

When I create the above class I get an error that tells me I need to specify a return type as per a normal method. Is this correct or am I doing something wrong?


Solution

  • If you declaring the abstract method then you should not give body

    protected abstract bool Validate();
    

    If it is not abstract method declaration but you giving implementation of an abstract method then you should return bool using return statement from method method to satisfy the return type in declartion.

    protected abstract bool Validate()
    {
         //The method code 
         return false;
    }
    

    An abstract method declaration introduces a new virtual method but does not provide an implementation of that method. Instead, non-abstract derived classes are required to provide their own implementation by overriding that method. Because an abstract method provides no actual implementation, the method-body of an abstract method simply consists of a semicolon, MSDN.