Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# generic method with inheritance and interface

this is something really simple Im sure, but Im struggling to get my head around the inheritance malarky when it comes to interfacing.

Given the following classes, how do I interface the Get method in an interface specific to class Parent, without overriding the base method?

public class Base<T, T2>
{
    public T Get<T, T2>(string key)
    {
        ...
    }
}

public class Parent : Base<Type1, Type2>, IParent
{
    ...
}

Here's what I have atm, but I keep getting a "inteface member Type1 IParent.Get(string) is not implemented" error.

public interface IParent
{
    Type1 Get(string key);
}
like image 551
beterthanlife Avatar asked Jan 02 '13 16:01

beterthanlife


People also ask

What C is 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 ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

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.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


3 Answers

public T Get<T, T2>(string key) will create a generic method on generic class. T and T2 will be arguments of this generic method and will have no relation to class's T and T2.

Just make it public T Get(string key).

like image 78
Krizz Avatar answered Sep 16 '22 11:09

Krizz


The T Get<T,T2>(string) method of Base<T,T2> and the method Type1 Get(string) method of IParent are two different method signatures. You would need to implement both. If you wanted both implementations to use the same functionality you could do the following:

public class ParentJ : Base<Type1, Type2>, IParent {
 public Type1 Get(string key) {
    return this.Get<Type1,Type2>(key);
 }
}

However I believe that your original intent is not to parameterize the Get() method in Base<T,T2> therefore you would write Base like so:

public class Base<T,T2> {
  public T Get(string key) {
    // implementation here
  }
}

That signature would satisfy the method signature in IParent.

You only need type parameters (e.g. T and T2) on methods when the type cannot or should not be inferred by the class that contains the method.

like image 42
Charles Lambert Avatar answered Sep 17 '22 11:09

Charles Lambert


When matching methods, this signature must match exactly. One of the components of the signature is the number of generic arguments.

Your IParent interface contains a method Get with zero type arguments. Your Base class contains a method Get with two type arguments.

While it looks like Base.Get shares its type arguments, it does not, the syntax used creates two new type arguments that shadow the type arguments of the class.

The fix is to simply implement a Get method in Parent that does not have any type arguments.

like image 42
Guvante Avatar answered Sep 19 '22 11:09

Guvante