ICollection.cs 3.0 KB

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