假设有如下定义:
public class KeyValueItem<K,V>
{
public K Key { get; set; }
public V Value { get; set; }
public override string ToString()
{
return T.ToString(); // does not compile
}
}
如何覆盖 ToString() 并返回 V.ToString();
看起来您想要使用:
public class KeyValueItem<K,V>
{
public K Key { get; set; }
public V Value { get; set; }
public override string ToString()
{
return this.Value.ToString(); // does compile
}
}
为什么呢? 因为你没有任何T,而K/V是类型,不是类型的实例。