3

您可能知道以下问题:您想在两个对象 A 和 B 之间共享一个集合(或者类似地希望通过一个属性公开一个集合)并且......好吧,您意识到这实际上不是一个好主意,因为 A然后B现在可以修改集合和废话世界末日...

但是您经常意识到您不想共享整个集合,而只想用集合中的项目初始化一个对象或将项目传递给对象的方法。此外,对象的项目在对象的生命周期内不会更改,因此您通常最终会执行以下操作:

public class Foo
{
  private List<int> items;

  public Foo(IEnumerable<int> items)
  {
    this.items = items.ToList();
  }

  public ReadOnlyCollection<int> Items
  {
    get { return new ReadOnlyCollection(this.items); }
  }
}

.ToList() 和 ReadonlyCollection-Wrapper 都为问题提供了合理的解决方案,但也遇到了一些问题: .ToList() 复制了整个 List - 所以我最终得到了同一个集合的 x 个实例,而 ReadOnlyCollection 只确保客户不会更改集合,但不向客户保证集合不会更改。

实际上这是我经常遇到的情况,因为我编写的大多数类(猜测:80 - 90%)实际上是不可变的,并且通常聚合基本上代表不可变数组的集合(在这种情况下意味着:固定大小的不可变序列项)。从我的角度来看,这个问题的一个很好的解决方案非常简单:为一个数组构建一个包装类,保证在构造函数中正确初始化,并且只提供不改变底层数组的方法/属性。

长话短说:以下(微不足道的)不可变数组概念的实现有什么问题吗?

顺便说一句:我认为 Sequence 是一个合适的名称,因为 a)它很好地描述了意图(一个固定大小的不可变项目序列)b)它不是 .net 当前使用的名称,并且 c)它很短,即很重要,因为我打算广泛使用它。

public sealed class Sequence<T> : IEnumerable<T>
    {
        private readonly T[] items;

        public Sequence(IEnumerable<T> items)
        {
            this.items = items.ToArray();
        }

        public Sequence(int size, Func<int, T> producer)
        {
            this.items = new T[size];

            for (int i = 0; i < size; i++)
            {
                this.items[i] = producer(i);
            }
        }

        public T this[int i]
        {
            get { return this.items[i]; }
        }

        public int Length
        {
            get { return this.items.Length; }
        }

        public bool Contains(T item)
        {
            for (int i = 0; i < this.items.Length; i++)
            {
                if (this.items[i].Equals(item))
                    return true;
            }

            return false;
        }

        public Enumerator<T> GetEnumerator()
        {
            return new Enumerator<T>(this);
        }

        IEnumerator<T> System.Collections.Generic.IEnumerable<T>.GetEnumerator()
        {
            return GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public struct Enumerator<T> : IEnumerator<T>
        {
            private T[] items;
            private int nextIndex;
            private T current;

            internal Enumerator(Sequence<T> immutableArray)
            {
                this.items = immutableArray.items;
                this.nextIndex = 0;
                this.current = default(T);
            }

            public T Current
            {
                get { return this.current; }
            }

            object System.Collections.IEnumerator.Current
            {
                get { return this.Current; }
            }

            public void Dispose()
            {
            }

            public bool MoveNext()
            {
                if (this.nextIndex < this.items.Length)
                {
                    this.current = this.items[this.nextIndex];
                    this.nextIndex++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            public void Reset()
            {
                this.nextIndex = 0;
                this.current = default(T);
            }
        }
    }
4

3 回答 3

3

你的Sequence班级会给你什么ReadOnlyCollection没有?难道你不能只使用简单的继承来提供你需要的构造函数吗?

public class Sequence<T> : ReadOnlyCollection<T>
{
    public Sequence(IEnumerable<T> items)
        : base(items.ToList()) { }

    public Sequence(int size, Func<int, T> generator)
        : base(Enumerable.Range(0, size).Select(generator).ToList()) { }

    // properties, methods etc provided by the ReadOnlyCollection base class
}
于 2009-09-16T13:42:03.160 回答
2

似乎还可以。请注意,您可以通过使用或仅返回将整个IEnumerable-Code 压缩为几行。yield returnItems.GetEnumerator()

于 2009-09-16T13:34:58.930 回答
1

.NET 刚刚发布了他们的第一个不可变集合,我建议您尝试一下。

于 2012-12-24T17:11:31.153 回答