109

Possible Duplicate:
Why Doesn’t C# Allow Static Methods to Implement an Interface?

In my application I want to use a Repository that will do the raw data access (TestRepository, SqlRepository, FlatFileRepository etc). Because such a repository would be used throughout the runtime of my application it seemed like a sensible thing to me to make it a static class so I could go

SqlRepository.GetTheThingById(5);

without it having to be regenerated all the time. Because I want my repositories to be interchangeable, I want them to implement a common interface: IRepository. But when I try to do so, I get:

Static classes cannot implement interfaces

Why can't they? How do you suggest I change my design then? Is there a pattern I could use?

UPDATE
Five years later: this question is visited 20k+ times, I learned about the disadvantages of the repository pattern, learned about IoC and realise my question was poorly formulated.

I wasn't really asking what the C# specification of an interface is, rather why it was deliberately restricting me in this specific way.

The practical answer is that the syntax for calling a method on an instance or on a type are different. But the question is closed.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
  • 1
    Duplicate of http://stackoverflow.com/questions/259026/ I think. – Michael Stum Aug 12 '09 at 14:15
  • 1
    If your post doesn't get closed as a duplicate, can you change inherit to implement? It's bugging me... – grenade Aug 12 '09 at 14:27
  • Duplicate: http://stackoverflow.com/questions/3957424/static-method-cannot-implement-interface-method-why – Marcel Apr 18 '13 at 10:21
  • 1
    @MichaelStum It's not a duplicate of any of those. Those are about static *methods* implementing methods specified by an interface implemented by the class those methods are a member of. This question is about static *classes* implementing interfaces. – DCShannon Dec 30 '14 at 04:22
  • I wrote an answer for the linked duplicate, but since it was actually an answer for this question it didn't make sense for that one, and I can't post it here because this is wrongly closed. I'd rather not have to open an *actual* duplicate. – DCShannon Dec 30 '14 at 04:24
  • Why do you despise the repository pattern? That and DI/IOC are not mutually exclusive. Repositories don't need to be static or singletons. They just provide a simple and consistent interface for getting and persisting objects that, lower down, need more complicated and granular operations to get and persist. – bubbleking Sep 07 '16 at 19:55
  • @bubbleking they are great for projects where you don't have to go much further then the things the average ORM gives you out of the box. Otherwise extended, complicated domains lead to monster objects or interdependent smaller repositories. That's all I can say in a comment box :) – Boris Callens Jan 30 '17 at 22:15
  • @BorisCallens Can you please refer me to a good material on the disadvantages of the repository pattern you have mentioned? – Marc.2377 Feb 21 '19 at 20:09
  • A good explanation is here: https://www.codeproject.com/Answers/1184339/Why-interface-cannot-have-static-methods-in-Csharp#answer3 – Rzassar Aug 30 '20 at 07:56

3 Answers3

47

Interfaces can't have static methods. A class that implements an interface needs to implement them all as instance methods. Static classes can't have instance methods. QED.

Updated in 2022: .NET 7 does support static interface methods.

Joe White
  • 94,807
  • 60
  • 220
  • 330
  • 103
    Correct but not an answer to the "why" question – Quango Aug 20 '13 at 14:54
  • 10
    Actually, it's exactly that. Each sentence logically leads to the next. Read his answer again, analyse it, let it sink in ;-) – Riegardt Steyn Nov 15 '13 at 12:47
  • 39
    @Heliac: It's not a cogent argument if the audience doesn't accept the premises. If Boris understood why that premise is true, why on Earth wouldn't he understand why static classes can't implement interfaces? – trolox Jan 28 '14 at 21:47
  • 18
    The question would then be why can't interfaces have static methods (or why can't interface methods be treated as both static and instance depending on how they're implemented)? Which basically goes back to the original question. Which tends to happen when a tautology is given as an answer. – Juan Jun 14 '14 at 12:28
  • 3
    Doesn't answer the why. Say you could implement an interface in a static class, how would you use it? You can't pass a reference to a static class so you couldn't have a bunch of methods or classes having a signature which takes the interface not the specific type. So you can't use the class for dependency injection the exact sort of thing you probably want to do with interfaces. Why C# doesn't treat a static class as a class that implicitly is a singleton so you could pass it around is another question which I can't answer. – Mike Oct 24 '17 at 15:23
  • 1
    For future readers, statements such as "Interfaces can't have static methods" and "A class that implements an interface needs to implement them all as instance methods" do not apply to C# 8 or greater. – derekbaker783 Jan 22 '22 at 12:42
  • FWIW regarding your update: Static interface methods are implemented directly on the interface. This is not at all the same thing as a static method in a class being used to implement a method from an interface. – Oliver Giesen Jul 02 '23 at 15:16
18

Maybe our experience will help. Rather than SqlRepository as a static class, we use AutoFac for injection and hide the container behind a static class. Then each entity has a static repository property:

public class Part : inheritence...
{
    public static IPartRepository Repository
    {
        get { return IoCContainer.GetInstance<IRepository<Part>>(); }
    }
    // ... more part-y stuff
}

This way we can swap out the implementation and callers always know where to get it:

Part p = Part.Repository.Get(id);

In another project there is a PartRepository registered with the container:

public class PartRepository : IPartRepository
{
    // IPartRepository implementation that talks to injected DAL
}

In yet another project we have mocks for testing, including repositories pre-loaded with known entires:

public class MockPartRepository : Dictionary<Part, int>, IPartRepository
{
    // IPartRepository implementation based on dictionary
}

...and it is registered with the container for unit testing. The SAME call gets the repository:

Part p = Part.Repository.Get(id);
n8wrl
  • 19,439
  • 4
  • 63
  • 103
  • Could you elaborate on this? Some more code? – Dennis G Nov 26 '12 at 20:16
  • 1
    @moontear: We've switched to AutoFac but the concept is the same. See edits to answer – n8wrl Nov 26 '12 at 21:02
  • 1
    Woah, wish I could upvote more. Updating after 3 years :-) We're using Spring.Net and I'm thinking of just using a singleton (for the logging framework) instead of building a full repository. Otherwise I love your solution! – Dennis G Nov 26 '12 at 22:39
14

By definition, interfaces create a contract for instances to fulfill. Since you cannot instantiate a static class, static classes cannot implement interfaces.

There is no need to have a static repository. Simply make it non-static and instantiate it when you need it.

JoshJordan
  • 12,676
  • 10
  • 53
  • 63
  • 23
    By definition, interfaces create contacts (e.g. methods that can be called). Static class has methods that can be called. Ego: static class can fulfill the contract. – Ian Boyd Sep 29 '13 at 17:35
  • 2
    That is true of generic interface definition, but this question is about C#, wherein your definition is incomplete. – JoshJordan Oct 21 '13 at 02:41