How to use the Clear method of array class in C#?

CsharpProgrammingServer Side Programming

The Array.Clear class in C# clears i.e.zeros out all elements.

In the below example, we have first considered an array with 3 elements.

int[] arr = new int[] { 11, 40, 20};

Now we have used the Array.clear method to zero out all the arrays.

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

Let us see an example of Array.clear method in c#.

Example

 Live Demo

using System;
class Program {
   static void Main() {
      int[] arr = new int[] {11, 40, 20};
      Console.WriteLine("Array (Old):");
      foreach (int val in arr) {
         Console.WriteLine(val);
      }
      Array.Clear(arr, 0, arr.Length);
      Console.WriteLine("Array (After using Clear):");
      foreach (int val in arr) {
         Console.WriteLine(val);
      }
   }
}

Output

Array (Old):
11
40
20
Array (After using Clear):
0
0
0
raja
Published on 18-Sep-2018 12:51:24
Advertisements