我觉得MemberwiseClone是不必要的 ,何必一定要用MemberwiseClone方法呢?你们说呢打个比方我写一个程序你们看看:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delete
{
public class Content
{
public int Val;
}
public class Cloner
{
public Content MyContent = new Content();
public Cloner(int newVal)
{
MyContent.Val = newVal;
}
}
class Program
{
static void Main(string[] args)
{
Cloner a = new Cloner(3);
Cloner
b = a;//这里不一样也是浅度复制吗?何必一定要用MemberwiseClone方法呢
Console.WriteLine(b.MyContent.Val);
}
}
}
...
Cloner
b = a;//这里不一样也是浅度复制吗?何必一定要用MemberwiseClone方法呢
...
b = a;
不是浅度复制,是引用拷贝。
namespace delete
{
public class Content
{
public int Val;
}
public class Cloner
{
public Content MyContent = new Content();
public Cloner(int newVal)
{
MyContent.Val = newVal;
}
public Cloner ShallowClone()
{
return (Cloner)this.MemberwiseClone();
}
}
class Program
{
static void Main(string[] args)
{
Cloner a = new Cloner(3);
Cloner b = a;//这里不是浅度复制。
Cloner c = a.ShallowClone();
...
Cloner
b = a;//这里不一样也是浅度复制吗?何必一定要用MemberwiseClone方法呢
...
b = a;
不是浅度复制,是引用拷贝。
namespace delete
{
public class Content
{
public int Val;
}
public class Cloner
{
public Content MyContent = new Content();
public Cloner(int newVal)
{
MyContent.Val = newVal;
}
public Cloner ShallowClone()
{
return (Cloner)this.MemberwiseClone();
}
}
class Program
{
static void Main(string[] args)
{
Cloner a = new Cloner(3);
Cloner b = a;//这里不是浅度复制。
Cloner c = a.ShallowClone();