1

I'm confused as to how generics, namely T works in C#. It doesn't seem to behave the same way as C++, unless I'm mistaken. I'm trying to create a simple interface for a data gateway:

EDIT Updated to match the answers. Visual Studio now complains the derived classes don't implement the interface methods.

public interface IDataGateway<T>
{
    void InsertRow(T row);
    void UpdateRow(T row);
    IEnumerable<T> GetTable();
}

public class LibraryGateway : IDataGateway<Media>
{
    public void InsertRow(Media item) { }
    public void Updaterow(Media item) { }
    public IEnumerable<Media> GetTable() { }
}

I do not know what type will be passed, which is why I'm trying to use generics.

8
  • try public class LibraryGateway : IDataGateway<Media> Apr 23, 2013 at 18:48
  • VS complains about using non-generic type IDataGateway with type arguments.
    – Lee
    Apr 23, 2013 at 18:49
  • Are you trying to create an interface that do not know the type that the implementor uses or to you want to implement with generic typing? Apr 23, 2013 at 18:52
  • Yes, reading all the answers I must have confused the scope to make generic. The interface being generic seems logical!
    – Lee
    Apr 23, 2013 at 18:53
  • 2
    I think there are two possible reasons: First, you have "Updaterow" with a lower-case r in your class implementation -- the case mismatch might be what's throwing it off. And second, like I said in my answer, I think the definition of your GetTable() method is a little off -- you have GetTable(T table) in your interface, but judging from the implementation in your class, it looks like you don't actually want it to accept any parameters. Apr 23, 2013 at 19:01

7 Answers 7

2

You are declaring the type argument on each method individually. You should declare it on the interface type itself.

public interface IDataGateway<T>
{
    void InsertRow(T row);
    void UpdateRow(T row);
    IEnumerable<T> GetTable(T table);
}

public class LibraryGateway : IDataGateway<Media>
{
    public void InsertRow(Media item) { }
    public void Updaterow(Media item) { }
    public IEnumerable<Media> GetTable() { }
}

To clarify, your current implementation could be rewritten as:

public interface IDataGateway
{
    void InsertRow<T>(T row);
    void UpdateRow<U>(U row);
    IEnumerable<V> GetTable(V table);
}

That is, there is no requirement for T to be the same as U and V. Each method is genericized by its own type parameter, but the containing interface type itself is not generic.

By declaring the type argument on the type itself, you restrict the type arguments of the methods to be identical when implemented by a subclass. In C++ terms, which you seem to be familiar with, your current implementation is analogous to three separate template functions whereas my implementation is more similar to a template class.

2
  • Great explanation thank you. You're right, in C++ I focused more on template methods.
    – Lee
    Apr 23, 2013 at 18:55
  • I don't see why would you need to split the generic parameter types. You would be inserting one type of row but updating another? Apr 23, 2013 at 18:57
1

It looks like you are trying to create and implement a generic interface, not generic methods.

public interface IDataGateway<T>
{
    void InsertRow(T row);
    void UpdateRow(T row);
    IEnumerable<T> GetTable(T table);
}

public class LibraryGateway : IDataGateway<Media>
{
    public void InsertRow(Media item) { }
    public void Updaterow(Media item) { }
    public IEnumerable<Media> GetTable(Media table) { }
}

When you have a generic method (as in the original question), it means a single implementation (e.g. LibraryGateway.InsertRow) could get called with an argument of any type - known only at runtime. However, when you implement a generic interface, you can parameterize the interface so LibraryGateway.InsertRow operates specifically on items of type Media. Other implementations of IDataGateway<T> may operate on other types, but LibraryGateway only operates on Media objects.

1

From your examples, I think what would work best is making the interface itself generic, as opposed to the methods within it:

public interface IDataGateway<T>
{
  void InsertRow(T row);
  void UpdateRow(T row);
  IEnumerable<T> GetTable(T table); // ???
}

I think your GetTable() method also needs fixing -- if T is the type of the object contained in the table, then it (probably!) isn't also the type of the table. Maybe you just mean IEnumerable<T> GetTable();?

Then you could do:

public class LibraryGateway : IDataGateway<Media>
{
  public void InsertRow(Media item) { }
  public void UpdateRow(Media item) { }
  public IEnumerable<Media> GetTable() { }
}

...and I think that should do what you're looking for.

0

First of all, you have to make your interface generic:

public interface IDataGateway<T>

and then specify T while implementing it:

public class LibraryGateway : IDataGateway<Media>
0

You are defining a non-generic interface with three generic methods.

Each of these methods has the ability to accept any type T, as the value of T is not the same between them.

What you probably meant to do was define a generic interface with three non-generic methods.

public interface IDataGateway<T>
{
    void InsertRow(T row);
    void UpdateRow(T row);
    IEnumerable<T> GetTable(T table);
}
0

Like this perhaps?

public interface IDataGateway<T>
{
    void InsertRow(T row);
    void UpdateRow(T row);
    IEnumerable<T> GetTable(T table);
}

public abstract class LibraryGateway<T>
{
    public void InsertRow(T item) { }
    public void Updaterow(T item) { }
    public IEnumerable<T> GetTable<T>() { }
}

public class MediaLibraryGateway : GateWay<Media>, IDataGateway<Media>
{

}
0

You miss spelled Updaterow, it should read UpdateRow.

Easiets way to implement interface is to, click on interface name with mouse, hit ctrl+. and then select implement interface

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.