通过参考传递变量.或价值困境
问题说明
我有一些无效的静态方法.
I have some void methods which are static.
最好通过引用或按值传递变量,因为我要将大量文本传递给这些变量:
Is it better to pass variables by reference or by value, because I'm passing large amount of text into these variables :
public static void renderText(ref StringBuilder build)
{
//Do your job.
}
所以有人可以向我解释一下,当我发送StringBuilder
的引用时会发生什么,它仅访问此StringBuilder
吗? (它不会正确复制!).
So could someone explain me, what happens when I send a reference of StringBuilder
, does it only access this StringBuilder
? (It does not copy it right!).
以防万一我没有将输入参数的值或任何其他属性更改为方法.
And just in case I'm not changing the value or any other property of input arguments into methods.
因此,在这种情况下,变量足够大并且没有被操纵,我是否应该始终发送它的引用?如果是,它是否会干扰某些事物?
So, in this cases where variables are huge enough and not manipulated, should I always send the reference of it, and if yes does it interfere with something?
正确答案
看看以下Jon Skeet的文章,其中他彻底解释了按引用传递或按值传递之间的区别.
Take a look at the following article by Jon Skeet in which he thoroughly explains the difference between passing by reference or by value.
or this blog post which illustrates the former article:
http://rapidapplicationdevelopment.blogspot.com/2007 /01/parameter-passing-in-c.html
The last article actually uses the StringBuilder type in its examples, so you can clearly see what's going on in your case. If you pass in the StringBuilder type by reference you'll get this:
通过引用传递的引用类型:
Reference type passed by reference:
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfiecb
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20