chris
chris

Reputation: 37460

C#: Generic interfaces and understanding "type parameter declaration must be an identifier not a type"

I'm trying to understand the generic interface as described in this

My example has an interface:

  public interface ITest<T> where T: class
  {
    T GetByID(int id);
  }

I have a class that implements the interface, using LINQ to enties in project Data, which contains the class myClass:

  public class Test<myClass> :  ITest<myClass> where myClass : class
  {
      Data.myEntities _db = new Data.myEntities();

      public myClass GetByID(int id)
      {
        var item = _db.myClass.First(m => m.ID == id);
        return item;
      }

  }

This produces an error saying "Cannot implicitly convert type 'Data.myClass' to 'myClass', but if I change public class Test<myClass> to public class Test<Data.myClass> I get the "Type parameter declaration must be an identifier not a type".

I'm obviously missing something, because I don't understand what's going on here. Can anyone explain it, or point to somewhere that might explain it better?

Thanks.

Upvotes: 1

Views: 4643

Answers (2)

Brian
Brian

Reputation: 118865

I think you want to just remove the generic parameter from the Test class.

... class Test : ITest<myClass> ...

as it stands now, the generic parameter name is shadowing the actual type name.

Upvotes: 2

n8wrl
n8wrl

Reputation: 19765

I suspect the problem is here:

 _db.myClass.First...

Is it possible you mean something like

_db.GetAll<myClass>().First...

I think you are confusing myClass as a type vs. myClass as a function _db implements?

Upvotes: 0

Related Questions