public class xcc : ICloneable
    {
        static IList<string> a = new List<string>();
        IList<string> b = new List<string>();
        public void test()
        {
            a.Add("a");
            xcc xcc = new xcc();
            xcc = ((xcc)Clone());
            b = xcc.a;
            a.Clear();
            foreach (string s in b)
            {
                Console.Write(s);
            }
            Console.ReadLine();
        }
        public object Clone()
        {            return this.MemberwiseClone();
        }
    }请指教。最好有代码,相关教程也行 感谢

解决方案 »

  1.   

    public class xcc : ICloneable
        {
            IList<string> a = new List<string>();
            IList<string> b = new List<string>();        public void test()
            {
                a.Add("a");
                 xcc xcc1 = ((xcc)Clone());
                b = xcc1.a;
                a.Clear();
                foreach (string s in b)
                {
                    Console.Write(s);
                }
                Console.ReadLine();
            }        public object Clone()
            {
                xcc x = new xcc();
                x.a = new List<string>();
                x.b = new List<string>();
                foreach (var item in a)
                {
                    x.a.Add((string)item.Clone());
                }
                foreach (var item in b)
                {
                    x.b.Add((string)item.Clone());
                }
                return x;
            }
        }
    你那个例子根本不是什么深拷贝,关于深拷贝和浅拷贝的知识建议你自己上网去查一下
      

  2.   

    有一点点疑问啊 。。我a是static的
      

  3.   

    明白了,你是要从静态的List a中拷贝数据到List b中吧 那根本没必要对整个类xcc实现ICloneable,因为string类型已经实现了这个接口,看看下面这个能不能解决你的疑问
    public class xcc
        {
            static IList<string> a = new List<string>();
            IList<string> b = new List<string>();        public void test()
            {
                a.Add("a");
                foreach (var item in a)
                {
                    b.Add((string)item.Clone());
                }
                a.Clear();
                foreach (string s in b)
                {
                    Console.Write(s);
                }
                Console.ReadLine();
            }
        }