IReadOnlyArray<T> in C# 可以翻译为 C# 中的 IReadOnlyArray<T>。

huangapple go评论53阅读模式
英文:

IReadOnlyArray<T> in C#

问题

C#是否有一种接口,允许您读取数组元素但不允许更改数组,或者是否可以在现有的Array类中实现自定义接口? 我知道可以使用包装器、适配器和类似的东西来实现所需的所有接口,但我很高兴在我的代码中保留所有数组,只需在某个地方返回接口。

英文:

I have some wish to be able to guarante that array will not be changed if I provide it somewhere.
Does C# have some interface for arrays that allows you to read elements, but doesn't allow to change?
Or is it possible to implement my hand made interface in the existing Array-class?

I know that I can make wrappers, adapters and something like this with all implemented interfaces I need.
But I would be glad to leave all arrays in my code, just return the interface somewhere.

答案1

得分: 3

以下是翻译好的内容:

  1. IEnumerable<T> - 当您只需要一个要迭代的项目序列时使用。甚至可以是无限长度的。
  2. IReadOnlyCollection<T> - 当您还需要知道有多少个项目时使用。
  3. IReadOnlyList<T> - 当您还需要按索引访问项目时使用。

请注意,所有这些接口都允许将集合转换为实际类型并进行修改。但这显然是不良实践,任何这样做的人都必须承受后果。这些接口由最常见的 .NET 集合(如 List<T>T[] 等)实现。

然后还有 ReadOnlySpan<T>ReadOnlyMemory<T>,但它们更适用于低级代码。我将其视为一种类型安全的指针,而不是用于集合的接口。

旧代码可能使用 ICollection<T>IList<T>,但我不喜欢这些接口,因为它们不是只读的,我认为它们相对较臃肿。还有一个 ReadOnlyCollection<T>,但它实现了 ICollection<T>IList<T>,因此违反了 Liskov 替代原则,因此我不会在新代码中使用它。

英文:

The most common interfaces are

  1. IEnumerable&lt;T&gt; - When you just need some sequence of items to iterate over. May even be of infinite length.
  2. IReadOnlyCollection&lt;T&gt; - When you also need to know how many items there are
  3. IReadOnlyList&lt;T&gt; - When you also need to access an item by index

Note that all of these allow the collection to be cast to the actual type, and modified. But this is obviously bad practice, and anyone doing that have to suffer the consequences. These interfaces are implemented by the most common .net collections, like List&lt;T&gt;, T[] etc.

Then there is ReadOnlySpan&lt;T&gt; and ReadOnlyMemory&lt;T&gt;, but these are more intended for low level code. I would view these as a kind of type safe pointer rather than an interface for a collection.

Old code may use ICollection&lt;T&gt; or IList&lt;T&gt;, I dislike these interfaces, since they are not read only, and I consider them rather bloated. There is also a ReadOnlyCollection&lt;T&gt;, but this implements ICollection&lt;T&gt; and IList&lt;T&gt;, and therefore violates Liskov substitution principle, so I would not use it for new code.

答案2

得分: 2

你可以简单地使用 Array.AsReadOnly,示例:

int[] myReadWriteArray = { 7, 9, 13, };
ReadOnlyCollection&lt;int&gt; myReadOnlyArray = Array.AsReadOnly(myReadWriteArray);

但请注意,这两种类型(int[]ReadOnlyCollection&lt;int&gt;)都实现了接口 IReadOnlyList&lt;out T&gt;,所以在某些情况下,而不是使用 AsReadOnly 包装在包装类中,你可以直接将相同的实例作为这个接口的类型返回:

int[] myReadWriteArray = { 7, 9, 13, };
IReadOnlyList&lt;int&gt; myReadOnlyReference = myReadWriteArray;
英文:

You can look into simply Array.AsReadOnly; example:

int[] myReadWriteArray = { 7, 9, 13, };
ReadOnlyCollection&lt;int&gt; myReadOnlyArray = Array.AsReadOnly(myReadWriteArray);

But also note that both types (int[] and ReadOnlyCollection&lt;int&gt;) implement the interface IReadOnlyList&lt;out T&gt;, so in some situations rather than wrapping in a wrapper class with AsReadOnly, it may be sufficient for you to just return the same instance typed as this interface:

int[] myReadWriteArray = { 7, 9, 13, };
IReadOnlyList&lt;int&gt; myReadOnlyReference = myReadWriteArray;

答案3

得分: 1

所有一维数组都实现了 IReadOnlyList&lt;T&gt; 接口。因此,您可以将类内部的数组公开为公共属性,如下所示:

class SomeClass
{
    private readonly int[] _numbers;

    public IReadOnlyList&lt;int&gt; Numbers => _numbers;
}

您的类的用户将无法修改内部数组,除非使用强制类型转换等技巧。

英文:

All one-dimensional arrays implement the IReadOnlyList&lt;T&gt; interface. So you could expose an array that is internal to your class as a public property like this:

class SomeClass
{
    private readonly int[] _numbers;

    public IReadOnlyList&lt;int&gt; Numbers =&gt; _numbers;
}

Users of your class won't be able to mutate the internal array, without resorting to tricks like casting.

答案4

得分: 0

请查看ReadOnlyMemory<T>。您可以返回此对象,而不是返回您的数组。

英文:

Please check out ReadOnlyMemory<T>. You can return this instead of your array.

huangapple
  • 本文由 发表于 2023年3月7日 22:51:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定