Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Concrete override of generic class

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>
 }
like image 709
daveharnett Avatar asked Jun 14 '12 13:06

daveharnett


People also ask

What is C in simple words?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Is C or C++ same?

The main difference between C and C++ is that C is a procedural programming language that does not support classes and objects. On the other hand, C++ is an extension of C programming with object-oriented programming (OOP) support.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

public class BarRepository : Repository<Bar>
{ 
    RepositoryInstructionResult Add(Bar item) 
    { //different implementation to Repository<T>.Add() } 
    //other methods inherit from Repository<T> 
} 
like image 139
roken Avatar answered Oct 04 '22 03:10

roken