
- 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
Creating an ArrayList having specified initial capacity in C#
To create an ArrayList having specified initial capacity, the code is as follows −
Example
using System; using System.Collections; public class Demo { public static void Main(String[] args) { ArrayList list1 = new ArrayList(5); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D"); Console.WriteLine("Capacity in ArrayList1 = "+list1.Capacity); Console.WriteLine("Elements in ArrayList1..."); foreach (string res in list1) { Console.WriteLine(res); } ArrayList list2 = new ArrayList(10); 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("Capacity in ArrayList2 = "+list2.Capacity); Console.WriteLine("Elements in ArrayList2..."); foreach (string res in list2) { Console.WriteLine(res); } Console.WriteLine("Is ArrayList1 equal to ArrayList2? = "+list1.Equals(list2)); list2.RemoveRange(3, 2); Console.WriteLine("Elements in ArrayList2 after removing elements in a range..."); foreach (string res in list2) { Console.WriteLine(res); } Console.WriteLine("Capacity in ArrayList2 = "+list2.Capacity); } }
Output
This will produce the following output −
Capacity in ArrayList1 = 5 Elements in ArrayList1... A B C D Capacity in ArrayList2 = 10 Elements in ArrayList2... A B C D E F G H I Is ArrayList1 equal to ArrayList2? = False Elements in ArrayList2 after removing elements in a range... A B C F G H I Capacity in ArrayList2 = 10
Example
Let us see another example −
using System; using System.Collections; public class Demo { public static void Main(String[] args) { ArrayList list = new ArrayList(5); list.Add("A"); list.Add("B"); list.Add("C"); list.Add("D"); Console.WriteLine("Capacity in ArrayList = "+list.Capacity); Console.WriteLine("Count of elements in ArrayList = "+list.Count); Console.WriteLine("Elements in ArrayList..."); foreach (string res in list) { Console.WriteLine(res); } list.Clear(); Console.WriteLine("Capacity in ArrayList = "+list.Capacity); Console.WriteLine("Count of elements in ArrayList = "+list.Count); } }
Output
This will produce the following output −
Capacity in ArrayList = 5 Count of elements in ArrayList = 4 Elements in ArrayList... A B C D Capacity in ArrayList = 5 Count of elements in ArrayList = 0
- Related Articles
- Creating StringBuilder having specified capacity in C#
- Creating a HybridDictionary with specified initial size in C#
- Creating a Case-Sensitive HybridDictionary with specified initial size in C#
- What is the Capacity property of an ArrayList class in C#?
- Add an element to specified index of ArrayList in Java
- Creating an empty HybridDictionary with specified case sensitivity in C#
- Insert an element into the ArrayList at the specified index in C#
- How to set minimum capacity for arraylist Listview in Android?
- Creating a synchronized wrapper for the ArrayList in C#
- Creating a read-only wrapper for the ArrayList in C#
- Get the unmodifiable view of the specified ArrayList in Java
- Set the capacity to the actual number of elements in the ArrayList in C#?
- Remove the element at the specified index of the ArrayList in C#
- Get or set the element at the specified index in ArrayList in C#
- Replace all occurrences of specified element of ArrayList with Java Collections

Advertisements