C#:如何正确克隆列表<&燃气轮机;继续

C#:如何正确克隆列表<&燃气轮机;继续,c#,list,generics,collections,clone,C#,List,Generics,Collections,Clone,我有两张单子,一张是原件,另一张是原件的复印件 List<Button> buttonList; // this is the original list List<Button> copyButtonList;// this is the copy of button list; this use to sort the list 我也尝试以下方法 copyButtonList = new List<Button>(butt

我有两张单子,一张是原件,另一张是原件的复印件

    List<Button> buttonList; // this is the original list
    List<Button> copyButtonList;// this is the copy of button list; this use to sort the list
我也尝试以下方法

        copyButtonList = new List<Button>(buttonList);
在那之后,我试着按如下方式打印这两个列表

foreach(var b in buttonList){
            msgList.Items.Add(b.Text);
        }
        foreach(var b in copyButtonList){
            msgList.Items.Add(b.Text);
        }
在上述三种情况下,两个列表都被排序:( 我只想对复制按钮列表进行排序,有人能指出我在这里犯的错误吗

更新:我的插入排序算法如下

 public static String insertionSort(List<Button> button)
    {
        String key;
        int i = 0;
        int j;
        String s = "";
        for (j = 1; j < button.Count; j++)
        {
            key = button.ElementAt(j).Text;
            i = j - 1;
            while (i >= 0 && int.Parse(button.ElementAt(i).Text) > int.Parse(key))
            {
                button.ElementAt(i + 1).Text = button.ElementAt(i).Text;

                i = i - 1;

            }
            button.ElementAt(i + 1).Text = key;
            if (i == -1)
            {

                s=(button.ElementAt(i + 1).Text + " is the starting Item, keep this in before " + button.ElementAt(i + 2).Text);

            }
            else if (i == j - 1)
            {
                s=(button.ElementAt(i + 1).Text + " is the last Item, keep this in after  " + button.ElementAt(i).Text);

            }
            else
            {
                s=(button.ElementAt(i + 1).Text + " is between " + button.ElementAt(i).Text + " and " + button.ElementAt(i + 2).Text);

            }

        }

        if (button.Count == 1)
        {
            s= ("This is the first Item");
        }
        return s;
    }
publicstaticstringinsertionsort(列表按钮)
{
字符串键;
int i=0;
int j;
字符串s=“”;
对于(j=1;j=0&&int.Parse(button.ElementAt(i.Text)>int.Parse(key))
{
button.ElementAt(i+1).Text=button.ElementAt(i).Text;
i=i-1;
}
button.ElementAt(i+1).Text=key;
如果(i==-1)
{
s=(button.ElementAt(i+1).Text+”是起始项,将其保留在“+button.ElementAt(i+2).Text”之前;
}
else如果(i==j-1)
{
s=(button.ElementAt(i+1).Text+”是最后一项,将其保留在“+button.ElementAt(i.Text)”之后;
}
其他的
{
s=(button.ElementAt(i+1).Text+”位于“+button.ElementAt(i).Text+”和“+button.ElementAt(i+2).Text”之间);
}
}
如果(button.Count==1)
{
s=(“这是第一项”);
}
返回s;
}

您可以使用上面提到的方法

List oldList=新列表();
List newList=新列表(oldList.Count);
oldList.ForEach((项目)=>
{
添加(新类型(项));
});

实际上,斯皮辛的问题并不愚蠢。 由于您没有更改按钮,因此不需要进行深度克隆。 你的问题可能在别的地方。 这里有一个例子

public class Customer
{
    public string Name { get; set; }

    public override string ToString()
    {
        return Name; 
    }
}

public class CustomerComparer : IComparer<Customer>
{
    public int Compare(Customer x, Customer y)
    {
        return x.Name.CompareTo(y.Name);
    }
}


void Print()
{

   List<Customer> l1 = new List<Customer>();
   l1.Add(new Customer() {  Name="aa"});
   l1.Add(new Customer() { Name = "cc" });
   l1.Add(new Customer() { Name = "bb" });

   List<Customer> l2 = new List<Customer>(l1);


   l2.Sort(new CustomerComparer());
   foreach (var item in l1)
        Console.WriteLine(item);


   Console.WriteLine();
   foreach (var item in l2)
        Console.WriteLine(item);

   Console.ReadLine();
我说的“改变按钮”是指像这样的线条

button.ElementAt(i + 1).Text = key;
您可能有两个列表,但它们都“点”/具有相同的对象。当您更改列表1中按钮的文本时,表示列表2中的对象也发生了更改

你需要了解什么是值类型,什么是引用类型以及它们之间的区别

在电脑上运行以下控制台应用程序,查看打印的内容

using System;

public struct Point
{
  public int X;
  public Point(int initialValue) { X = initialValue; }
}

class Program
{

  static void Main(string[] args)
  {

    Point point1 = new Point(5);

    Console.WriteLine("Before:" + point1.X);
    ChangePoint(point1);
    Console.WriteLine("After:" + point1.X);
    Console.ReadLine();
  }

  private static void ChangePoint(Point p)
  {
    p.X = 20;
  }
}
然后把单词“struct”改为“class”,看看打印的是什么


由于结构是值类型,而类是引用类型,因此得到的结果不同。

排序算法修改按钮,这两个列表中的按钮都是相同的。如果确实想这样做,则需要深度复制对象,即复制或复制每个按钮,而不仅仅是它们的引用


但是对列表进行排序的更好的解决方案是只对列表进行排序,即切换元素索引。(例如
按钮[i+1]=按钮[i]

列表的所有3种浅层克隆方法都是正确的。你的问题出在其他地方。@Athari是对的……你想对副本做什么?愚蠢的问题:你在排序之前验证了两个列表的排序顺序了吗?哦!另一个愚蠢的问题:你的
insertionSort
是如何实现的?你交换了文本而不是索引posi吗列表中的选项?@Andre我只想在不影响原始文件的情况下对副本进行排序。Alif OP正在处理基本内容,使用
ForEach
和lambdas而不是
ForEach
可能是个坏主意。这很好,但我仍然无法找出我的代码的问题您的问题是您正在更改InsertionOrthey中的按钮亲爱的,你能解释一下“你正在改变按钮”这一术语吗?在你的解释中,我想一次复制一个元素到另一个列表中,然后做一些操作,然后再添加另一个元素到该列表中,像wise一样做一些操作。我仍然不理解这里使用的深度复制
public class Customer
{
    public string Name { get; set; }

    public override string ToString()
    {
        return Name; 
    }
}

public class CustomerComparer : IComparer<Customer>
{
    public int Compare(Customer x, Customer y)
    {
        return x.Name.CompareTo(y.Name);
    }
}


void Print()
{

   List<Customer> l1 = new List<Customer>();
   l1.Add(new Customer() {  Name="aa"});
   l1.Add(new Customer() { Name = "cc" });
   l1.Add(new Customer() { Name = "bb" });

   List<Customer> l2 = new List<Customer>(l1);


   l2.Sort(new CustomerComparer());
   foreach (var item in l1)
        Console.WriteLine(item);


   Console.WriteLine();
   foreach (var item in l2)
        Console.WriteLine(item);

   Console.ReadLine();
using System.Linq;
copyButtonList = buttonList.Select(ee => new Button() {  Text= ee.Text}).ToList();
button.ElementAt(i + 1).Text = key;
using System;

public struct Point
{
  public int X;
  public Point(int initialValue) { X = initialValue; }
}

class Program
{

  static void Main(string[] args)
  {

    Point point1 = new Point(5);

    Console.WriteLine("Before:" + point1.X);
    ChangePoint(point1);
    Console.WriteLine("After:" + point1.X);
    Console.ReadLine();
  }

  private static void ChangePoint(Point p)
  {
    p.X = 20;
  }
}