
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Remove all elements from the ArrayList in C#
To remove all elements from ArrayList, the code is as follows −
Example
using System; using System.Collections; public class Demo { public static void Main(String[] args){ ArrayList list1 = new ArrayList(); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D"); list1.Add("E"); list1.Add("F"); list1.Add("G"); list1.Add("H"); list1.Add("I"); Console.WriteLine("Elements in ArrayList1..."); foreach (string res in list1){ Console.WriteLine(res); } ArrayList list2 = new ArrayList(); list2.Add("A"); list2.Add("B"); list2.Add("C"); list2.Add("D"); list2.Add("E"); list2.Add("F"); list2.Add("G"); list2.Add("H"); list2.Add("I"); Console.WriteLine("Elements in ArrayList2..."); foreach (string res in list2){ Console.WriteLine(res); } ArrayList list3 = new ArrayList(); list3 = list2; Console.WriteLine("Is ArrayList3 equal to ArrayList2? = "+list3.Equals(list2)); Console.WriteLine("Count of elements in ArrayList2 = " + list2.Count); list2.Clear(); Console.WriteLine("Count of elements in ArrayList2 (Updated) = " + list2.Count); } }
Output
This will produce the following output −
Elements in ArrayList1... A B C D E F G H I Elements in ArrayList2... A B C D E F G H I Is ArrayList3 equal to ArrayList2? = True Count of elements in ArrayList2 = 9 Count of elements in ArrayList2 (Updated) = 0
Example
Let us see another example −
using System; using System.Collections; public class Demo { public static void Main(String[] args){ ArrayList arrList = new ArrayList(); arrList.Add("One"); arrList.Add("Two"); arrList.Add("Three"); arrList.Add("Four"); arrList.Add("Five"); Console.WriteLine("Elements in ArrayList..."); foreach (string res in arrList){ Console.WriteLine(res); } Console.WriteLine("Count of elements in ArrayList = " + arrList.Count); arrList.Clear(); Console.WriteLine("Count of elements in ArrayList (Updated) = " + arrList.Count); } }
Output
This will produce the following output −
Elements in ArrayList... One Two Three Four Five Count of elements in ArrayList = 5 Count of elements in ArrayList (Updated) = 0
- Related Articles
- Remove all elements from the ArrayList in Java
- Remove all the elements from an ArrayList that are in another Collection in Java
- How to remove all elements of ArrayList in Java?
- Remove a range of elements from the ArrayList in C#
- Java Program to Remove duplicate elements from ArrayList
- Remove all elements from the SortedSet in C#
- Remove all elements from the Collection in C#
- Remove all elements from the Hashtable in C#
- How to remove the redundant elements from an ArrayList object in java?
- Remove all elements from TreeSet in Java
- Remove all elements from OrderedDictionary in C#
- Remove all elements from Java NavigableMap
- Remove all elements from Java LinkedHashSet
- Remove all elements from a HashSet in Java
- Remove all elements from a HashSet in C#

Advertisements