> With Java I could now do something like the following somewhere in my program:
>
> ICommand<?> command = new AuthenticationCommand();
> IResult result = command.Process();
>
> But C# does not know wildcards. Is there a work-around without using casts?
In C#3:
var command = new AuthenticationCommand();
IResult result = command.Process();
Which would assign the type "AuthenticationCommand" to command -- not
exactly wildcarding but syntactically similar in this case. In C#2 you
have lost with your exact code.
You can use the explicit type in the declaration of command:
ICommand<AuthenticationResult> command = new AuthenticationCommand();
You might consider making ICommand unparametrised:
interface ICommand { IResult Process(); }
if you really need to uniformly treat results.
An alternative approach, which I don't really like too much is:
interface IResult { }
interface ICommand { IResult Process(); }
interface ICommand<T>: ICommand where T: IResult { new T Process(); }
class Foo: IResult {}
class X : ICommand<Foo> {
IResult ICommand.Process() { return Process(); }
public Foo Process() { return new Foo(); }
}
class Program
{
static void Main(string[] args)
{
ICommand command = new X();
IResult r = command.Process();
}
}

Signature
Helge Jensen
mailto:helge.jensen@slog.dk
sip:helge.jensen@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-