2
关于双向词典使用集合初始化为双向词典
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我所需要的只是简单的初始化。我不打算在每个表单中使用它。但是我担心我的班级会混淆可以使用它的其他人。 –