Length; i += 1)
idCity = idCity.Add(i + 100, city[i]);
Assert.AreEqual(idCity[102], "Paris");
Map lookup operations
The lookup operations for a map are very similar to those of a .NET dictionary.
If you need to look up the value of a key, you may use the C# indexer syntax:
Map
cityState =
new Map("Athens", "GA", "Paris", "TX"};
Assert.AreEqual(cityState["Paris"], "TX");
164 Modeling Systems with Structured State
An exception will be thrown if the requested key is not in the map. Normally,
you will want to use the ContainsKey method to check that the key is in the map
before doing the lookup.
You can combine the ContainsKey check and lookup into a single operation using
TryGetValue.
static string LookupState(string city)
{
Map cityState =
new Map("Athens", "GA", "Paris", "TX"};
string state;
return (cityState.TryGetValue(city, out state) ? state
: "Unknown state");
}
Iterating through maps
Maps support iteration through keys, values, or key value pairs:
Map cityLength =
new Map("Athens", 6, "Paris", 5};
foreach(string city in cityLength.Keys) /* ... */
foreach(int length in cityLength.Values) /* ... */
foreach(Pair keyValuePair in cityLength) /* ... */
The Keys and Values properties return sets. This means that when iterating
through values of a map, values associated with more than one key will only be
encountered once.
Pages:
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233