Lists.cs 971 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Text;
  5. namespace Google.ProtocolBuffers.Collections {
  6. public static class Lists {
  7. public static IList<T> AsReadOnly<T>(IList<T> list) {
  8. return Lists<T>.AsReadOnly(list);
  9. }
  10. }
  11. /// <summary>
  12. /// Utilities class for dealing with lists.
  13. /// </summary>
  14. public static class Lists<T> {
  15. static readonly ReadOnlyCollection<T> empty = new ReadOnlyCollection<T>(new T[0]);
  16. /// <summary>
  17. /// Returns an immutable empty list.
  18. /// </summary>
  19. public static ReadOnlyCollection<T> Empty {
  20. get { return empty; }
  21. }
  22. /// <summary>
  23. /// Returns either the original reference if it's already read-only,
  24. /// or a new ReadOnlyCollection wrapping the original list.
  25. /// </summary>
  26. public static IList<T> AsReadOnly(IList<T> list) {
  27. return list.IsReadOnly ? list : new ReadOnlyCollection<T>(list);
  28. }
  29. }
  30. }