【发布时间】:2016-07-20 13:18:25
【问题描述】:
关于双向字典:Bidirectional 1 to 1 Dictionary in C#
我的双词典是:
internal class BiDirectionContainer<T1, T2>
{
private readonly Dictionary<T1, T2> _forward = new Dictionary<T1, T2>();
private readonly Dictionary<T2, T1> _reverse = new Dictionary<T2, T1>();
internal T2 this[T1 key] => _forward[key];
internal T1 this[T2 key] => _reverse[key];
internal void Add(T1 element1, T2 element2)
{
_forward.Add(element1, element2);
_reverse.Add(element2, element1);
}
}
我想添加这样的元素:
BiDirectionContainer<string, int> container = new BiDirectionContainer<string, int>
{
{"111", 1},
{"222", 2},
{"333", 3},
}
但我不确定在BiDirectionContainer 中使用IEnumerable 是否正确?
如果是我应该返回什么?有没有其他方法可以实现这样的功能?
【问题讨论】:
-
@Dirk Vollmar 我所需要的只是简单的初始化。我不打算在 foreach 语句中使用它。但我担心我的课会混淆其他可以使用它的人。
标签: c# dictionary ienumerable bidirectional