66using System . Collections ;
77using System . Collections . Generic ;
88
9+ #nullable enable
10+
911namespace Microsoft . Data . SqlClient . Tests . Common
1012{
13+ /// <summary>
14+ /// Utility class that enables disposal of a collection of <see cref="IDisposable"/> objects
15+ /// with a single <c>using</c> statement.
16+ /// </summary>
17+ /// <typeparam name="T">Type of the elements contained within.</typeparam>
1118 public class DisposableArray < T > : IDisposable , IEnumerable < T >
12- where T : IDisposable
19+ where T : IDisposable ?
1320 {
1421 private readonly T [ ] _elements ;
1522
16- public T this [ int i ]
17- {
18- get => _elements [ i ] ;
19- set => _elements [ i ] = value ;
20- }
21-
22- public int Length => _elements . Length ;
23-
23+ /// <summary>
24+ /// Constructs a new instance with <paramref name="size"/> elements.
25+ /// </summary>
26+ /// <remarks>
27+ /// Remember when using this constructor that the underlying array will be initialized to
28+ /// <c>default(T)</c>. If <typeparamref name="T"/> is a reference type, this will be
29+ /// <c>null</c> - even if <typeparamref name="T"/> is not nullable!
30+ /// </remarks>
31+ /// <param name="size">Number of elements the new instance will contain.</param>
2432 public DisposableArray ( int size )
2533 {
2634 _elements = new T [ size ] ;
2735 }
2836
37+ /// <summary>
38+ /// Constructs a new instance from an existing array of elements.
39+ /// </summary>
40+ /// <param name="elements">Array of elements to store within the current instance.</param>
2941 public DisposableArray ( T [ ] elements )
3042 {
3143 _elements = elements ;
3244 }
3345
46+ /// <summary>
47+ /// Gets or sets the element at index <see cref="i"/>.
48+ /// </summary>
49+ /// <param name="i">The element to get/set will be at this position in the array</param>
50+ public T this [ int i ]
51+ {
52+ get => _elements [ i ] ;
53+ set => _elements [ i ] = value ;
54+ }
55+
56+ /// <summary>
57+ /// Gets the number of elements in the array.
58+ /// </summary>
59+ public int Length => _elements . Length ;
60+
61+ /// <summary>
62+ /// Disposes all elements in the current instance. Each element will be checked for
63+ /// <c>null</c> before disposing it.
64+ /// </summary>
3465 public void Dispose ( )
3566 {
3667 foreach ( T element in _elements )
@@ -41,9 +72,11 @@ public void Dispose()
4172 GC . SuppressFinalize ( this ) ;
4273 }
4374
75+ /// <inheritdoc/>
4476 public IEnumerator < T > GetEnumerator ( ) =>
4577 ( ( IEnumerable < T > ) _elements ) . GetEnumerator ( ) ;
4678
79+ /// <inheritdoc/>
4780 IEnumerator IEnumerable . GetEnumerator ( ) =>
4881 _elements . GetEnumerator ( ) ;
4982 }
0 commit comments