您可以围绕
ArrayList<T>
,实现
IReadOnlyList<T>
. 类似:
public class FooImmutableArray<T> : IReadOnlyList<T> {
private readonly T[] Structure;
public static FooImmutableArray<T> Create(params T[] elements) {
return new FooImmutableArray<T>(elements);
}
public static FooImmutableArray<T> Create(IEnumerable<T> elements) {
return new FooImmutableArray<T>(elements);
}
public FooImmutableArray() {
this.Structure = new T[0];
}
private FooImmutableArray(params T[] elements) {
this.Structure = elements.ToArray();
}
private FooImmutableArray(IEnumerable<T> elements) {
this.Structure = elements.ToArray();
}
public T this[int index] {
get { return this.Structure[index]; }
}
public IEnumerator<T> GetEnumerator() {
return this.Structure.AsEnumerable().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
public int Count { get { return this.Structure.Length; } }
public int Length { get { return this.Structure.Length; } }
}