Sabari M Sabari M
Updated date Jan 11, 2023
In this code snippet, We will see how to clear or empty an empty array in C#

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

Sabari M
Sabari M
Software Professional, India

IT professional with 15+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

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