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.MakeRelativeUri(Uri) Method
- Array.GetValue() Method in C# with Examples | Set - 1
- C# | CharEnumerator.MoveNext() Method
- C# | Math.IEEERemainder() Method
- C# | Substring() Method
- C# | IndexOfAny() Method
- C# | CharEnumerator.GetType() Method
- File.GetLastWriteTimeUtc() Method in C# with Examples
- MathF.Sin() Method in C# with Examples
- Double.CompareTo Method in C# with Examples
- C# | Graphics.DrawLine() Method | Set - 1
- UInt16.GetHashCode Method in C# with Examples
- Stack.Peek Method in C#
- C# | IsNullOrEmpty() Method
- C# | Math.Sign() Method
- Int64.CompareTo Method in C# with Examples
- C# | PadRight() Method
- How to use Array.BinarySearch() Method in C# | Set -1
- C# | EndsWith() 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 firstname.lastname@example.org. 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.
Improved By : shubham_singh