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);
}
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 ...
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? 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.
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.
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)
.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With