
- 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
How to create a shallow copy of ArrayList in C#?
To create a shallow copy of ArrayList in C#, the code is as follows −
Example
using System; using System.Collections; public class Demo { public static void Main(){ ArrayList list = new ArrayList(); list.Add("One"); list.Add("Two"); list.Add("Three"); list.Add("Four"); list.Add("Five"); list.Add("Six"); list.Add("Seven"); list.Add("Eight"); Console.WriteLine("ArrayList elements..."); foreach(string str in list){ Console.WriteLine(str); } Console.WriteLine("ArrayList is read-only? = "+list.IsReadOnly); Console.WriteLine("Does the element Six in the ArrayList? = "+list.Contains("Six")); list.Insert(4, "Twelve"); Console.WriteLine("
ArrayList elements...UPDATED"); foreach(string str in list){ Console.WriteLine(str); } ArrayList list2 = new ArrayList(); list2 = (ArrayList)list.Clone(); Console.WriteLine("
Cloned ArrayList..."); foreach(string str in list2){ Console.WriteLine(str); } } }
Output
This will produce the following output −
ArrayList elements... One Two Three Four Five Six Seven Eight ArrayList is read-only? = False Does the element Six in the ArrayList? = True ArrayList elements...UPDATED One Two Three Four Twelve Five Six Seven Eight Cloned ArrayList... One Two Three Four Twelve Five Six Seven Eight
Example
Let us now see another 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"); list2.Add("G"); list2.Add("I"); Console.WriteLine("Elements in ArrayList2..."); foreach (string res in list2){ Console.WriteLine(res); } Console.WriteLine("Count of elements in ArrayList2 = " + list2.Count); list2.Remove("G"); Console.WriteLine("Elements in ArrayList2... (UPDATED)"); foreach (string res in list2){ Console.WriteLine(res); } Console.WriteLine("Count of elements in ArrayList2 (Updated) = " + list2.Count); ArrayList list3 = new ArrayList(); list3 = (ArrayList)list2.Clone(); Console.WriteLine("
Cloned ArrayList from ArrayList2..."); foreach(string str in list3){ Console.WriteLine(str); } } }
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 G I Count of elements in ArrayList2 = 11 Elements in ArrayList2... (UPDATED) A B C D E F H I G I Count of elements in ArrayList2 (Updated) = 10 Cloned ArrayList from ArrayList2... A B C D E F H I G I
- Related Articles
- How to create a shallow copy of Hashtable in C#?
- How to create a shallow copy of the BitArray in C#?
- How to create a shallow copy of SortedList Object in C#?
- How to shallow copy the objects in JavaScript?
- Deep Copy and Shallow Copy in Java
- Return a shallow copy of IdentityHashMap in Java
- What is Shallow Copy and how it is different from Deep Copy in C#?
- Copy - Shallow and deep copy operations in Python
- How do you make a shallow copy of a list in Java?
- How to copy the entire ArrayList to a one-dimensional Array in C# ?
- How to copy or clone a Java ArrayList?
- How to copy elements of ArrayList to Java Vector
- Python Shallow and Deep Copy operations
- What is the difference between shallow copy and deep copy in Java?
- Copy the elements of collection over a range of elements in ArrayList in C#

Advertisements