admin管理员组

文章数量:1190353

I dont know what for it is "in" TCommand keyword in that interface where TCommand is a class with a few properies needed for handler. Is it needed? What it gives in that context ? or maybe "in" is only explicit way to say what is implicit mechanism in generics ?

public interface ICommandHandler<in TCommand> where TCommand : class, ICommand
{
    Task HandleAsync(TCommand command);
}

Comment So ..it is related only for that specificCommandHandler = handler; where SpecificHandler variable can be assigned BaseHandler type ?

Additionally SpecificHandler can be used with a ... BaseCommand ?? like

specificHandler<SpecificCommand> sHandler = new SpecificHandler();
sHandler(BaseCommand) ;

?? if yes ... tell me what for ?:)

I dont know what for it is "in" TCommand keyword in that interface where TCommand is a class with a few properies needed for handler. Is it needed? What it gives in that context ? or maybe "in" is only explicit way to say what is implicit mechanism in generics ?

public interface ICommandHandler<in TCommand> where TCommand : class, ICommand
{
    Task HandleAsync(TCommand command);
}

Comment So ..it is related only for that specificCommandHandler = handler; where SpecificHandler variable can be assigned BaseHandler type ?

Additionally SpecificHandler can be used with a ... BaseCommand ?? like

specificHandler<SpecificCommand> sHandler = new SpecificHandler();
sHandler(BaseCommand) ;

?? if yes ... tell me what for ?:)

Share Improve this question edited Jan 25 at 14:46 Artur asked Jan 24 at 16:38 ArturArtur 1292 silver badges11 bronze badges 1
  • Your example of specificHandler<SpecificCommand> sHandler = new SpecificHandler(); makes no sense to me - is specificHandler a variable or a type? Did you mean ICommandHandler<SpecificCommand> instead of specificHandler<SpecificCommand>? What does sHandler(BaseCommand); mean, given that BaseCommand is a type, and you haven't mentioned the HandleAsync method? A complete example would make it much easier to help you - please take the time to really make your question as easy to read as possible. – Jon Skeet Commented Jan 25 at 15:31
Add a comment  | 

1 Answer 1

Reset to default 3

Suppose you have BaseCommand and SpecificCommand, where SpecificCommand : BaseCommand. With the contravariance you've specified, you could write:

ICommandHandler<BaseCommand> handler = ...; // Whatever initialization you want
// Implicit reference conversion
ICommandHandler<SpecificCommand> specificCommandHandler = handler;

Without the contravariance, that second statement wouldn't be valid, because there wouldn't be any implicit conversion from ICommandHandler<BaseCommand> to ICommandHandler<SpecificCommand>.

Now you don't need that if all you want is to write handler.HandleAsync(new SpecificCommand()) - but if you want to pass a handler to a method expecting an ICommandHandler<SpecificCommand> then that implicit conversion is exactly what you want.

本文标签: ccontravariance in genericswhat for in that caseStack Overflow