📜  如何在C#中创建SortedList对象的浅表副本

📅  最后修改于: 2021-05-29 14:50:52             🧑  作者: Mango

SortedList.Clone()方法用于创建SortedList对象的浅表副本。

句法:

public virtual object Clone();

返回值:返回SortedList对象的浅表副本。返回值的类型将为Object

注意:集合的浅表副本仅复制集合的元素,无论它们是引用类型还是值类型,但不复制引用所引用的对象。新集合中的引用指向的对象与原始集合中的引用指向的对象相同。

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

范例1:

// C# code to copy the 
// contents of one SortedList 
// to another SortedList. 
using System; 
using System.Collections; 
  
class Geeks { 
  
    // Main Method 
    public static void Main(String[] args) 
    { 
            // Creating a SortedList 
            SortedList mySL = new SortedList();
  
            // Adding elements to SortedList 
            mySL.Add(1, "C");
            mySL.Add(2, "C++");
            mySL.Add(3, "Java");
            mySL.Add(4, "C#");
              
  
            // Creating a shallow copy of mySL
            // we need to cast it explicitly
            SortedList myNewSL = (SortedList)mySL.Clone();
  
            // displaying the elements of mySL
            Console.WriteLine("Elements of mySL: ");
              
            for (int i = 0; i < mySL.Count; i++)
            { 
                Console.WriteLine("{0}:\t{1}", mySL.GetKey(i), 
                                          mySL.GetByIndex(i)); 
            }
                                        
            // displaying the elements of myNewSL
            Console.WriteLine("\nElements of myNewSL: ");
              
            for (int i = 0; i < myNewSL.Count; i++) 
            { 
                Console.WriteLine("{0}:\t{1}", myNewSL.GetKey(i), 
                                          myNewSL.GetByIndex(i)); 
              
            } 
          
    } 
} 
输出:
Elements of mySL: 
1:    C
2:    C++
3:    Java
4:    C#

Elements of myNewSL: 
1:    C
2:    C++
3:    Java
4:    C#

范例2:

// C# code to copy the 
// contents of one SortedList 
// to another SortedList. 
using System; 
using System.Collections; 
  
class Geeks { 
  
    // Main Method 
    public static void Main(String[] args) 
    { 
            // Creating a SortedList 
            SortedList mySL = new SortedList();
  
            // Adding elements to SortedList 
            mySL.Add(1, "HTML");
            mySL.Add(2, "CSS");
            mySL.Add(3, "PHP");
            mySL.Add(4, "DBMS");
              
  
            // Creating a shallow copy of mySL
            // we need to cast it explicitly
            SortedList myNewSL = (SortedList)mySL.Clone();
              
            // checking for the equality 
            // of References mySL and myNewSL
            Console.WriteLine("Reference Equals: {0}", 
               Object.ReferenceEquals(mySL, myNewSL));
                 
            // displaying the elements of mySL
            Console.WriteLine("Elements of mySL: ");
               
            for (int i = 0; i < mySL.Count; i++)
            { 
                Console.WriteLine("{0}:\t{1}", mySL.GetKey(i), 
                                          mySL.GetByIndex(i)); 
            }
                                         
            // displaying the elements of myNewSL
            Console.WriteLine("\nElements of myNewSL: ");
               
            for (int i = 0; i < myNewSL.Count; i++) 
            { 
                Console.WriteLine("{0}:\t{1}", myNewSL.GetKey(i), 
                                          myNewSL.GetByIndex(i)); 
               
            } 
              
            // Replaces the values at 
            // index 1 of mySL
            mySL.SetByIndex(1, "C#"); 
              
            Console.WriteLine("\nAfter Replaces Elements in mySL\n");
              
            // displaying the elements of myNewSL
            Console.WriteLine("\nElements of myNewSL: ");
               
            for (int i = 0; i < myNewSL.Count; i++) 
            { 
                Console.WriteLine("{0}:\t{1}", myNewSL.GetKey(i), 
                                          myNewSL.GetByIndex(i)); 
               
            } 
              
            // displaying the elements of mySL
            Console.WriteLine("\nElements of mySL: ");
               
            for (int i = 0; i < mySL.Count; i++)
            { 
                Console.WriteLine("{0}:\t{1}", mySL.GetKey(i), 
                                          mySL.GetByIndex(i)); 
            }
              
          
    } 
} 
输出:
Reference Equals: False
Elements of mySL: 
1:    HTML
2:    CSS
3:    PHP
4:    DBMS

Elements of myNewSL: 
1:    HTML
2:    CSS
3:    PHP
4:    DBMS

After Replaces Elements in mySL


Elements of myNewSL: 
1:    HTML
2:    CSS
3:    PHP
4:    DBMS

Elements of mySL: 
1:    HTML
2:    C#
3:    PHP
4:    DBMS

参考:

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