Skip to main content
added 203 characters in body
Source Link

Short answer is that there's not a built-in way of doing this. The framework doesn't automatically keep track of your array's initial state for you, just its current state, so it has no way of knowing how to re-initialize it to its original state. You could do it manually though. The exact approach to this depends on what your array was initialized to in the first place:

        // Array will obviously contain {1, 2, 3}
        int[] someRandomArray = { 1, 2, 3 };

        // Won't compile
        someRandomArray = { 1, 2, 3 };

        // We can build a completely new array with the initial values
        someRandomArray = new int[] { 1, 2, 3 };

        // We could also write a generic extension method to restore everything to its default value
        someRandomArray.ResetArray();

        // Will be an array of length 3 where all values are 0 (the default value for the int type)
        someRandomArray = new int[3];

The ResetArray extension method is below:

// The <T> is to make T a generic type
public static void ResetArray<T>(this T[] array)
    {
        for (int i = 0; i < array.Length; i++)
        {
            // default(T) will return the default value for whatever type T is
            // For example, if T is an int, default(T) would return 0
            array[i] = default(T);
        }
    }

Short answer is that there's not a built-in way of doing this. The framework doesn't automatically keep track of your array's initial state for you, just its current state, so it has no way of knowing how to re-initialize it to its original state. You could do it manually though. The exact approach to this depends on what your array was initialized to in the first place:

        // Array will obviously contain {1, 2, 3}
        int[] someRandomArray = { 1, 2, 3 };

        // Won't compile
        someRandomArray = { 1, 2, 3 };

        // We can build a completely new array with the initial values
        someRandomArray = new int[] { 1, 2, 3 };

        // We could also write a generic extension method to restore everything to its default value
        someRandomArray.ResetArray();

        // Will be an array of length 3 where all values are 0 (the default value for the int type)
        someRandomArray = new int[3];

The ResetArray extension method is below:

public static void ResetArray<T>(this T[] array)
    {
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = default(T);
        }
    }

Short answer is that there's not a built-in way of doing this. The framework doesn't automatically keep track of your array's initial state for you, just its current state, so it has no way of knowing how to re-initialize it to its original state. You could do it manually though. The exact approach to this depends on what your array was initialized to in the first place:

        // Array will obviously contain {1, 2, 3}
        int[] someRandomArray = { 1, 2, 3 };

        // Won't compile
        someRandomArray = { 1, 2, 3 };

        // We can build a completely new array with the initial values
        someRandomArray = new int[] { 1, 2, 3 };

        // We could also write a generic extension method to restore everything to its default value
        someRandomArray.ResetArray();

        // Will be an array of length 3 where all values are 0 (the default value for the int type)
        someRandomArray = new int[3];

The ResetArray extension method is below:

// The <T> is to make T a generic type
public static void ResetArray<T>(this T[] array)
    {
        for (int i = 0; i < array.Length; i++)
        {
            // default(T) will return the default value for whatever type T is
            // For example, if T is an int, default(T) would return 0
            array[i] = default(T);
        }
    }
Source Link

Short answer is that there's not a built-in way of doing this. The framework doesn't automatically keep track of your array's initial state for you, just its current state, so it has no way of knowing how to re-initialize it to its original state. You could do it manually though. The exact approach to this depends on what your array was initialized to in the first place:

        // Array will obviously contain {1, 2, 3}
        int[] someRandomArray = { 1, 2, 3 };

        // Won't compile
        someRandomArray = { 1, 2, 3 };

        // We can build a completely new array with the initial values
        someRandomArray = new int[] { 1, 2, 3 };

        // We could also write a generic extension method to restore everything to its default value
        someRandomArray.ResetArray();

        // Will be an array of length 3 where all values are 0 (the default value for the int type)
        someRandomArray = new int[3];

The ResetArray extension method is below:

public static void ResetArray<T>(this T[] array)
    {
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = default(T);
        }
    }