ICollection.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System.Collections
  5. {
  6. // Base interface for all collections, defining enumerators, size, and
  7. // synchronization methods.
  8. public interface ICollection : IEnumerable
  9. {
  10. // Interfaces are not serializable
  11. // CopyTo copies a collection into an Array, starting at a particular
  12. // index into the array.
  13. //
  14. void CopyTo(Array array, int index);
  15. // Number of items in the collections.
  16. int Count
  17. { get; }
  18. // SyncRoot will return an Object to use for synchronization
  19. // (thread safety). You can use this object in your code to take a
  20. // lock on the collection, even if this collection is a wrapper around
  21. // another collection. The intent is to tunnel through to a real
  22. // implementation of a collection, and use one of the internal objects
  23. // found in that code.
  24. //
  25. // In the absence of a static Synchronized method on a collection,
  26. // the expected usage for SyncRoot would look like this:
  27. //
  28. // ICollection col = ...
  29. // lock (col.SyncRoot) {
  30. // // Some operation on the collection, which is now thread safe.
  31. // // This may include multiple operations.
  32. // }
  33. //
  34. //
  35. // The system-provided collections have a static method called
  36. // Synchronized which will create a thread-safe wrapper around the
  37. // collection. All access to the collection that you want to be
  38. // thread-safe should go through that wrapper collection. However, if
  39. // you need to do multiple calls on that collection (such as retrieving
  40. // two items, or checking the count then doing something), you should
  41. // NOT use our thread-safe wrapper since it only takes a lock for the
  42. // duration of a single method call. Instead, use Monitor.Enter/Exit
  43. // or your language's equivalent to the C# lock keyword as mentioned
  44. // above.
  45. //
  46. // For collections with no publicly available underlying store, the
  47. // expected implementation is to simply return the this pointer. Note
  48. // that the this pointer may not be sufficient for collections that
  49. // wrap other collections; those should return the underlying
  50. // collection's SyncRoot property.
  51. object SyncRoot
  52. { get; }
  53. // Is this collection synchronized (i.e., thread-safe)? If you want a
  54. // thread-safe collection, you can use SyncRoot as an object to
  55. // synchronize your collection with. If you're using one of the
  56. // collections in System.Collections, you could call the static
  57. // Synchronized method to get a thread-safe wrapper around the
  58. // underlying collection.
  59. bool IsSynchronized
  60. { get; }
  61. }
  62. }