重要的问题 [英] Important refs question

查看:67
本文介绍了重要的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


执行以下操作时,它是复制到

数组的副本还是引用?


SqlCommand cmd = new SqlCommand(" w",x);

myArrayList.Add(cmd); //参考

或副本?

我问的原因是我想继续使用相同的变量名

创建一个新的cmd,然后将其复制到数组中


例如:

SqlCommand cmd = new SqlCommand(" y" ,z);

myArrayList.Add(cmd); //参考

或副本?

这可能吗?它实际上似乎工作但我不认为它应该。


我应该在将对象放入数组列表之前复制该对象

非常感谢


Simon

Hi all,

When executing the following, is it a copy or a reference thats copied to
the array?

SqlCommand cmd = new SqlCommand("w", x);
myArrayList.Add(cmd); // A reference
or a copy?
The reason i ask is that I then want to go on and use the same variable name
to make a new cmd and then copy that to the array as well

eg:

SqlCommand cmd = new SqlCommand("y", z);
myArrayList.Add(cmd); // A reference
or a copy?
Is this possible? It does actually seem to work but I don''t think it should.

Should I make a copy of the object before putting it in the array list

Many thanks

Simon

推荐答案

西蒙


在这种情况下它是引用。

一般规则是所有非值类型都是引用,值类型是

它们究竟是什么 - 值。


所以使用这样的代码是绝对安全的:


for(...){

SqlCommand cmd = new SqlCommand(...);

myList.Add(cmd);

}


你可以获取更多信息
http://msdn.microsoft.com/library/de...rencetypes.asp


HTH

Alex


" Simon Harvey" < SI ********** @ the-web-works.co.uk>在消息中写道

news:ev ************** @ tk2msftngp13.phx.gbl ...
Hi, Simon

in this case it is reference.
General rule is that all non-value types are references and value types are
exactly what they are - values.

So it is absolutely safe to use code like this:

for (...) {
SqlCommand cmd = new SqlCommand(...);
myList.Add(cmd);
}

You can get more information at
http://msdn.microsoft.com/library/de...rencetypes.asp

HTH
Alex

"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
大家好,
SqlCommand cmd = new SqlCommand(" w",x);
myArrayList.Add(cmd); //一个
引用或副本?

我问的原因是我想继续使用相同的变量
名来制作一个新的cmd,然后复制那个数组也是如此:

SqlCommand cmd = new SqlCommand(" y",z);
myArrayList.Add(cmd); // A
参考或副本?

这可能吗?它实际上似乎工作,但我不认为它应该是

我应该在将对象放入数组列表之前复制该对象

非常感谢<西蒙
Hi all,

When executing the following, is it a copy or a reference thats copied to
the array?

SqlCommand cmd = new SqlCommand("w", x);
myArrayList.Add(cmd); // A reference or a copy?
The reason i ask is that I then want to go on and use the same variable name to make a new cmd and then copy that to the array as well

eg:

SqlCommand cmd = new SqlCommand("y", z);
myArrayList.Add(cmd); // A reference or a copy?
Is this possible? It does actually seem to work but I don''t think it should.
Should I make a copy of the object before putting it in the array list

Many thanks

Simon



您好Alex,


感谢您的回复。


我原本认为它们被引用会引起问题。


例如,假设您向列表中添加多个命令,重复

更新我在OP中演示的相同变量名。


当你来访问命令时,引用只会指向

最后一个对象。其余的都没有参考它们可能是垃圾

随时收集


这不是这种情况


再次感谢


Simon
Hi Alex,

Thanks for your reply.

I would have thought that them being references would cause a problem.

For example, lets say you add multiple commands to a list, repeatedly
updating the same variable name as I demonstrated in the OP.

When you come to access the command, the references will only point to the
last object. The rest will have no references to them and may be garbage
collected at any moment

Is this not the case

Thanks again

Simon


Simon Harvey< si ********** @ the -web-works.co.uk>写道:
Simon Harvey <si**********@the-web-works.co.uk> wrote:
执行以下操作时,它是复制到数组的副本还是引用?

SqlCommand cmd = new SqlCommand(" w" ,x);
myArrayList.Add(cmd); //参考
或副本?
When executing the following, is it a copy or a reference thats copied to
the array?

SqlCommand cmd = new SqlCommand("w", x);
myArrayList.Add(cmd); // A reference
or a copy?




这是参考文献的副本。


http://www.pobox.com/~skeet/csharp/ parameters.html 获取有关

参数传递和参考/值类型的信息。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www .pobox.com / ~siget

如果回复小组,请不要给我发邮件



It''s a copy of the reference.

See http://www.pobox.com/~skeet/csharp/parameters.html for info on
parameter passing and reference/value types.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于重要的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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