【发布时间】:2010-04-28 06:25:26
【问题描述】:
已经定义了这个接口:
public interface IInputBoxService<out T> {
bool ShowDialog();
T Result { get; }
}
为什么下面的代码有效:
public class StringInputBoxService : IInputBoxService<string> {
...
}
...
IInputBoxService<object> service = new StringInputBoxService();
这不是吗?:
public class IntegerInputBoxService : IInputBoxService<int> {
...
}
...
IInputBoxService<object> service = new IntegerInputBoxService();
这与 int 是值类型有什么关系吗?如果是,我该如何规避这种情况?
谢谢
【问题讨论】:
-
不,但据我了解,它会导致相同的结果。 int 只是 Int32 的别名。
标签: c# .net generics covariance