How to assign 0 to whole array

After creation all items of array will have default values, which is 0 for int. So, you don’t need to do anything here.

From Arrays (C# Programming Guide):

The default values of numeric array elements are set to zero, and
reference elements are set to null.


Also from C# Specification 12.2 Array creation

Elements of arrays created by array-creation-expressions are always
initialized to their default value.

5.2 Default values

For a variable of a value-type, the default value is the same as the
value computed by the value-type’s default constructor

4.1.2 Default constructors

For sbyte, byte, short, ushort, int, uint, long, and ulong, the
default value is 0.


but after assigning other values i want all the indexes to be 0 again
so then how would i do it?

UPDATE: You can use Array.Clear:

Sets a range of elements in the Array to zero, to false, or to null,
depending on the element type.

In your case:

Array.Clear(array, 0, array.Length);

Consider also to use List<int> instead of array – it allows to add/remove items dynamically.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)