如何复制对象而不是geting引用 [英] How to copy an object instead of geting the reference

查看:78
本文介绍了如何复制对象而不是geting引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我想在用户点击

提交之后才更改对象。按钮,所以我首先新建一个对象并使用=为了得到内存中的对象

,我发现操作=只得到我要复制的

目标对象的引用,我该如何复制呢?

In the C# , I want to change the object only after the user click the
"Submit" button , so I first new an object and use the "=" to get the object
in memory , I found that the operation "=" only get a reference to the
target object which I want to copy , how can I do copy it ?

推荐答案

" DogEye" <ジ***** @ tom.com> ó???Yéì/ó???Yéìá×??×?ó??èóì????Y ??:news:eX ************** @ TK2MSFTNGP15.phx。 gbl ...
"DogEye" <ji*****@tom.com> ó???Yéì/ó???Yéìá × ??×?ó??è óì???àY??: news:eX**************@TK2MSFTNGP15.phx.gbl...
在C#中,我想在用户点击
提交后才更改对象。按钮,所以我首先新建一个对象并使用=为了让对象在内存中,我发现操作=只获取对我要复制的目标对象的引用,如何复制它?
In the C# , I want to change the object only after the user click the
"Submit" button , so I first new an object and use the "=" to get the object
in memory , I found that the operation "=" only get a reference to the
target object which I want to copy , how can I do copy it ?




如果对象类型支持复制(克隆)它实现了ICloneable

界面。


//开始

字符串s1 ="原始字符串" ;;

String s2 =(String)s1.Clone(); //这必须是原始字符串的副本

s1 ="另一个字符串" ;;

Console.WriteLine(s1);

Console.WriteLine(s2);

//结束


谢尔盖



If the object type supports copying (cloning) it implements the ICloneable
interface.

// begin
String s1 = "Original string";
String s2 = (String) s1.Clone(); // this must be a copy of the original string
s1 = "Another string";
Console.WriteLine(s1);
Console.WriteLine(s2);
// end

Sergei


DogEye写道:
在C#中,我想在用户点击
提交后才更改对象。按钮,所以我首先新建一个对象并使用=为了获得内存中的
对象,我发现操作=只得到我要复制的
目标对象的引用,我该如何复制呢?
In the C# , I want to change the object only after the user click the
"Submit" button , so I first new an object and use the "=" to get the object in memory , I found that the operation "=" only get a reference to the
target object which I want to copy , how can I do copy it ?




为什么要复制对象你刚刚创建?那么你会创建一个新的

对象并在内存中保留它的副本?你打算怎么处理

原始对象?



Why do you want to copy the object you just created ? So you''d create a new
object and keep a copy of that in memory ? What are you going to do with the
original objcet ?


1。这个例子完全是胡说八道,因为字符串是一个专门的对象

并且是不可变的,因此只是通过引用而言。另一个你实际上是b $ b的实例正在制作副本。因此......

string s1 =" two"

string s2 = s1;

s1 =" one";

System.Diagnostics.Debug.WriteLine(string.Format(" s1 {0},s2

{1}",s1,s2));


2. DogEye,虽然在本质上谢尔盖的复制对象的技术是正确的,但请注意,可以使用复制和克隆方法来获取

制作浅层和深层副本。不幸的是(我似乎记得)在框架中他们的行为有一点不一致 - 所以对于更复杂的

对象,你应该确保检查文档看看什么

功能你在假设之前就已经得到了。


Br,


马克。


谢尔盖 < SE **** @ nospam.summertime.mtu-net.ru>在消息中写道

news:OA ************** @ TK2MSFTNGP12.phx.gbl ...
1. This example is complete nonsense, since a string is a specialised object
and is immutable and therefore just by "referencing" another instance you
are effectively making a copy. See thus...
string s1 = "two";
string s2 = s1;
s1 = "one";
System.Diagnostics.Debug.WriteLine(string.Format(" s1 {0}, s2
{1}",s1,s2));

2. DogEye, Although in essence Sergei''s technique for copying an object is
correct, please note that available to you are Copy and Clone methods to
make shallow and deep copies. Unfortunately (I seem to remember) there is a
little inconsistancy of their actions in the Framework - so for more complex
objects you should make sure to check the documentation to see what
functionality you are getting before assuming it.

Br,

Mark.

"Sergei" <se****@nospam.summertime.mtu-net.ru> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl...
" DogEye" <ジ***** @ tom.com> ó???Yéì/ó???Yéìá×??×?ó??èóì???àY??:
新闻:eX ************** @ TK2MSFTNGP15.phx.gbl ...
"DogEye" <ji*****@tom.com> ó???Yéì/ó???Yéìá × ??×?ó??è óì???àY??:
news:eX**************@TK2MSFTNGP15.phx.gbl...
在C#中,我想在用户点击
提交后才更改对象。按钮,所以我首先新建一个对象并使用=为了得到内存中的
对象,我发现操作=只获取对我要复制的目标对象的引用,我该如何复制呢?
In the C# , I want to change the object only after the user click the
"Submit" button , so I first new an object and use the "=" to get the
object
in memory , I found that the operation "=" only get a reference to the
target object which I want to copy , how can I do copy it ?



如果对象类型支持复制(克隆),则实现ICloneable
接口。

//开始
String s1 =" Original string";
String s2 =(String)s1.Clone(); //这必须是原始
字符串的副本
s1 ="另一个字符串" ;;
Console.WriteLine(s1);
Console.WriteLine(s2); <

谢尔盖



If the object type supports copying (cloning) it implements the ICloneable
interface.

// begin
String s1 = "Original string";
String s2 = (String) s1.Clone(); // this must be a copy of the original
string
s1 = "Another string";
Console.WriteLine(s1);
Console.WriteLine(s2);
// end

Sergei



这篇关于如何复制对象而不是geting引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆