我看到许多方法将接口(interface)指定为返回值。我的想法是否真的意味着:我的方法可以返回从该接口(interface)继承的每个类类型?如果不是,请给我一个好的答案。
最佳答案
是的,您的方法可以返回实现该接口(interface)的任何类型。
这是一个例子:
using System;
class Foo
{
public IComparable GetComparable()
{
// Either of these return statements
// would be valid since both System.Int32
return 4;
// and System.String
return "4";
// implement System.IComparable
}
}
关于c# - 如果一个方法返回一个接口(interface),这意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2380057/