prog.cpp
prog.cpp

Reputation: 25

In c# - Arrays in set and get method implement

I am strugling with the int[Array] since yesterday. I have the following code:

public int[] Numbers
    {
        get
        {
            return class.intNumbers();
        }
        set
        {
            int[] Number = class.intNumbers();
             Number[Number.Length + 1] = value; 
            writer.NumberValue("Name", "Id", "Here goes the Array");
        }
    }

What I want the code to do is to take the array from other class and on the next index to put my value.(Fails at Number[Number + 1] = value;) The get method is succesfully finished, all I have to do now is "set". Do you have any ideas?

P.S I only want to use array and not arraylist :)

Upvotes: 1

Views: 82

Answers (1)

Ray Krungkaew
Ray Krungkaew

Reputation: 6965

  1. You can use ArrayResizer, although I don't know why you do not want to use List here.

    int[] arr = { 1, 2, 3 };
    Array.Resize(ref arr , 5);
    

    https://msdn.microsoft.com/en-us/library/bb348051(v=vs.110).aspx

  2. You could also implement private List<int> numberList Then return numberList.ToArray(); in getter.

Upvotes: 1

Related Questions