11.6. Arrays as Collections

C# arrays are first-class objects, and therefore all methods that apply to the canonical System.Object class can also be applied to a regular array. C# arrays are of type ICollection (in the System.Collections namespace); in contrast, Java arrays are not formally tied to the java.util.Collection package. Arrays are not thread-safe, and therefore operations that modify arrays should use the SyncRoot property of the array to lock the array object, as shown in the following code snippet. (Note the use of the foreach construct.) Because the class Array implements the ICollection and IList interfaces, many of the properties and methods of the class come from these interfaces.

 int[] arr = new int[] {5,5,5,5,5,5}; lock( ...

Get .NET for Java Developers: Migrating to C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.