EmptyArray.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. class EmptyArray<T>
  9. {
  10. static T[] instance;
  11. EmptyArray()
  12. {
  13. }
  14. internal static T[] Instance
  15. {
  16. get
  17. {
  18. if (instance == null)
  19. instance = new T[0];
  20. return instance;
  21. }
  22. }
  23. internal static T[] Allocate(int n)
  24. {
  25. if (n == 0)
  26. return Instance;
  27. else
  28. return new T[n];
  29. }
  30. internal static T[] ToArray(IList<T> collection)
  31. {
  32. if (collection.Count == 0)
  33. {
  34. return EmptyArray<T>.Instance;
  35. }
  36. else
  37. {
  38. T[] array = new T[collection.Count];
  39. collection.CopyTo(array, 0);
  40. return array;
  41. }
  42. }
  43. internal static T[] ToArray(SynchronizedCollection<T> collection)
  44. {
  45. lock (collection.SyncRoot)
  46. {
  47. return EmptyArray<T>.ToArray((IList<T>)collection);
  48. }
  49. }
  50. }
  51. class EmptyArray
  52. {
  53. static object[] instance = new object[0];
  54. EmptyArray()
  55. {
  56. }
  57. internal static object[] Instance
  58. {
  59. get
  60. {
  61. return instance;
  62. }
  63. }
  64. internal static object[] Allocate(int n)
  65. {
  66. if (n == 0)
  67. return Instance;
  68. else
  69. return new object[n];
  70. }
  71. }
  72. }