1

Given the below interface declarations:

Declaration A

public interface EventHandler<T>
{
    void Handle(T command);
}

Declaration B

public interface EventHandler<in T>
{
    void Handle(T @event);
}

In normal testing these all do the same thing. The Handle method is called as expected.

In what ways do the above vary, and how do they behave differently in other scenarios?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
morleyc
  • 2,169
  • 10
  • 48
  • 108
  • possible duplicate of [What does "out" mean before a Generic type parameter?](http://stackoverflow.com/questions/10027611/what-does-out-mean-before-a-generic-type-parameter) – gdoron Apr 20 '13 at 21:37
  • 1
    Regarding to `@`, it lets you use saved-keywords, as in the last example, `event` is a keyword and `var event = 2` won't compile, prefixing it with `@` will fix it. – gdoron Apr 20 '13 at 21:38
  • 1
    Do the first two really work for you? I expected a compiler error for those, and I do get a compiler error for them. –  Apr 20 '13 at 21:40
  • @hvd, `int @event = 2;` should work just fine. – gdoron Apr 20 '13 at 22:24
  • @gdoron I know, my question was to the OP, not to you. Sorry for not being clear about that. –  Apr 20 '13 at 22:25
  • Apologies edited as blatant silly typo you pointed out @hvd. All comments and answer helped i had gotten confused with the dynamic @ combination – morleyc Apr 20 '13 at 22:35

1 Answers1

11

in specifies a generic type parameter as a contravariant: in (Generic Modifier) (C# Reference). There is also out for covariant.

@ allows you use registered keywords as identifiers:

Keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @if is a valid identifier but if is not because if is a keyword.

Source: C# Keywords

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263