c# Concrete override of generic class

daveharnett picture daveharnett · Jun 14, 2012 · Viewed 7.7k times · Source

Here's the generic class I'm working with:

 public interface IRepository<T> where T : EntityObject
{
    RepositoryInstructionResult Add(T item);
    RepositoryInstructionResult Update(T item);
    RepositoryInstructionResult Delete(T item);
}
public class Repository<T> : IRepository<T> where T : EntityObject
{

    RepositoryInstructionResult Add(T item)
    { //implementation}
    RepositoryInstructionResult Update(T item);
    { //implementation}
    RepositoryInstructionResult Delete(T item);
    { //implementation}
 }

Now I'm looking to occasionally alter the behavior of the methods when t : a specific type. Is something like the following possible? This particular attempt gives an error (Error 5: Partial declarations of 'Repository' must have the same type parameter names in the same order).

public class Repository<Bar> //where Bar : EntityObject
{
    RepositoryInstructionResult Add(Bar item)
    { //different implementation to Repository<T>.Add() }
    //other methods inherit from Repository<T>
 }

Answer

roken picture roken · Jun 14, 2012
public class BarRepository : Repository<Bar>
{ 
    RepositoryInstructionResult Add(Bar item) 
    { //different implementation to Repository<T>.Add() } 
    //other methods inherit from Repository<T> 
}