-2

I was working with a array in C# and I want it to have its default values again i tried creating a second array as following:

static readonly int[] defmenuItemsParInt = { 60, 60, 0 };
static int[] menuItemsParInt = defmenuItemsParInt;

for some reason this changed the default array aswell so i tried:

static int[] menuItemsParInt = { 60, 60, 0 };
static readonly int[] defmenuItemsParInt = menuItemsParInt;

(the arrays are declared above the main class)

both I used my reset function:

static void reset()
        {
            menuItemsParInt = defmenuItemsParInt;
        }

these methods didnt work is there a easier method or am i doing something wrong?

Ian.V
  • 345
  • 3
  • 19
  • So are you trying to convert int array to string array or other way around? – xszaboj Dec 08 '16 at 11:44
  • 2
    Your code currently provided wouldnt compile - so, no this is definately wrong. what exactly are you trying to do? You have ints on one and string on the other.. – BugFinder Dec 08 '16 at 11:45
  • Arrays are assigned by reference. You need to copy it. – CodeCaster Dec 08 '16 at 11:45
  • `defmenuItemsParInt` and `menuItemsParInt` refer to the same array. You need to copy the items from one array to the other. – Dennis_E Dec 08 '16 at 11:48
  • lol i copied the wrong part of the code sorry guys i editted it – Ian.V Dec 08 '16 at 11:48
  • Possible duplicate of [What is the difference between a reference type and value type in c#?](http://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c) – slawekwin Dec 08 '16 at 11:56

3 Answers3

1

One solution is use CopyTo function like below. The CopyTo function copies all the elements of the array defmenuItemsParInt to the destination array mentioned as the first argument. This way, since as the elements are only getting copied, it won't affect the default array in any case.

static readonly string[] defmenuItemsParInt = { 60, 60, 0 };
static string[] menuItemsParInt = new int[defmenuItemsParInt.Length];
defmenuItemsParInt.CopyTo(menuItemsParInt, 0);
Sethu Bala
  • 457
  • 3
  • 17
  • @CodeCaster The CopyTo function copies all the elements of the array defmenuItemsParInt to the destination array mentioned as the first argument. This way, since as the elements are only getting copied, it won't affect the default array in any case. – Sethu Bala Dec 08 '16 at 11:53
1

Objects (arrays included) in C# are stored by reference. This implies that when you write menuItemsParInt = defmenuItemsParInt you are not copying the object data, but a reference to it. As a result both identifiers (menuItemsParInt and defmenuItemsParInt) point to the same data.

To make a copy of the data in the array you can use Clone method:

static int[] menuItemsParInt = (int[])defmenuItemsParInt.Clone();

The same should be done when reverting the changes:

static void reset()
{
    menuItemsParInt = (int[])defmenuItemsParInt.Clone();
}
Community
  • 1
  • 1
slawekwin
  • 6,270
  • 1
  • 44
  • 57
0

You are changing the elements because your second array is actually pointing at them. Try this instead:

string[] defmenuItemsParInt = { "60", "60", "0" };
int[] menuItemsParInt = (int[]) defmenuItemsParInt.Clone();´

This makes a clone of your initial array. Hope it helps in any way.

Pete Gee
  • 26
  • 2