Array - Clear
Sets a range of elements in the to zero, to false, or to null, depending on the element type.
public static void Main() { Array myIntArray = Array.CreateInstance(typeof(Int32), 8); myIntArray.SetValue(8, 0); myIntArray.SetValue(2, 1); myIntArray.SetValue(6, 2); myIntArray.SetValue(3, 3); myIntArray.SetValue(7, 4); myIntArray.SetValue(9, 5); myIntArray.SetValue(17, 6); myIntArray.SetValue(11, 7); // Displays the values of the Array. Console.WriteLine( "The Int32 array contains the following:" ); PrintValues(myIntArray); Console.WriteLine("myIntArray.Clear(2,4);"); // C# Extension Method: Array - Clear myIntArray.Clear(2,4); PrintValues(myIntArray); } public static void PrintValues(Array myArr) { int i = 0; int cols = myArr.GetLength(myArr.Rank - 1); foreach (object o in myArr) { if ( i < cols ) { i++; } else { Console.WriteLine(); i = 1; } Console.Write( "\t{0}", o); } Console.WriteLine(); }
View Source
using System; public static partial class Extensions { /// <summary> /// Sets a range of elements in the to zero, to false, or to null, depending on the element type. /// </summary> /// <param name="array">The whose elements need to be cleared.</param> /// <param name="index">The starting index of the range of elements to clear.</param> /// <param name="length">The number of elements to clear.</param> public static void Clear(this Array array, Int32 index, Int32 length) { Array.Clear(array, index, length); } }