@@ -35,7 +35,9 @@ public ParseErrorElementDictionary(string key, ParseErrorElement value)
3535 ? this . elements is null
3636 ? throw new KeyNotFoundException ( $ "The key { key } was not found in the dictionary")
3737 : this . elements [ key ]
38- : this . firstItem ! ;
38+ : this . firstKey == key
39+ ? this . firstItem !
40+ : throw new KeyNotFoundException ( $ "The key { key } was not found in the dictionary") ;
3941
4042 public IEnumerable < string > Keys => this . firstKey is null ? this . elements ! . Keys : new [ ] { this . firstKey } ;
4143
@@ -44,8 +46,27 @@ public ParseErrorElementDictionary(string key, ParseErrorElement value)
4446 public int Count => this . firstKey is null ? this . elements is null ? 0 : this . elements . Count : 1 ;
4547
4648 public bool ContainsKey ( string key ) => this . firstKey is null ? this . elements is null ? false : this . elements . ContainsKey ( key ) : this . firstKey == key ;
47- public IEnumerator < KeyValuePair < string , ParseErrorElement > > GetEnumerator ( ) => throw new NotImplementedException ( ) ;
48- public bool TryGetValue ( string key , out ParseErrorElement value ) => throw new NotImplementedException ( ) ;
49+ public IEnumerator < KeyValuePair < string , ParseErrorElement > > GetEnumerator ( ) =>
50+ this . elements is null
51+ ? new Enumerator ( this . firstKey ! , this . firstItem ! )
52+ : this . elements . GetEnumerator ( ) ;
53+ public bool TryGetValue ( string key , out ParseErrorElement value )
54+ {
55+ if ( this . elements is null )
56+ {
57+ if ( this . firstKey == key )
58+ {
59+ value = firstItem ;
60+ return true ;
61+ }
62+
63+ value = default ;
64+ return false ;
65+ }
66+
67+ return this . elements . TryGetValue ( key , out value ) ;
68+ }
69+
4970 IEnumerator IEnumerable . GetEnumerator ( ) => this . GetEnumerator ( ) ;
5071
5172 public ParseErrorElementDictionary Merge ( ParseErrorElementDictionary other )
@@ -91,4 +112,48 @@ public ParseErrorElementDictionary Merge(ParseErrorElementDictionary other)
91112
92113 return new ( elements ) ;
93114 }
115+
116+ private struct Enumerator : IEnumerator < KeyValuePair < string , ParseErrorElement > >
117+ {
118+ private bool valid ;
119+ private KeyValuePair < string , ParseErrorElement > _current ;
120+ public Enumerator ( string key , ParseErrorElement value )
121+ {
122+ this . _current = new KeyValuePair < string , ParseErrorElement > ( key , value ) ;
123+ }
124+
125+ public KeyValuePair < string , ParseErrorElement > Current
126+ {
127+ get
128+ {
129+ if ( ! valid )
130+ {
131+ throw new InvalidOperationException ( "The enumerator is not valid." ) ;
132+ }
133+
134+ return _current ;
135+ }
136+ }
137+
138+ object IEnumerator . Current => this . Current ;
139+
140+ public void Dispose ( ) { }
141+ public bool MoveNext ( )
142+ {
143+ if ( ! this . valid )
144+ {
145+ this . valid = true ;
146+ return true ;
147+ }
148+ else
149+ {
150+ this . valid = false ;
151+ return false ;
152+ }
153+ }
154+ public void Reset ( )
155+ {
156+ this . valid = false ;
157+ }
158+ }
94159}
0 commit comments