Please explain the utility of abstract methods in C#

.net, c#

Solution

public abstract class MyBaseController {
    public void Authenticate() { var r = GetRepository(); }
    public abstract void GetRepository();
}
public class ApplicationSpecificController {
    public override void GetRepository() { /*get the specific repo here*/ }
}

This is just some dummy code that represents some real world code I have (for brevity this is just sample code)

I have 2 ASP MVC apps that do fairly similar things. Security / Session logic (along with other things) happens the same in both. I've abstracted the base functionality from both into a new library that they both inherit. When the base class needs things that can only be obtained from the actual implementation I implement these as abstract methods. So in my above example I need to pull user information from a DB to perform authentication in the base library. To get the correct DB for the application I have an abstract `GetRepository` method that returns the repository for the application. From here the base can call some method on the repo to get user information and continue on with validation, or whatever.

When a change needs to be made to authentication I now only need to update one lib instead of duplicating efforts in both. So in short if you want to implement some functionality but not all then an abstract class works great. If you want to implement no functionality use an interface.

Problem

Just the 5 minute overview would be nice....

Original source