if your class implements ICloneable Interface, you can tryC a = new C();
C b = (C)a.Clone();

解决方案 »

  1.   

    otherwise, you could always write a copy constructor for your class
      

  2.   

    here is an example:using System;class Copy : ICloneable
    {
      int m_nValue;
      public Copy(int nValue)
      {
    m_nValue = nValue;
      }
     
      public object Clone()
      {
         Copy c = new Copy(this.m_nValue);
         return c;
      }  public void Show()
      {
    Console.WriteLine(m_nValue);
      }
    }class CopyTest
    {
      int m_nValue;
      public CopyTest(int nValue)
      {
    m_nValue = nValue;
      }
      
      public CopyTest(CopyTest ct)
      {
    m_nValue = ct.m_nValue ;
      }  public void Show()
      {
    Console.WriteLine(m_nValue);
      }  public static void Main()
      { Console.WriteLine("Using Clone:"); Copy c = new Copy(789);
    c.Show(); Copy c2 = (Copy)c.Clone();
    c2.Show(); Console.WriteLine("Using Copy constructor:"); CopyTest ct = new CopyTest(456);
    ct.Show(); CopyTest  ct2 = new CopyTest(ct);
    ct2.Show();
      }
    }