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();
}
CarenRose
  • 1,266
  • 1
  • 12
  • 24
  • Possible duplicate of [Generic method with multiple constraints](https://stackoverflow.com/questions/588643/generic-method-with-multiple-constraints) – Steve May 28 '18 at 23:56

1 Answers1

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.

Max von Hippel
  • 2,856
  • 3
  • 29
  • 46
acandylevey
  • 315
  • 2
  • 14
  • Can I also just use Class, instead of Base1, Base2, like this below? void foo() where TOne : class where TTwo : class –  May 29 '18 at 00:08
  • Yes, you can just use class to restrict to class types as per the table at the start of the linked article. – acandylevey May 30 '18 at 01:06