C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program: We use Array.Clear to reset all elements in an int array to zero, without reallocating the array or changing its reference.
Int ArrayLength: We use the Length property of the array as the third argument. This clears all elements.
Array LengthC# program that uses Array.Clear
using System;
class Program
{
static void Main()
{
int[] integerArray = new int[]
{
4,
6,
8,
1,
3
};
//
// Display the array
//
Console.WriteLine("--- Integer array before ---");
foreach (int value in integerArray)
{
Console.WriteLine(value);
}
//
// Clear all elements in the array.
//
Array.Clear(integerArray, 0, integerArray.Length);
//
// Display the array
//
Console.WriteLine("--- Integer array after ---");
foreach (int value in integerArray)
{
Console.WriteLine(value);
}
}
}
Output
--- Integer array before ---
4
6
8
1
3
--- Integer array after ---
0
0
0
0
0
Here: The first argument to Array.Clear is the target array to clear. And the second specifies the offset to start clearing at.
Tip: The third argument uses Math.Min to clear either 2 elements or all elements, whichever is smaller.
Also: If you clear 2 elements and the array has one element, you will get an error. Math.Min prevents this exception.
Math.Max, MinC# program that clears object array
using System;
class Program
{
class Employee
{
public string Name { get; set; }
public int Salary { get; set; }
}
static void Main()
{
Employee[] employees = new Employee[3];
employees[0] = new Employee() { Name = "Bob", Salary = 10000 };
employees[1] = new Employee() { Name = "Susan", Salary = 13000 };
employees[2] = new Employee() { Name = "John", Salary = 20000 };
//
// Display the employee array.
//
Console.WriteLine("--- Employee array before ---");
foreach (Employee employee in employees)
{
Console.Write(employee.Name);
Console.Write(": ");
Console.WriteLine(employee.Salary);
}
//
// Clear first two elements in employee array.
//
Array.Clear(employees, 0, Math.Min(2, employees.Length));
//
// Display the employee array.
//
Console.WriteLine("--- Employee array after ---");
foreach (Employee employee in employees)
{
if (employee != null)
{
Console.Write(employee.Name);
Console.Write(": ");
Console.WriteLine(employee.Salary);
}
else
{
Console.WriteLine("null");
}
}
}
}
Output
--- Employee array before ---
Bob: 10000
Susan: 13000
John: 20000
--- Employee array after ---
null
null
John: 20000
Version 1: The Array.Clear method is invoked. It clears the 128-element int array that was allocated as a local variable.
Version 2: Here we use a for-loop and assign each element to zero. We time this version and the previous version.
Result: The for-loop version may be faster because we use 0 directly, instead of a "default" value.
C# program that benchmarks Array.Clear
using System;
using System.Diagnostics;
class Program
{
const int _max = 10000;
static void Main()
{
int[] array = new int[128];
// Version 1: clear with Array.Clear.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
Array.Clear(array, 0, array.Length);
}
s1.Stop();
// Version 2: clear with for-loop.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
for (int z = 0; z < array.Length; z++)
{
array[z] = 0;
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
}
}
Output
280.49 ns Array.Clear
45.23 ns for
Note: All struct fields will be set to null or 0, depending on their type. The default value can be found with the default operator.
StructDefault