InterviewSolution
Saved Bookmarks
| 1. |
How to empty an array in C#? |
|
Answer» An array can be emptied using the Array.Clear() method. This method sets the required range of the elements in the array to their default values where the array type may be integer, boolean, CLASS instances etc. To empty the whole array, the range of elements should simply be the length of the whole array. A program that demonstrates the Array.Clear() method to empty the array is given as follows: using SYSTEM; PUBLIC class Demo { public static void Main() { int[] arr = new int[] {3, 9, 7, 1, 6}; Console.Write("The original integer array is: "); foreach (int i in arr) { Console.Write(i + " "); } Array.Clear(arr, 0, arr.Length); Console.Write("\nThe modified integer array is: "); foreach (int i in arr) { Console.Write(i + " "); } } }The output of the above program is given as follows: The original integer array is: 3 9 7 1 6 The modified integer array is: 0 0 0 0 0 |
|