1

Is there a way to prevent reinitialization of an array? (to force specific length)

example initial:

int[] array = new int[3]{0,0,0};

usage:

array = new int[5]{1,2,3,4,5};

The above usage will reinitialize the array with length of 5.

The array will always have 3 elements, but the values of the elements will always be changing.

I am trying to avoid doing the following to assign it's values:

array[0] = 1;
array[1] = 2;
array[2] = 3;
4
  • 5
    It's not clear what you mean. What exactly are you trying to prevent? Which line should be invalid? What's wrong with int[] array = { 1, 2, 3 }? Commented Apr 25, 2015 at 19:37
  • You want to avoid 1 or 2? or both? Commented Apr 25, 2015 at 19:38
  • I want to prevent changing the length of the array via reinitialization such as my usage example. So, if try to use "array = { 1, 2, 3, 4 }", it will just throw an exception or something. Commented Apr 25, 2015 at 19:40
  • 2
    Don't expose the field. Make it as property. Validate the value in property setter. Commented Apr 25, 2015 at 19:42

4 Answers 4

2

Try declaring it readonly:

readonly int[] array = new int[3]{0,0,0};
Sign up to request clarification or add additional context in comments.

Comments

2

I am not sure whether it is what you want, but I can propose to you the following wrapper class:

public class FixedArrayHolder<T>
{
    public FixedArrayHolder(Int32 fixedLength, T[] array)
    {
        if (fixedLength < 0)
            throw new ArgumentOutOfRangeException();
        if (array == null)
            throw new ArgumentNullException();

        this.FixedLength = fixedLength;
        this.Array = array;
    }

    public Int32 FixedLength
    {
        get;
        private set;
    }

    private T[] m_array;

    public T[] Array
    {
        get
        {
            return this.m_array;
        }
        set
        {
            if (value == null)
                throw new ArgumentNullException();
            if (value.Length != this.FixedLength)
                throw new ArgumentException();

            this.m_array = value;
        }
    }

    public static implicit operator T[](FixedArrayHolder<T> fixedArray)
    {
        if (fixedArray == null)
            return null;

        return fixedArray.Array;
    }
}

You can use this class in place of standard arrays:

// Works
var array = new FixedArrayHolder<Int32>(3, new Int32[] { 1, 2, 3 });

// Works
array.Array = new Int32[] { 3, 4, 5 };

// Fails
array.Array = new Int32[] { 1, 2, 3, 4 };

P.S.: You can extend it with some indexer and IEnumerable, IList members to allow more streamlined access to elements if it is required.

Comments

1
private int[] origin = new int[3];
    public int[] Origin {
        get {  return origin;} 
        set{
            if (value.Length >3) throw new ArgumentOutOfRangeException();
            else origin = value;
        }
    }

Don't expose the field. Make it as property. Validate the value in property setter. – Sriram Sakthivel

This worked perfectly, thanks!

Comments

0

I think you should encapsulate the array in a class, instantiating it from the constructor. Use a public getter and private setter to control access:

public int[] MyArray {
    get;
    private set;
}

Fiddle: https://dotnetfiddle.net/ZsjrsN

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.