我有一本特殊的字典。我不确定如何准确地执行此操作,但我希望使 get 方法成为虚拟的,而不是 set 方法:
public TValue this[TKey key]
{
get { ... }
set { ... }
}
是否可能,如果可能,正确的组合是什么?
最佳答案
你不能直接 - 你需要添加一个单独的方法:
protected virtual TValue GetValue(TKey key) { ...}
public TValue this[TKey key]
{
get { return GetValue(key); }
set { ... }
}
关于C# Indexer 属性 - 有什么方法可以虚拟化 get 而不是 set 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6655636/