How to create a shallow copy of ArrayList in C# (original) (raw)
Last Updated : 07 Feb, 2019
ArrayList.Clone() Method is used to create a shallow copy of the specified ArrayList. A shallow copy of a collection copies only the elements of the collection irrespective of reference types or value types. But it does not copy the objects that the references refer to. The references in the new collection point to the same objects that the references in the original collection point to. Syntax:
public virtual object Clone ();
Below programs illustrate the use of above-discussed method:Example 1:
csharp `
// C# code to illustrate the use // of ArrayList.Clone Method using System; using System.Collections;
public class GFG {
// Main Method
public static void Main(String[] args)
{
// Creating an empty ArrayList
ArrayList list = new ArrayList();
// Use Add() method
// to Add elements
// in the list
list.Add("Geeks");
list.Add("for");
list.Add("Geeks");
list.Add("10");
list.Add("20");
// Displaying the list
Console.WriteLine("Elements of Original ArrayList: \n");
// Displaying the elements in ArrayList
foreach(string str in list)
{
Console.WriteLine(str);
}
// Creating another ArrayList
ArrayList sec_list = new ArrayList();
// using Clone() Method
sec_list = (ArrayList)list.Clone();
// Displaying the Cloned ArrayList
Console.WriteLine("\nElements of Cloned ArrayList: \n");
// Displaying the elements in ArrayList
foreach(string str1 in sec_list)
{
Console.WriteLine(str1);
}
}
}
`
Output:
Elements of Original ArrayList:
Geeks for Geeks 10 20
Elements of Cloned ArrayList:
Geeks for Geeks 10 20
Example 2:
csharp `
// C# code to illustrate the use // of ArrayList.Clone Method using System; using System.Collections;
public class GFG {
// Main Method
public static void Main(String[] args)
{
// Creating an empty ArrayList
ArrayList list = new ArrayList();
// Use Add() method
// to Add elements
// in the list
list.Add(10);
list.Add(20);
list.Add(30);
list.Add(40);
list.Add(50);
// Displaying the list
Console.WriteLine("Elements of Original ArrayList: \n");
// calling function
Result(list);
// using Clone() method
ArrayList sec_list = (ArrayList)list.Clone();
// Displaying the Cloned ArrayList
Console.WriteLine("\nElements of Cloned ArrayList: \n");
// calling function
Result(sec_list);
// adding a new value to
// original ArrayList
list.Add(60);
Console.WriteLine("\nAfter Adding, Original ArrayList: \n");
// Calling function
Result(list);
Console.WriteLine("\nAfter Adding, Cloned ArrayList: \n");
// Calling function
Result(sec_list);
// checking for the equality
// of References list and sec_list
Console.WriteLine("\nReference Equals: {0}",
Object.ReferenceEquals(list, sec_list));
}
// method to display the values public static void Result(ArrayList ar) { // This method prints all the // elements in the ArrayList. foreach(int i in ar) Console.WriteLine(i); } }
`
Output:
Elements of Original ArrayList:
10 20 30 40 50
Elements of Cloned ArrayList:
10 20 30 40 50
After Adding, Original ArrayList:
10 20 30 40 50 60
After Adding, Cloned ArrayList:
10 20 30 40 50
Reference Equals: False
Reference:
Similar Reads
- How to create a shallow copy of BitArray in C# BitArray.Clone() Method is used to create a shallow copy of the specified BitArray. A shallow copy of a collection copies only the elements of the collection irrespective of reference types or value types. But it does not copy the objects that the references refer to. The references in the new colle 2 min read
- C# | How to create a shallow copy of the BitArray BitArray.Clone Method is used to create a shallow copy of the BitArray. This method only copies the elements of the collection, whether they are reference types or value types, but it does not copy the objects that the references refer to.Syntax: public object Clone (); Example 1: Here we create an 3 min read
- How to create a shallow copy of Hashtable in C# Hashtable.Clone Method is used to create a shallow copy of the Hashtable. When we make a shallow copy, only the elements of the collection get copied irrespective of its type, it doesn't copy the objects referred to by the references. Basically, it creates a new object and that object points to the 3 min read
- How to create the ArrayList in C# ArrayList() constructor is used to initialize a new instance of the ArrayList class which will be empty and will have the default initial capacity. ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dyn 2 min read
- How to create a shallow copy of SortedList Object in C# SortedList.Clone() Method is used to create a shallow copy of a SortedList object. Syntax: public virtual object Clone(); Return Value: It returns a shallow copy of the SortedList object. The type of returned value will be Object. Note: A shallow copy of a collection copies only the elements of the 3 min read
- C# | Copying the elements of ArrayList to a new array ArrayList.ToArray Method is used to copy the elements of the ArrayList to a new array. This method contains two methods in its overload list as follows: ToArray()ToArray(Type)ToArray() This method is used to copy the elements of the ArrayList to a new Object array. The elements are copied using Arra 2 min read
- C# | How to copy the entire ArrayList to a one-dimensional Array ArrayList.CopyTo Method is used to copy the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array. Syntax: public virtual void CopyTo (Array array); Here, array is the one-dimensional Array which is the destination of the elements copied from ArrayList 3 min read
- C# | How to convert an ArrayList to Array In C#, an array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float etc. and the elements are stored in a contiguous location.ArrayList represen 4 min read
- C# | How to get Synchronize access to the ArrayList ArrayList.SyncRoot Property is used to get an object which can be used to synchronize access to the ArrayList. ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searc 2 min read
- Shallow Copy and Deep Copy in C# In general, when we try to copy one object to another object, both the objects will share the same memory address. Normally, we use assignment operator, = , to copy the reference, not the object except when there is value type field. This operator will always copy the reference, not the actual objec 5 min read