110,580
社区成员
发帖
与我相关
我的任务
分享
namespace Copy_string
{
class Test_instance
{
public String data;
public Test_instance(string newData)
{
data = newData;
}
public object GetCopy()
{
return MemberwiseClone();
}
}
class Program
{
static void Main(string[] args)
{
Test_instance source = new Test_instance("dage");
Test_instance target = (Test_instance)source.GetCopy();
Console.WriteLine(target.data);
source.data = "xiaodi";
Console.WriteLine(target.data);
Console.WriteLine("sucess...");
Console.ReadKey();
}
}
}
Test_instance source = new Test_instance("dage");
//Test_instance target = (Test_instance)source.GetCopy();
Test_instance target = source;
Console.WriteLine(target.data);
source.data = "xiaodi";
Console.WriteLine(target.data);
Console.WriteLine("sucess...");
Console.ReadKey();
static void Main(string[] args)
{
Test_instance source = new Test_instance("dage");
// 此时,source.data 指向"dage"
Test_instance target = (Test_instance)source.GetCopy();
// 此时,source.data 指向"dage" target.data 指向"dage"
Console.WriteLine(target.data);
source.data = "xiaodi";
// 此时,source.data 指向"xiaodi" target.data 指向"dage"
Console.WriteLine(target.data);
Console.WriteLine("sucess...");
Console.ReadKey();
}