2

I have

public interface IPersistedModel<TModel, TIdentifier>

I'd like to define an interface where I have a generic method that uses the generic type parameters from the above. Is there a way to do this? I've tried the following but neither compiles.

public interface IModelCollection1<IPersistedModel<TModel, TIdentifier>>
{
    IEnumerable<TModel> ReadCollection(TIdentifier identifier);
}

or

public interface IModelCollection2<T> where T : IPersistedModel<TModel, TIdentifier>
{
    IEnumerable<TModel> ReadCollection(TIdentifier identifier);
}

3 Answers 3

3

Unfortunately, you need to include the generic types in the interface itself:

public interface IModelCollection<T, TModel, TIdentifier> 
       where T : IPersistedModel<TModel, TIdentifier>
{
    IEnumerable<TModel> ReadCollection(TIdentifier identifier);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply use a generic method which adds generic parameters only to that method

class A
{

static void Swap<T>(ref T lhs, ref T rhs)
{
    T temp;
    temp = lhs;
    lhs = rhs;
    rhs = temp;
}

}

Comments

0

Try this:

public interface Ione <T, U> where T : IInterfaceOne<T> where U : IInterfaceTwo<U>
{

}

public interface IInterfaceOne<T>
{

}

public interface IInterfaceTwo<U>
{

}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.