This method is used to set a range of elements in an array to the default value of each element type.
public static void Clear (Array array, int index, int length);
array: It is an array whose elements need to be cleared.
index: It is the starting index of the range of elements to clear.
length: It is the number of elements to clear.
- ArgumentNullException: if array is null
- IndexOutOfRangeException: if the index is less than the lower bound of the array or the length is less than zero or the sum of index and length is greater than the size of the array.
Below are the examples to illustrate the Array.Clear() Method:
Array Before Operation: 10 20 30 40 Array After Operation: 10 0 0 40
Example 2: For ArgumentNullException
Try to clear the element from null Array: Exception Thrown: System.ArgumentNullException
Example 3: For IndexOutOfRangeException
Array Before Operation: 10 20 30 40 Taking index out of bound: Exception Thrown :System.IndexOutOfRangeException
- Difference between Method Overriding and Method Hiding in C#
- Stack.Contains() Method in C#
- Stack.Pop() Method in C#
- C# | ToUpper() Method
- C# | ToLower() Method
- C# | Math.Abs() Method | Set - 2
- C# | PadLeft() Method
- Queue.Contains() Method in C#
- C# | Method Overloading
- C# | Math.Max() Method
- C# | Math.Cos() Method
- C# | Math.Exp() Method
- C# | Math.Sin() Method
- C# | String.Contains() Method
- C# | IndexOfAny() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.