0

Ok , to simplify we have 2 classes:A,B: both clases have a primary key, called key.

public class A{
String key;
}

public class B{
int key;
}

So we have an interface to manage all entities i called it IRepository, and for this example has only 1 operation:

public interface IRepository<T,TKey> where T: class wher Y:class {
    TKey getKey(T entity);
}

So when i implement this class and finally resolving the generics, in A case good:

public class RepositoryA<A,String> : IRepository<T, TKey>{
 public String getKey(A entity)=> A.key;
}

But in B case obviously not, because int is not an object, so i did a class int wrapper:

public class RepositoryB<B,IntWrapper> : IRepository<B, IntWrapper>{
public String getKey(B entity)=> B.key;
}

Int wrapper just a container of the int value.

 public class IntWrapper{
       int value;
    }

So the question is: can i indicate something like this???

public interface IRepository<T,TKey> where T: class where TKey:class|nonclassvalue 

Or what is the correct way to do what im doing without tricking IntWrapper?

related with this questions:

c# where for generic type constraint class may NOT be

c# generic constraint where is not class?

hesolar
  • 543
  • 4
  • 23
  • 7
    Just remove the `where TKey: class` constraint from your `IRepository` definition? – Heinzi Mar 23 '22 at 09:38
  • Perfect, answer the question with that – hesolar Mar 23 '22 at 09:40
  • 1
    + i dont think you need `IntWrapper`, was that a hack to get around `where TKey: class`? :) – Jan 'splite' K. Mar 23 '22 at 09:41
  • 3
    The code in your question is incorrect - every snippet which has generics in it has some error which means that it won't compile. Please make sure that you have run the code in your question, and made sure that it reproduces your problem – canton7 Mar 23 '22 at 09:42

3 Answers3

2

So the question is: can i indicate something like this???

public interface IRepository<T,TKey> where T: class where TKey:class|nonclassvalue

Sure, just remove the constraint on TKey altogether:

public interface IRepository<T,TKey> where T: class
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Same answer ... +1 for you ;) ... you answered first (in comments). @Author: Accept this :D! – Martin Mar 23 '22 at 09:46
  • 1
    Note that the original `class` constraint and `nonclassvalue` (which I think is OP's way of saying `struct`?) disallows nullable types. So perhaps `TKey: notnull` is a better constraint? – Sweeper Mar 23 '22 at 09:50
1

Just use it without the TKey constraint

public interface IRepository<T,TKey> 
where T: class

Without the TKey:class|nonclassvalue

Martin
  • 3,096
  • 1
  • 26
  • 46
1
public interface IRepository<T, TKey>
{
    TKey getKey(T entity);
}

public class RepositoryA: IRepository<A, string>
{
    public string getKey(A entity) {
      return entity.key;
    }
}

public class RepositoryB: IRepository<B, int>{
  public int getKey(B entity)=> entity.key;
}

Is this what you are looking for?

Wei QIn
  • 75
  • 1
  • 9