-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathDisposableArray.cs
More file actions
50 lines (40 loc) · 1.22 KB
/
DisposableArray.cs
File metadata and controls
50 lines (40 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Collections.Generic;
namespace Microsoft.Data.SqlClient.Tests.Common
{
public class DisposableArray<T> : IDisposable, IEnumerable<T>
where T : IDisposable
{
private readonly T[] _elements;
public T this[int i]
{
get => _elements[i];
set => _elements[i] = value;
}
public int Length => _elements.Length;
public DisposableArray(int size)
{
_elements = new T[size];
}
public DisposableArray(T[] elements)
{
_elements = elements;
}
public void Dispose()
{
foreach (T element in _elements)
{
element?.Dispose();
}
GC.SuppressFinalize(this);
}
public IEnumerator<T> GetEnumerator() =>
((IEnumerable<T>)_elements).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() =>
_elements.GetEnumerator();
}
}