Manoj
Manoj

Reputation: 437

Type parameter declaration must be an identifier

interface IRepository<TEntity,Tid> where TEntity :class 
{
    int Insert(TEntity entity);
    int Delete(TEntity entity);
    TEntity GetById(Tid id);
    TEntity GetByFilters(TEntity entity);
    int Update(TEntity entity);        
}

Trying to implement

internal class Repository<XYZClass, string> : IRepository<XYZCLass, string> 
{    
    //...All interface methods    
}

Getting below error:

type parameter declaration must be an identifier not a type

Any Suggestions..

Upvotes: 0

Views: 908

Answers (4)

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 61912

Maybe you want a base class constraint?

internal class Repository<TYourEntity> : IRepository<TYourEntity, string>
    where TYourEntity : XyzClass
{    
    //...All interface methods    
}

Note that this constraint means that the constraint on the interface (the reference type constraint TEntity : class) is satisfied.


For completeness, the other "legal" interpretations of what you meant (also in other answers already), are:

// don't constrain generic parameter, just name it TXyz (TXyz is "declared" here)

internal class Repository<TXyz> : IRepository<TXyz, string>
    where TXyz : class
{    
    //...All interface methods    
}

and:

// make class non-generic (the type XyzClass exists and is declared elsewhere)

internal class Repository : IRepository<XyzClass, string>
{    
    //...All interface methods    
}

Upvotes: 1

Lee
Lee

Reputation: 144126

If you want Repository to be non-generic you need to specify both types:

internal class Repository : IRepository<XYZClass, string> { ... }

If you want Repository to be generic but specify that TId is a string you can use

internal class Repository<TEntity> : IRepository<TEntity, string> where TEntity : class
{    
    //...All interface methods    
}

string is a type, not a generic type parameter.

Upvotes: 1

Jon Egerton
Jon Egerton

Reputation: 41539

The declaration should only include type parameters for those that are generic to those consuming the class.

In other words, if you implementation of IRepository sets the TEntity parameter to XYZClass and the TID parameter to string (ie a concrete class definition), then you should have:

internal class Repository : IRepository<XYZCLass, string> 
{    
    //...All interface methods    
}

If you wish to define one type and leave the other generic then:

internal class Repository<TEntity> : IRepository<TEntity, string> 
{    
    //...All interface methods    
}

Otherwise, both types should still be left open:

internal class Repository<TEntity,TID> : IRepository<TEntity, TID> 
{    
    //...All interface methods    
}

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

Thus repository is not generic (it is implementation of repository interface parametrized with concrete classes):

internal class Repository : IRepository<XYZCLass, string>

Upvotes: 0

Related Questions