Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can static classes not implement interfaces?
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/259026/why-doesnt-c-allow-static-methods-to-implement-an-interface">Why Doesn&rsquo;t C# Allow Static Methods to Implement an Interface?</a> </p> </blockquote> <p>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</p> <pre><code>SqlRepository.GetTheThingById(5); </code></pre> <p>without it having to be regenerated all the time. Because I want my repositories to be interchangeable, I wnat them to implement a common interface: IRepository. But when I try to do so, I get </p> <pre><code>"Static classes cannot implement interfaces" </code></pre> <p>Why can't they? How do you suggest I change my design then? Is there a pattern I could use?</p> <p><strong>UPDATE</strong><br> 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. </p> <p>I wasn't really asking what the C# specification of an interface is, rather why it was deliberately restricting me in this specific way. </p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload