c# Generics "in" keyword -


i've been assigned maintenance work on existing application. i've come across following code:

public interface ientityservice<t, in tkey> {     t getentitybyid(tkey id);         ienumerable<t> getall();         void update(t entity);         void delete(tkey key); } 

i'm not sure in keyword second generic argument, tkey.

i came across following msdn article (should) explain me perfectly:
in (generic modifier) (c# reference)

however, don't truly understand it. here's says:

for generic type parameters, in keyword specifies type parameter contravariant. can use in keyword in generic interfaces , delegates.

contravariance enables use less derived type specified generic parameter. allows implicit conversion of classes implement variant interfaces , implicit conversion of delegate types. covariance , contravariance in generic type parameters supported reference types, not supported value types.

a type can declared contravariant in generic interface or delegate if used type of method arguments , not used method return type. ref , out parameters cannot variant.

an interface has contravariant type parameter allows methods accept arguments of less derived types specified interface type parameter. example, because in .net framework 4, in icomparer interface, type t contravariant, can assign object of icomparer(of person) type object of icomparer(of employee) type without using special conversion methods if employee inherits person.

a contravariant delegate can assigned delegate of same type, less derived generic type parameter.

i suppose makes sense, particularly citing

contravariance enables use less derived type specified generic parameter.

how can of use int? there "less derived type" ever pass in?

i note reference of int in last line of question. sure ientityservice<> used int keys? built compound key (primary keys composed of multiple columns)

now, in nhibernate example, compound keys use whole class represent them, have table mytable a

class mytablekey  {      public int code;     public int subcode; } 

if have secondary table musubtable connected table have

class mysubtablekey : mytablekey  {     public int subsubcode;  } 

where mysubtable table has primary key full primary key of mytable (code + subcode) plus field (subsubcode).


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -