什么是重presenting不可变列表中的.NET最好的方法是什么? [英] What is the best way of representing an immutable list in .NET?

查看:154
本文介绍了什么是重presenting不可变列表中的.NET最好的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用F#为真正的工作,并重新找回了不可改变的数据结构之美,如F#中可识别联合和记录。我还发现他们是很简单的从C#来使用,尤其是当他们并不需要在F#运行任何直接的相关性。但是,当涉及到重新presenting列出了这些结构,我还没有找到一个理想的解决方案。

I've recently started using F# for "real work" and rediscovered the beauty of immutable data structures such as the discriminated unions and records in F#. I've also found them to be quite straight forward to use from C#, especially as they don't require any direct dependencies on the F# runtime. However, when it comes to representing lists in these structures, I have not yet found an ideal solution.

我的第一次尝试键入列为SEQ<'一>(IEnumerable的在C#世界),它提供了一个很好的常规采集接口没有出口任何方法变异的收集方式的ICollection<>和它的朋友一样。但是,由于我有过歧视工会或记录的构造函数无法控制,它有可能为这些类型的实例的创建者提供一个IEnumerable<>的实施可能会改变或使用时抛出(如LINQ前pression)。 IEnumerable的<>会,以不给我证明的价值是不可变的,并为此线程安全的编译器的帮助

My first attempt was to type the lists as seq<'a> (IEnumerable in the C# world) which provides a nice general collection interface without exporting any methods for mutating the collection the way ICollection<> and its friends does. However, since I have no control over the constructor of a discriminated union or record, it is possible for the creator of an instance of these types to provide an IEnumerable<> implementation that might change or throw when used (such as a LINQ expression). IEnumerable<> will therefor not give me any help from the compiler in proving that the value is immutable and therefor thread safe.

我目前的策略是要使用的确实保证一个不可变的集合F#列表类型,但增加了一个依赖于F#运行时和非F#项目中使用时看起来有点假。但它确实允许F#模式匹配它的IEnumerable&LT;>没有。它也不会放弃在列表中的实际再presentation任何选择,在某些情况下(如原始值的大型列表)的F#列表重新presentation并不真正适合。

My current strategy is to use the F# list type which does guarantee an immutable collection, but adds a dependency on the F# runtime and looks a bit off when used from non F# projects. It does however allow for F# pattern matching which IEnumerable<> does not. It also doesn't give any choice in the actual representation of a list, and in some cases (such as large lists of primitive values) the F# list representation does not really fit.

我真正想看到的是在.NET中一个不可变的数组类型,再presented一样紧凑的普通阵列但不被突变的编译器保证。我欢迎常量在C ++中,虽然它可能不是很可能发生。在此期间,有我已经错过了任何其他的选择吗?

What I really would like to see is an immutable array type in .NET, represented just as compactly as the normal array but with a compiler guarantee of not being mutated. I would welcome const as in C++ although it's probably not very likely to happen. In the meantime, is there any other option I've missed?

推荐答案

如果你想要的是一个不变的数组实现,那么你可以使用类似下面的

If what you want is an immutable array implementation then you can just use something like the following

public sealed class ImmutableArray<T> : IEnumerable<T>{
  private readonly T[] m_array;
  public T this[int index] {
    get { return m_array[index]; }
  }
  public int Length {
    get { return m_array.Length; }
  }
  public ImmutableArray(IEnumerable<T> enumerable) {
    m_array = enumerable.ToArray();
  }
  // IEnumerable<T> implementation ommitted
}

这篇关于什么是重presenting不可变列表中的.NET最好的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆