Clear an array


Jump to: navigation, search
C# Code Snippets

C# Source Code Bank

See also …
edit

This C# code snippet clears out all array values using the Clear method of the IList interface.

using System;
using System.Collections;
 
public class ClearArray
{
 
   public static void Main()
   {
      int[] ints = {1, 2, 4};
      Console.Write ("Before: ");
      foreach (int value in ints)
      {
         Console.Write ("{0} ", value);
      }
      Console.WriteLine();
      ClearList (ints);
      Console.Write ("After:  ");
      foreach (int value in ints)
      {
         Console.Write ("{0} ", value);
      }
      Console.WriteLine();
   }
      
   public static void ClearList (IList iList)
   {
      iList.Clear();
   }
}


 IList Clear example (program output)
Before: 1 2 4
After: 0 0 0


Personal tools