TechieClues TechieClues
Updated date Jan 11, 2023
In this code snippet, We will see how to clear or empty an empty array in C#
  • 1.8k
  • 0
  • 0

In the following example explains how to clear or empty an array in C# using Array.Clear() method.

using System;

namespace EmptyArray
{
    class Program
    {
        static void Main()
        {
            int[] arr = new int[] { 100, 200, 300, 400, 500 };
            Console.WriteLine("Array:");
            Console.WriteLine("------------------");

            foreach (var item in arr)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("------------------");

            // Clear array values
            Array.Clear(arr, 0, arr.Length);

            Console.WriteLine("Array after clear:");
            Console.WriteLine("------------------");
            foreach (var item in arr)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("------------------");
            Console.ReadKey();
        }
    }
}

Output:

Array:
------------------
100
200
300
400
500
------------------
Array after clear:
------------------
0
0
0
0
0
------------------

 

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!