Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The big problem here isn't really what your library does with the values (which is a potential problem, albeit a much more manageable one), but rather what callers might do with the values. If you need to treat them as immutable, then you need to ensure that a library consumer cannot change the contents after their original assignment. The easy fix here would be to create an interface that exposes all the array members that your library uses, then create an immutable wrapper class for an array that implements this interface to use in your <code>InputObject</code> class. <em>e.g.</em>:</p> <pre><code>public interface IArray&lt;T&gt; { int Length { get; } T this[int index] { get; } } internal sealed class ImmutableArray&lt;T&gt; : IArray&lt;T&gt; where T : struct { private readonly T[] _wrappedArray; internal ImmutableArray(IEnumerable&lt;T&gt; data) { this._wrappedArray = data.ToArray(); } public int Length { get { return this._wrappedArray.Length; } } public T this[int index] { get { return this._wrappedArray[index]; } } } public class InputObject { private readonly IArray&lt;double&gt; _x; private readonly IArray&lt;double&gt; _y; public InputObject(double[] x, double[] y) { this._x = new ImmutableArray&lt;double&gt;(x); this._y = new ImmutableArray&lt;double&gt;(y); } public IArray&lt;double&gt; X { get { return this._x; } } public IArray&lt;double&gt; Y { get { return this._y; } } //... } </code></pre> <p>The elements in your "immutable" array contents would still be mutable if T is mutable, but at least you're safe for the double type.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload