📜  如何在C#中创建ArrayList的浅表副本

📅  最后修改于: 2021-05-29 18:57:07             🧑  作者: Mango

ArrayList.Clone()方法用于创建指定ArrayList的浅表副本。集合的浅表副本仅复制集合的元素,而与引用类型或值类型无关。但是它不会复制引用所引用的对象。新集合中的引用指向的对象与原始集合中的引用指向的对象相同。

句法:

public virtual object Clone ();

下面的程序说明了上面讨论的方法的使用:

范例1:

// 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);
        }
    }
}
输出:
Elements of Original ArrayList: 

Geeks
for
Geeks
10
20

Elements of Cloned ArrayList: 

Geeks
for
Geeks
10
20

范例2:

// 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); 
} 
}
输出:
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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.arraylist.clone?view=netframework-4.7.2