In the following example explains how to clear or empty an array in C# using Array.Clear()
method.
using System;
namespace EmptyArray
{
class Program
{
static void Main()
{
int[] arr = new int[] { 100, 200, 300, 400, 500 };
Console.WriteLine("Array:");
Console.WriteLine("------------------");
foreach (var item in arr)
{
Console.WriteLine(item);
}
Console.WriteLine("------------------");
// Clear array values
Array.Clear(arr, 0, arr.Length);
Console.WriteLine("Array after clear:");
Console.WriteLine("------------------");
foreach (var item in arr)
{
Console.WriteLine(item);
}
Console.WriteLine("------------------");
Console.ReadKey();
}
}
}
Output:
Array:
------------------
100
200
300
400
500
------------------
Array after clear:
------------------
0
0
0
0
0
------------------
Comments (0)