Question about C# 4.0's generics covariance

Posted by devoured elysium on Stack Overflow See other posts from Stack Overflow or by devoured elysium
Published on 2010-04-28T06:25:26Z Indexed on 2010/04/28 6:33 UTC
Read the original article Hit count: 162

Filed under:
|
|
|

Having defined this interface:

public interface IInputBoxService<out T> {
    bool ShowDialog();
    T Result { get; }
}

Why does the following code work:

public class StringInputBoxService : IInputBoxService<string> {
    ...
}

...

IInputBoxService<object> service = new StringInputBoxService();

and this doesn't?:

public class IntegerInputBoxService : IInputBoxService<int> {
    ...
}

...

IInputBoxService<object> service = new IntegerInputBoxService();

Does it have anything to do with int being a value type? If yes, how can I circumvent this situation?

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET