nmZ
nmZ

Reputation: 21

C# Clear Array but not fill with 0 alternative of List.Clear (get size back to 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!

Upvotes: 0

Views: 863

Answers (3)

Kyle Baran
Kyle Baran

Reputation: 1839

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.

Upvotes: 0

Shaharyar
Shaharyar

Reputation: 12439

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.

Upvotes: -1

cdhowie
cdhowie

Reputation: 168988

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}

Upvotes: 5

Related Questions