4

The code below uses a single generic parameter.

Is there a way to take multiple generic variables, where I want 2 or more classes? (eg, T1 class, T2 class, etc.)

Original generic:

public interface IGenericRepository<T> where T : class 
{
    IQueryable<T> GetAll();
    IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
    void Add(T entity);
    void Delete(T entity);
    void Edit(T entity);
    void Save();
}
1

1 Answer 1

7

Generics types can be anything, not just T - T just happens to be common.

Example:

void foo<TOne, TTwo>() 
   where TOne : BaseOne
   where TTwo : BaseTwo

More information can been seen here. Check out the "Constraining Multiple Parameters" section.

Sign up to request clarification or add additional context in comments.

2 Comments

Can I also just use Class, instead of Base1, Base2, like this below? void foo<TOne, TTwo>() where TOne : class where TTwo : class
Yes, you can just use class to restrict to class types as per the table at the start of the linked article.