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