AreEqual(cities.Count, 6);
You can also create a bag from an IEnumerable object:
static Bag
ConvertToBag(IEnumerable list)
{
return new Bag(list);
}
Like sets, bags implement the IEnumerable interface, so they can be used iteratively.
If an element appears in a bag more than once, iteration will include it more
than once. If you want to get access to the unique values found in a bag, you can
use the Keys property.
Iteration over bags occurs in an arbitrary order.
Systems with Complex State 169
Bag properties and queries
Bags support all of the set operations. The behavior of these operations is similar to
that of sets, except that multiplicity is respected. For example, the Union operation
for a bag adds the multiplicities of each element.
Bags have a few operations that are not available on sets. For example, you can
find out the multiplicity of an element in a bag with the CountItem method. You can
find out the number of distinct elements in the bag using the CountUnique property.
The Count property returns to total number of elements, including multiples.
10.3.6 Pairs and triples
A pair is a data record with two elements. A pair is an immutable value with
structural equality.
Pair nameId = new Pair(1, "Alice");
Assert.AreEqual(nameId.First, 1);
Assert.AreEqual(nameId.Second, "Alice");
The pair constructor takes the paired values as its argument.
Pages:
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238