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

📅  最后修改于: 2021-05-29 13:55:16             🧑  作者: Mango

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

句法:

public object Clone ();

例子:

// C# code to illustrate the use 
// of BitArray.Clone Method
using System;
using System.Collections;
  
public class GFG {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an empty BitArray
        BitArray bit1 = new BitArray(4);
  
        // Initializing values in bit1
        bit1[0] = false;
        bit1[1] = false;
        bit1[2] = true;
        bit1[3] = true;
  
        // Displaying the list
        Console.WriteLine("Elements of Original BitArray: \n");
          
        // calling function
        Result(bit1);
  
        // using Clone() method
        BitArray bit2 = (BitArray)bit1.Clone();
  
        // Displaying the Cloned BitArray
        Console.WriteLine("\nElements of Cloned BitArray: \n");
          
        // calling function
        Result(bit2);
          
          
        // checking for the equality  
        // of References bit1 and bit2 
        Console.WriteLine("\nReference Equals: {0}", 
          Object.ReferenceEquals(bit1, bit2)); 
  
          
    }
      
// method to display the values
public static void Result(IEnumerable bit) 
{ 
    // This method prints all the 
    // elements in the BitArray. 
    foreach(Object obj in bit) 
        Console.WriteLine(obj); 
} 
}
输出:
Elements of Original BitArray: 

False
False
True
True

Elements of Cloned BitArray: 

False
False
True
True

Reference Equals: False