None of these is particularly satisfactory??”the predicate approach is much neater, giving
emphasis to what you want to achieve rather than how exactly it should happen. It??™s a
good idea to experiment with predicates a bit to get comfortable with them, particularly
if you??™re likely to be using C# 3 in a production setting any time in the near future??”this
more functional style of coding is going to be increasingly important over time.
Next we??™ll have a brief look at the methods that are present in ArrayList but not
List
, and consider why that might be the case.
FEATURES ???MISSING??? FROM LIST
A few methods in ArrayList have been shifted around a little??”the static ReadOnly
method is replaced by the AsReadOnly instance method, and TrimToSize is nearly
replaced by TrimExcess (the difference is that TrimExcess won??™t do anything if the
size and capacity are nearly the same anyway). There are a few genuinely ???missing???
pieces of functionality, however. These are listed, along with the suggested
workaround, in table 3.3.
The Synchronized method was a bad idea in ArrayList to start with, in my view. Making
individual calls to a collection doesn??™t make the collection thread-safe, because so
many operations (the most common is iterating over the collection) involve multiple
Table 3.3 Methods from ArrayList with no direct equivalent in List
ArrayList method Way of achieving similar effect
Adapter None provided
Clone list.
Pages:
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213