m-y*_*m-y 6 c# virtual dictionary indexer properties
我有一种特殊的字典.我不确定如何准确地执行此操作,但我希望将get方法设为虚拟,而不是set方法:
public TValue this[TKey key]
{
get { ... }
set { ... }
}
Run Code Online (Sandbox Code Playgroud)
是否可能,如果是,那么正确的组合是什么?
Mar*_*ell 13
你不能直接这样做- 你需要添加一个单独的方法:
protected virtual TValue GetValue(TKey key) { ...}
public TValue this[TKey key]
{
get { return GetValue(key); }
set { ... }
}
Run Code Online (Sandbox Code Playgroud)
抱歉...在C#中没有这样做的语法,但你可以这样做.
public TValue this[TKey key]
{
get { return GetValue(key) }
set { ... }
}
protected virtual TValue GetValue(TKey key)
{
...
}
Run Code Online (Sandbox Code Playgroud)