将长字符串作为参数传递(通过ref和value) [英] Passing long Strings as parameters (by ref and by value)

查看:65
本文介绍了将长字符串作为参数传递(通过ref和value)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有长字符串(例如1MB或2MB),通过引用方法或值传递

那些更高效?

解决方案

6月19日,5:30 * pm,Sujeet< Suj ... @ discussion.microsoft.comwrote:


如果有的话是长字符串(如1MB或2MB)通过ref方法或值传递

那些更高效?



String是一个引用类型,因此按值传递的字符串参数是

仍然只需要引用的大小(即4或8

字节)。


http://pobox.com/~skeet/csharp/parameters.html

Jon


Sujeet,C#和VB中的字符串对象是引用类型对象。它们是在堆上分配的
,并且您的变量包含对堆上实际的

字符串数据的引用。因此当你将一个任意长度的字符串传递给一个方法时,你总是会传递一个指针。 (可以这么说)实际的字符串数据,

而不是实际的字符串数据本身,无论你是通过

值还是通过引用传递它。


因此传递指针的性能影响通过引用

与值的比较小,如果实际的

字符串数据在堆栈上传递的话。当您通过值传递时,

编译的代码仅发送指针。关闭被叫方法和

忘了它。当你通过引用传递时,编译的代码发送

指针。 off到被调用的方法,并且在方法完成其处理后,(可能更新的)值返回到调用方法的

变量。所以这可能仅仅是一两条IL指令的问题。


相反,你应该主要考虑被调用的方法是否有
任何改变指针的理由在

处理期间指向不同的字符串。如果没有,请按值传递。如果是这样,通过引用传递它,但是调用方法中的
文档被调用的方法可能会改变

变量的内容指向不同的字符串。


对于其他语言,性能差异可能非常大。但对于C#

和VB,我们获得免费通行证。


" performant" - jeez,我讨厌这个词。即便是那个在很久以前创造了很长时间的人,后来也开始采取防御措施了。希望我记得他的名字。我在

..Net的早期,曾经看过他在VBITS会议上发言。
。他曾在CLR工作过。


HTH,

Tom Dacon

Dacon Software Consulting

" ; Sujeet" < Su **** @ discuss.microsoft.com写信息

新闻:AA ************************ ********** @ microsof t.com ...


如果有长字符串(如1MB或2MB),它的性能更高通过参考方法或价值来传递

那些?



其他海报已经回答了这个问题 - 但只是补充一点,这个

仅适用于单一海报AppDomain中;在不同的AppDomains(远程处理/ IPC)或进程(IPC)或

COM-interop或通过网络进行交谈时,所有的赌注都是关闭的。可能很明显,但值得注意。


Marc


If there are long strings (like 1MB or 2MB) is it more performant to pass
those by ref to methods or by value?

解决方案

On Jun 19, 5:30*pm, Sujeet <Suj...@discussions.microsoft.comwrote:

If there are long strings (like 1MB or 2MB) is it more performant to pass
those by ref to methods or by value?

String is a reference type, so a string parameter passed by value is
still only going to require the size of a reference (i.e. 4 or 8
bytes).

See http://pobox.com/~skeet/csharp/parameters.html

Jon


Sujeet, string objects in C# and VB are reference-type objects. They''re
allocated on the heap, and your variable contains a reference to the actual
string data on the heap. So when you pass a string of any length to a method
you''re always passing a "pointer" (so to speak) to the actual string data,
rather than the actual string data itself, no matter whether you pass it by
value or by reference.

So the performance implications of passing that "pointer" by reference
versus by value are small compared to what would be the cost if the actual
string data were being passed on the stack. When you pass by value, the
compiled code merely sends the "pointer" off to the called method and
forgets about it. When you pass by reference, the compiled code sends the
"pointer" off to the called method, and after the method has completed its
processing the (possibly updated) value is returned to the calling method''s
variable. So it''s just a matter of one or two IL instructions, probably.

Instead, you should primarily consider whether or not the called method has
any reason to change the "pointer" to point to a different string during its
processing. If not, pass it by value. If so, pass it by reference, but
document in the calling method that the called method can potentially change
the variable''s contents to point to a different string.

For other languages the performance differences can be extreme. But for C#
and VB we get a free pass.

"performant" - jeez, I hate that word. Even the guy who coined it a long
time ago later got defensive about it. Wish I could remember his name. I
used to see him speak at the VBITS conferences back in the early days of
..Net. He worked on the CLR.

HTH,
Tom Dacon
Dacon Software Consulting
"Sujeet" <Su****@discussions.microsoft.comwrote in message
news:AA**********************************@microsof t.com...

If there are long strings (like 1MB or 2MB) is it more performant to pass
those by ref to methods or by value?



The other posters have answered this fine - but just to add that this
only applies within a single AppDomain; all bets are off when talking
between different AppDomains (remoting / IPC) or processes (IPC) or
COM-interop or over the network. Maybe obvious, but worth noting.

Marc


这篇关于将长字符串作为参数传递(通过ref和value)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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