The msdn docs (http://msdn.microsoft.com/en-us/library/tfakywbh.aspx) report the syntax for the Comparison delgate with what looks like a keyword ‘in’.
public delegate int Comparison<in T>(
T x,
T y
)
Does the ‘in’ have any actual meaning? Are there other keywords that may appear there?
inmeans the generic parameter is contravariant. This means, in this case, that you can assign aComparison<Base>to aComparison<Derived>.You can do this, because a
Comparison<Derived>variable can accept a method which takesBasetype parameters. When you call aComparison<Derived>, you need to passDerivedvariables, which happen to be valid parameters to a method acceptingBaseparameters. This means that it makes sense to assign aComparison<Base>to aComparison<Derived>.The opposite of
inis (naturally)out. This means the parameter is covariant, and can assign aDerivedgeneric to aBasegeneric. This would be used, for example, in specifying the return type of a delegate.A handy way of remembering which is which:
inshould only be used for types that are only passed in.outshould only be used for types that are only passed out.Read more here:
in (Generic Modifier) (C# Reference)
Covariance and Contravariance (C# and Visual Basic)