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#
- C# | Uri.IsHexEncoding() Method
- Queue.Contains() Method in C#
- C# | Insert() Method
- C# | Replace() Method
- Anonymous Method in C#
- C# | IndexOfAny() Method
- Extension Method in C#
- C# | Uri.ToString() Method
- C# | ToLower() Method
- C# | StartsWith() Method
- C# | Math.Log() Method
- C# | Copy() Method
- C# | Remove() Method
- C# | ToUpper() 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.