0

Is it possible to fully remove Array in C# but not to fill it with 0's:

for(int i=0;i<a.Length;i++)
{
a[i]=0;
}

or Array.Clear(a,0,a.Length);

But to clear it in a way that List.Clear() does so that it's size will be 0 again like before filling. I tried a=new int[15]; but prevous values where still there. Thanks!

nmZ
  • 21
  • 2

3 Answers3

5

Arrays in C# are fixed-length; you cannot change the size of an array. You can allocate an array of a different size and copy the elements in order to simulate resizing (this is exactly what List<T> does internally), but you cannot "clear an array" in the sense that you reduce it to zero elements.

I tried a=new int[15]; but prevous values where still there.

The previous values cannot possibly still be there, because this allocates a new int array of 15 elements, where all elements are zero.

Note that this does not alter the array that a referenced; rather, it creates a new array and stores a reference to it in a. So if you initialized a from another array variable, they would have referred to the same array, but after assigning a new array to a the other variable would continue to point to the old array. Perhaps this is where the "previous values" are coming from.

var a = new int[] { 1, 2, 3 };
var b = a;

// a and b now reference the same array.

a = new int[] { 4, 5, 6 };

// a is now {4,5,6} but b remains {1,2,3}
cdhowie
  • 158,093
  • 24
  • 286
  • 300
0

As others have said, it depends on the type semantics that you're putting into the array.

Value types (such as int, bool, and float) are ... well, values. They represent a quantity, something tangible, a state. Thus, they are required to be known at compile time and have a default value.

By contrast, reference types (basically every class) don't actually hold any values themselves, but "group" data together by means of reference. Reference types will either point to other reference types, or eventually to a value type (which holds actual data).

This distinction is important to your question. List<T> is a dynamically sized collection that can grow or shrink without creating a new object because of how it is implemented. Each element in the list points to the next element, thus it's size cannot be known ahead of time.

Arrays are a fixed-size collection that are declared to be a specific size. The type of array determines how much memory is reserved by the system. For example a byte[] of 100 elements will consume less memory than an Int64[] array of 100 elements. Thus, the system needs to know ahead of time how many bytes to reserve in total, which means it needs a default value to "fall back" on to satisfy compile-time checking. Where T[] is a reference type/class, this is null. For value types, this is usually 0 (or default(T)).

If you wanted to remove all the values of an array, similar to how List.Clear() works, you can do int[] a = new int[0];, but note that you are creating an entirely new array and reallocating the memory for them (hence the keyword new). Other objects will need to reference this new array. By design, you can't simply resize an array. A list is a mutable collection and supports changing the number of elements. You could also try int[] a = null, but this sets it to no object at all, which is again, something different.

Kyle Baran
  • 1,793
  • 2
  • 15
  • 30
  • *"in both of these cases you are creating an entirely new array"* -- Not in the `a = null;` case. You are setting `a` to the absence of any object in that case. – cdhowie Oct 04 '14 at 07:21
  • Edited again. Gosh, I really need to fact check before I answer questions, hah. – Kyle Baran Oct 04 '14 at 07:23
-1

It depends whether the array's elements are Value type or Reference type.

In your case it is value type so you'll have to have some value in it. You can not assign null to it.

Because value type objects have some default values.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • [Arrays are always reference types](http://stackoverflow.com/questions/1533757/is-int-a-reference-type-or-a-value-type).. the issue has no relation to being an array of structs or classes. – user2864740 Oct 04 '14 at 06:47
  • Arrays are reference type. But the elements of an array are value type (in this case). There is a difference. Here we are not dealing with array's reference, but with it's elements. – Shaharyar Oct 04 '14 at 06:49
  • No, it doesn't. Assigning `default(T)` (or another applicable value) for each item in the given array `T[]` makes no difference. The fact that default(int) is 0 doesn't change this as the issue is not about "null" values but rather the array having a particular size / number of elements. – user2864740 Oct 04 '14 at 06:50