30

I have a global variable int[] and I want to clear its data and fill it again in a loop.

How could this possible in C#?

4
  • 6
    How can you have a global variable in C#? Commented Apr 20, 2010 at 8:11
  • 1
    @rep_movsd that is another question ;) lol Commented Apr 20, 2010 at 8:13
  • Does the number of elements change in each iteration of the loop? It would be good to see some code. Commented Apr 20, 2010 at 8:26
  • Duplicates: stackoverflow.com/questions/1407715/… stackoverflow.com/questions/1807307/… Commented Apr 20, 2010 at 8:28

6 Answers 6

61

The static Array.Clear() method "sets a range of elements in the Array to zero, to false, or to Nothing, depending on the element type". If you want to clear your entire array, you could use this method an provide it 0 as start index and myArray.Length as length:

Array.Clear(myArray, 0, myArray.Length);
Sign up to request clarification or add additional context in comments.

4 Comments

This method don't clear array literally, but just set 0/false/null for each element in array.
@Nyerguds array.Length = 0
@Benjin The .Length property is read-only information, not a variable. You can't change the length of an array in c# after its construction.
@Nyerguds erm, I meant ==. One could reasonably (absent the context of the question details) interpret "cleared array" as "empty array", and initialize to length 0. Dunno why; just meant to provide an alternative to 1_bug's comment.
5

This is not correct answer for your post but you can use this logic according to your need. Here is a code Snippets taken from here

using System;

class ArrayClear
{

   public static void Main()
   {
      int[] integers = { 1, 2, 3, 4, 5 };
      DumpArray ("Before: ", integers);
      Array.Clear (integers, 1, 3);
      DumpArray ("After:  ", integers);
   }

   public static void DumpArray (string title, int[] a)
   {
      Console.Write (title);
      for (int i = 0; i < a.Length; i++ )
      {
         Console.Write("[{0}]: {1, -5}", i, a[i]);
      }
      Console.WriteLine();
   }
}

and output of this is:

Before: [0]: 1    [1]: 2    [2]: 3    [3]: 4    [4]: 5
After:  [0]: 1    [1]: 0    [2]: 0    [3]: 0    [4]: 5

Comments

3

Wouldnt it be easier to use a list instead.

public List<int> something = new List<int>();

And then:

something.Add(somevalue);

And to clear:

something.Clear();

8 Comments

-1: You have NO idea that it would be easier to use a list! The question is about arrays.
@Adrian: Agreed! This question asks about arrays, not lists!
@Adrian: Thats correct i do have no idea that i would be easier, however im just suggesting, OP can just take this under advisement and select another answer as the correct one if he really needs an array. A bit harsh to just downvote imho :(.
I agree with Fabian, a List IS a good alternative to an array and can easily be converted to one if required. Suggesting a better alternative is all part of answering a question - it is up to the OP to decide whether it's an appropriate answer or not.
Maybe the OP ran into the "XY Problem" with using an array, so it's perfectly fine for @Fabian to suggest an alternative... meta.stackexchange.com/questions/66377/what-is-the-xy-problem
|
3

Why not just create new array and assign it to existing array variable?

x = new int[x.length];

1 Comment

Because that means the garbage collector has extra work cleaning up everything you leave behind in memory.
0
  int[] x 
  int[] array_of_new_values

  for(int i = 0 ; i < x.Length && i < array_of_new_values.Length ;i++)
  {
        x[i] = array_of_new_values[i]; // this will give x[i] its new value
  }

Why clear it? Just assign new values.

2 Comments

I can see many reasons why you would want a clean zero'd array, debugging for example, seeing the data going into the array would be much easier to monitor than overwriting data.
I change some values and the other must be "0". I can do it in this way but I was looking for 1 line code.
0

For two dimensional arrays, you should do as bellow:

Array.Clear(myArray, 0, myArray.GetLength(0)*myArray.GetLength(1));

1 Comment

myArray.Length is a shorter way to say myArray.GetLength(0)*myArray.GetLength(1) for 2D arrays.

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.